SlideShare a Scribd company logo
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
}

More Related Content

PPTX
virtual function
PPTX
Virtual function in C++ Pure Virtual Function
PPTX
Pure virtual function and abstract class
PPTX
07. Virtual Functions
PPTX
Virtual function complete By Abdul Wahab (moon sheikh)
PDF
Virtual Functions
PPTX
Pointers, virtual function and polymorphism
DOCX
Virtual function
virtual function
Virtual function in C++ Pure Virtual Function
Pure virtual function and abstract class
07. Virtual Functions
Virtual function complete By Abdul Wahab (moon sheikh)
Virtual Functions
Pointers, virtual function and polymorphism
Virtual function

What's hot (20)

PPT
16 virtual function
PPTX
Pointers,virtual functions and polymorphism cpp
PPTX
pointers,virtual functions and polymorphism
PPTX
Polymorphism
PPT
pointers, virtual functions and polymorphisms in c++ || in cpp
PPTX
Function overloading and overriding
PPTX
pointer, virtual function and polymorphism
PPTX
polymorphism and virtual function
PPT
3 Function Overloading
PPT
C++ polymorphism
PPTX
Polymorphism 140527082302-phpapp01
PPTX
Abstract Base Class and Polymorphism in C++
PPTX
Journey of a C# developer into Javascript
PPTX
Function overloading in c++
PPTX
Polymorphism in C++
PDF
2nd puc computer science chapter 8 function overloading
PPTX
Polymorphism
PPTX
C++ concept of Polymorphism
PDF
Functions in C++
16 virtual function
Pointers,virtual functions and polymorphism cpp
pointers,virtual functions and polymorphism
Polymorphism
pointers, virtual functions and polymorphisms in c++ || in cpp
Function overloading and overriding
pointer, virtual function and polymorphism
polymorphism and virtual function
3 Function Overloading
C++ polymorphism
Polymorphism 140527082302-phpapp01
Abstract Base Class and Polymorphism in C++
Journey of a C# developer into Javascript
Function overloading in c++
Polymorphism in C++
2nd puc computer science chapter 8 function overloading
Polymorphism
C++ concept of Polymorphism
Functions in C++
Ad

Similar to Virtual function (20)

PDF
22 scheme OOPs with C++ BCS306B_module4.pdf
PDF
polymorphism for b.tech iii year students
PPT
Polymorphism in C++ for beginners reference
PPTX
Oopsecondgrouppresentation 180726073512-converted (1)
PPTX
Object Oriented Programming using C++: Ch11 Virtual Functions.pptx
KEY
Javascript tid-bits
PPT
Lec 42.43 - virtual.functions
PPTX
Virtual, Pure Virtual functions and Abstract class.pptx
PPT
JavaScript - Programming Languages course
PDF
Javascript
PPTX
Lecture 14-overridding.pptx..................................
PPTX
Lecture 14-overridding.pptx,,,,,,,,,,,,,,,,,
PDF
JavaScript Essentials
PPTX
Lecture 7, c++(complete reference,herbet sheidt)chapter-17.
PDF
C++aptitude questions and answers
PPTX
4front en
PDF
C++ aptitude
PDF
Example for Virtual and Pure Virtual function.pdf
PPT
06 inheritance
22 scheme OOPs with C++ BCS306B_module4.pdf
polymorphism for b.tech iii year students
Polymorphism in C++ for beginners reference
Oopsecondgrouppresentation 180726073512-converted (1)
Object Oriented Programming using C++: Ch11 Virtual Functions.pptx
Javascript tid-bits
Lec 42.43 - virtual.functions
Virtual, Pure Virtual functions and Abstract class.pptx
JavaScript - Programming Languages course
Javascript
Lecture 14-overridding.pptx..................................
Lecture 14-overridding.pptx,,,,,,,,,,,,,,,,,
JavaScript Essentials
Lecture 7, c++(complete reference,herbet sheidt)chapter-17.
C++aptitude questions and answers
4front en
C++ aptitude
Example for Virtual and Pure Virtual function.pdf
06 inheritance
Ad

More from Burhan Ahmed (20)

PPTX
Wireless mobile communication
PPTX
Uses misuses and risk of software
PPTX
Types of computer
PPTX
PPTX
Topology
PPTX
The distinction of prophet muhammad (s.a.w) among the teachers of moral conduct
PPTX
Software house organization
PPT
Social interaction
PPTX
Role model
PPTX
Rights and duties
PPTX
Planning work activities
PPTX
Peripheral devices
PPTX
Parallel computing and its applications
PPTX
Operator overloading
PPT
Normalization
PPTX
Managing strategy
PPT
Letter writing
PPTX
Job analysis and job design
PPTX
Intellectual property
PPTX
Inheritance
Wireless mobile communication
Uses misuses and risk of software
Types of computer
Topology
The distinction of prophet muhammad (s.a.w) among the teachers of moral conduct
Software house organization
Social interaction
Role model
Rights and duties
Planning work activities
Peripheral devices
Parallel computing and its applications
Operator overloading
Normalization
Managing strategy
Letter writing
Job analysis and job design
Intellectual property
Inheritance

Recently uploaded (20)

PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PPTX
Week 4 Term 3 Study Techniques revisited.pptx
PDF
Anesthesia in Laparoscopic Surgery in India
PPTX
Cell Types and Its function , kingdom of life
PDF
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PDF
Origin of periodic table-Mendeleev’s Periodic-Modern Periodic table
PPTX
Cell Structure & Organelles in detailed.
PPTX
BOWEL ELIMINATION FACTORS AFFECTING AND TYPES
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PPTX
master seminar digital applications in india
PDF
TR - Agricultural Crops Production NC III.pdf
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PDF
Complications of Minimal Access Surgery at WLH
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PPTX
Microbial diseases, their pathogenesis and prophylaxis
PDF
Microbial disease of the cardiovascular and lymphatic systems
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
O5-L3 Freight Transport Ops (International) V1.pdf
Week 4 Term 3 Study Techniques revisited.pptx
Anesthesia in Laparoscopic Surgery in India
Cell Types and Its function , kingdom of life
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
Origin of periodic table-Mendeleev’s Periodic-Modern Periodic table
Cell Structure & Organelles in detailed.
BOWEL ELIMINATION FACTORS AFFECTING AND TYPES
Supply Chain Operations Speaking Notes -ICLT Program
master seminar digital applications in india
TR - Agricultural Crops Production NC III.pdf
Pharmacology of Heart Failure /Pharmacotherapy of CHF
Module 4: Burden of Disease Tutorial Slides S2 2025
Complications of Minimal Access Surgery at WLH
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
Microbial diseases, their pathogenesis and prophylaxis
Microbial disease of the cardiovascular and lymphatic systems

Virtual function

  • 2.  Parent class pointer can point to its child class object.  But converse is not true.
  • 3.  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.
  • 4. class parent { public: void f1() { cout<<"parent ka"<<endl; } }; class child:public parent { public: void f1() { cout<<"child ka"<<endl; } ;
  • 5. int main() { parent *p; child c; p=&c; c.f1(); // child's p->f1(); // parent's return 0; }
  • 6. 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.
  • 7.  This is called Problem of function overriding.  Solution Late Binding / Dynamic Binding How to achieve late / dynamic binding? Ans: virtual function
  • 8. Early Binding Late Binding Pointer type is considered Pointer’s pointing content’s type is considered.
  • 9. class parent { public: virtual void f1() { cout<<"parent"<<endl; } };
  • 10. int main() { parent *p; child c; p=&c; c.f1(); // child's p->f1(); // child’s return 0; }
  • 11. 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.
  • 12. 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.
  • 13. 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)
  • 14. 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 }