Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[SOLVED] virtual functions in c++

Status
Not open for further replies.

jeffrey samuel

Advanced Member level 4
Joined
Jul 23, 2012
Messages
1,092
Helped
107
Reputation
214
Reaction score
107
Trophy points
1,363
Location
chennai,india
Activity points
6,373
why is the integer variables get reassigned once the control leave the first virtual function?
i found the value assigned to the variable changing when the next function of the same name is called in the program is it a limitation or some complier error cos i tried it in several system before concluding it.


note that the value is not garbage and it is distinct and is same for a set of values
 

Code:
class base 
{
void get()// initialises the variables to the value given by the user
virtual void show()//shows two values assigned
}
class derived
{
cmp()// compares the two integers and sends the largest
void show()// displays the largest no
}
main()
{
base *ptr// pointer to base
base a
ptr=&a
ptr->get()
ptr->show()
derived b
ptr=&b
ptr->cmp()// the values were changed when control reached this fn
ptr->show()// wrong op got displayed
}
 
Last edited:

Code:
class base 
{
void get()// initialises the variables to the value given by the user
virtual void show()//shows two values assigned
}
class derived
{
cmp()// compares the two integers and sends the largest
void show()// displays the largest no
}
main()
{
base *ptr// pointer to base
base a
ptr=&a
ptr->get()
ptr->show()
derived b
ptr=&b
ptr->cmp()// the values were changed when control reached this fn
ptr->show()// wrong op got displayed
}

I dont know why, you have not given all the necessary details. From the above structure it appears that the base class variables are protected and you are publicly inheriting derive class.
so the structure will be like this:-


Code C++ - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
class base 
{
protected:
int x,y;
public:
void get(int p, int q){x=p;y=q}                  // initialises the variables to the value given by the user
virtual void show(){ cout<<x<<y;}            //shows two values assigned
}
 
class derived: public base{
int cmp(){ 
               if(x>y)
                   return x;
               else
                   return y;
                }                                     // compares the two integers and sends the largest
 
void show()  {   cout<<cmp();}         // displays the largest no
}
 
int main()
{
base *ptr ;                 //POINTER TO BASE TYPE CREATED 
base a;                     //OBJECT OF BASE CLASS
ptr=&a;                    //BASE POINTER POINTS TO BASE CLASS OBJECT 
ptr->get(3,4) ;         // CALLS BASE'S get() METHOD
ptr->show();           // CALLS BASE'S show()
 
derived b;                // derive class's object
ptr=&b;                  //  base pointer points to derived class's object 
ptr->get(5,6);         // values for derived object's member variables     
//ptr->cmp();               //   this cannot be done. as per your description above cmp() returns a value. you can call like, int p=ptr->cmp();
ptr->show() ;              // access to derived class show(); This is where you will see the change. It is natural because its a seperate object 
return 0;
}



So you see everything happening according to the rule of Polymorphism. It is derermined at run time which version of a virtual function actually gets called. This determination is based solely upon the type of the object that is being pointed to by a base class pointer.

- - - Updated - - -

So the reason : In that particular line where you are noticing the change, actually it is comming from another object.
 
Last edited:
Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top