v_eman
asked on
virtual functions
I am a beginner in c++. I understand that, virtual function can be used to resolve the conflict. Something like this,
in the given example, if virtual is not used, the parent class function is called.
but , why some one wants to declare a pointer of parent class type object and define it for a child class object.
A *obj1; //A is the base class
obj1=new(B) // B inherits A
what is the use? any good example?..
I will go for
B *obj1;
obj1=new(B);
Then no need to worry about virtual.
# include<iostream>
using namespace std;
class A
{
public:
virtual void disp();
};
void A::disp()
{
cout<<"Class: A"<<endl;
}
class B : public A
{
public:
void disp();
};
void B::disp()
{
cout<<"Class: B"<<endl;
}
int main()
{
A *obj1;
obj1 = new(B);
obj1->disp();
getchar();
return 0;
}
in the given example, if virtual is not used, the parent class function is called.
but , why some one wants to declare a pointer of parent class type object and define it for a child class object.
A *obj1; //A is the base class
obj1=new(B) // B inherits A
what is the use? any good example?..
I will go for
B *obj1;
obj1=new(B);
Then no need to worry about virtual.
# include<iostream>
using namespace std;
class A
{
public:
virtual void disp();
};
void A::disp()
{
cout<<"Class: A"<<endl;
}
class B : public A
{
public:
void disp();
};
void B::disp()
{
cout<<"Class: B"<<endl;
}
int main()
{
A *obj1;
obj1 = new(B);
obj1->disp();
getchar();
return 0;
}
why some one wants to declare a pointer of parent class type object and define it for a child class object.
Look at it from a real world perspective
Lets say you initially design a "Father" class and then create an object of it. The Father has certain characteristics or properties. Now your client application may have been using only the Father class and referring to its behaviour by calling functions defined in the Father class.
A "Son" object is created later. There will be something in common between Father and Son to the extent that the Son can be considered as being similar to the Father or even a type of "Father".
Let's take the example of "Walk" being a characteristic which both Father and Son have. Now you have defined a function "Walk" in Father and have called it in your client application in several places.
Sometime in the future you decide that you want to replace the Father's walk with the Son's walk and so then all you need to do is use the same Father pointer in your client application but just initialize it with a "Son" object.
Now because "Walk" was a virtual function you automatically end up calling Son's Walk function wherever Father's Walk was called.
This is putting it in very layman terms.
If you want to read up about virtual functions you also need to understand inheritance.
http://www.cplusplus.com/doc/tutorial/inheritance/
http://www.cplusplus.com/doc/tutorial/polymorphism/
Look at it from a real world perspective
Lets say you initially design a "Father" class and then create an object of it. The Father has certain characteristics or properties. Now your client application may have been using only the Father class and referring to its behaviour by calling functions defined in the Father class.
A "Son" object is created later. There will be something in common between Father and Son to the extent that the Son can be considered as being similar to the Father or even a type of "Father".
Let's take the example of "Walk" being a characteristic which both Father and Son have. Now you have defined a function "Walk" in Father and have called it in your client application in several places.
Sometime in the future you decide that you want to replace the Father's walk with the Son's walk and so then all you need to do is use the same Father pointer in your client application but just initialize it with a "Son" object.
Now because "Walk" was a virtual function you automatically end up calling Son's Walk function wherever Father's Walk was called.
This is putting it in very layman terms.
If you want to read up about virtual functions you also need to understand inheritance.
http://www.cplusplus.com/doc/tutorial/inheritance/
http://www.cplusplus.com/doc/tutorial/polymorphism/
class Father{
public :
virtual Walk();
};
class Son : public Father {
public:
virtual Walk();
};
//below is your client app
main()
{
Father* aFamily = new Father; //or new Son();
aFamily->Walk(); //calls Father->Walk() or Son->Walk()
delete aFamily;
aFamily = 0;
}
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
Open in new window