Virtual
Function
 Parent class pointer can point to its child class
object.
 But converse is not true.
 If we have functions with the same names in
parent class and child class as well.
 If this function is called using child class
object then child’s function will be executed.
class parent
{
public:
void f1()
{
cout<<"parent ka"<<endl;
}
};
class child:public parent
{
public:
void f1()
{
cout<<"child ka"<<endl;
}
;
int main()
{
parent *p;
child c;
p=&c;
c.f1(); // child's
p->f1(); // parent's
return 0;
}
Need of virtual function
 The need is based on a problem that is:
If parent class pointer (pointing to child class’s
address) calls the function then which function will
be run?
 Answer: Early Binding
 when object calls -> type of object
 when pointer calls -> type of pointer
So the parent’s function will be executed even
pointer is pointing to child class object.
 This is called Problem of function
overriding.
 Solution
Late Binding / Dynamic Binding
How to achieve late / dynamic binding?
Ans: virtual function
Early Binding Late Binding
Pointer type is considered Pointer’s pointing content’s type is
considered.
class parent
{
public:
virtual void f1()
{
cout<<"parent"<<endl;
}
};
int main()
{
parent *p;
child c;
p=&c;
c.f1(); // child's
p->f1(); // child’s
return 0;
}
Virtual function working concept
 Compiler do 2 tasks when the virtual function is
made in a class.
 Create a pointer in parent class.
 Object dependent pointer.
 Create just 1 time .
 Inherited by all descendent classes.
 Create a pointer array
 Not object dependent.
 Create for all classes
 Not inherited by all descendent classes.
 Each class has its own array.
What does this pointer contains/ points?
Ans: the address of pointer array. It will point
that particular array which class object is made.
What does pointer array contains?
Ans: addresses of virtual functions.
class A
{
*vptr
public:
void f1();
virtual void f2();
virtual void f3();
virtual void f4();
};
class B : public A
{
public:
void f1();
void f2();
void f4(int x); // not ovveriding
};
vtable
vtable
F2
addre
ss
F3
addre
ss
F4
addre
ss
F2
addre
ss
F3
adres
s
(inher
ited)
F4
adres
s
(inher
ited)
Example 1 Example 2
main()
{
A *p , o;
p= &o;
p->f1();EB // A
p->f2(); LB // A
p-> f3(); LB //A
p->f4(); LB // A
p-> f4(5); EB // error
}
main()
{
A *p ;
B o;
p= &o;
p->f1();EB // A
p->f2(); LB // B
p-> f3(); LB // A
p->f4(); LB // A
p-> f4(5); EB // error
}

Virtual function

  • 1.
  • 2.
     Parent classpointer can point to its child class object.  But converse is not true.
  • 3.
     If wehave functions with the same names in parent class and child class as well.  If this function is called using child class object then child’s function will be executed.
  • 4.
    class parent { public: void f1() { cout<<"parentka"<<endl; } }; class child:public parent { public: void f1() { cout<<"child ka"<<endl; } ;
  • 5.
    int main() { parent *p; childc; p=&c; c.f1(); // child's p->f1(); // parent's return 0; }
  • 6.
    Need of virtualfunction  The need is based on a problem that is: If parent class pointer (pointing to child class’s address) calls the function then which function will be run?  Answer: Early Binding  when object calls -> type of object  when pointer calls -> type of pointer So the parent’s function will be executed even pointer is pointing to child class object.
  • 7.
     This iscalled Problem of function overriding.  Solution Late Binding / Dynamic Binding How to achieve late / dynamic binding? Ans: virtual function
  • 8.
    Early Binding LateBinding Pointer type is considered Pointer’s pointing content’s type is considered.
  • 9.
    class parent { public: virtual voidf1() { cout<<"parent"<<endl; } };
  • 10.
    int main() { parent *p; childc; p=&c; c.f1(); // child's p->f1(); // child’s return 0; }
  • 11.
    Virtual function workingconcept  Compiler do 2 tasks when the virtual function is made in a class.  Create a pointer in parent class.  Object dependent pointer.  Create just 1 time .  Inherited by all descendent classes.  Create a pointer array  Not object dependent.  Create for all classes  Not inherited by all descendent classes.  Each class has its own array.
  • 12.
    What does thispointer contains/ points? Ans: the address of pointer array. It will point that particular array which class object is made. What does pointer array contains? Ans: addresses of virtual functions.
  • 13.
    class A { *vptr public: void f1(); virtualvoid f2(); virtual void f3(); virtual void f4(); }; class B : public A { public: void f1(); void f2(); void f4(int x); // not ovveriding }; vtable vtable F2 addre ss F3 addre ss F4 addre ss F2 addre ss F3 adres s (inher ited) F4 adres s (inher ited)
  • 14.
    Example 1 Example2 main() { A *p , o; p= &o; p->f1();EB // A p->f2(); LB // A p-> f3(); LB //A p->f4(); LB // A p-> f4(5); EB // error } main() { A *p ; B o; p= &o; p->f1();EB // A p->f2(); LB // B p-> f3(); LB // A p->f4(); LB // A p-> f4(5); EB // error }