UrbanPro
true

Learn C++ Language from the Best Tutors

  • Affordable fees
  • 1-1 or Group class
  • Flexible Timings
  • Verified Tutors

Search in

Polymorphism and Virtual Functions

Darpan Kumar
18/05/2017 0 0

Polymorphism and Virtual Functions 

Compile Time Polymorphism or Early Binding or Static Binding

  • An object is bound to its function call at compile time.
  • Implemented using overloaded functions and operators
  • Main advantage is efficiency 

Run Time Polymorphism or Late Binding or Dynamic Binding 

  • Function calls are not resolved until run time
  • Implemented using virtual functions
  • Main advantage is flexibility 

Virtual Function 

  • Keyword virtual tells the compiler to enforce late binding of the function when the address of the base class is used
  • The inhertance from the base class to the derived class must be public
  • A call to a virtual function is resolved according to the underlying type of object for which it is called.
  • A call to a non-virtual function is resolved according to the type of the pointer or reference.
  • If a function is declared virtual in the base class it is virtual in all derived classes throghout the inhertance hierarchy.
  • Virtual function must be member of some class
  • Virtual function cannot be a static member function or a global function
  • A virtual function that has been defined in a base class need not be defined in the derived class. If it is not, the function defined for the base is called
  • It is an error for a derived class function to differ from a base class virtual function in the return type only.

 

Example: 

#include

using namespace std; 

class Base                                   // Base class

{

public:

          virtual void f1()                  // Virtual member function.

          { cout << "Called Base::f1()\n"; }

 

          virtual void f2()                  // Virtual member function.

          { cout << "Called Base::f2()\n"; } 

}; 

class Derived : public Base            // Derived class : public inheritance

{

public:

          void f1()                           // Implementation of virtual function - override

          { cout << "Called Derived::f1()\n"; } 

          void f3()

          { cout << "Called Derived::f3()\n"; } 

};            

 int main()

{

          Base *bp = new Derived;

          bp->f1();

          bp->f2();

          // bp->f3();                      // Error: Base::f3 not available 

          cout << "Size of Derived = " << sizeof(Derived) << endl;

          return 0;

Result:

Called Derived::f1()

Called Base::f2()

Size of Derived = 4   (VPTR) 

Mechanism of Virtual Function Call

  • Whenever there is one or more virtual function in a class the compiler secretly places a virtual pointer VPTR in the class.
  • The compiler secretly inserts code into the beginning of the constructor that initializes the VPTR to point to proper VTABLE.
  • When a virtual function call is made through a base class pointer (polymorphic call) the compiler quietly inserts code to fetch the VPTR and look up the function address in the VTABLE, thus calling the correct function and causing late binding to take place. 

 Function Overriding 

  • The redefinition of a virtual member function in a derived class with the same signature is called function overriding

Pure virtual function and abstract class

  • The use of one or more pure virtual function makes a class abstract, which means that no object can be instantiated from it.
  • When an abstract class is inherited, all the pure virtual functions must be implemented, or the inherited class becomes abstract as well. 

Syntax:

virtual void pure_virtual_func() = 0;

0 Dislike
Follow 0

Please Enter a comment

Submit

Other Lessons for You

Advantages of C++ Language
Advantages of C++ - C++ is a profoundly convenient dialect and is frequently the dialect of decision for multi-gadget, multi-stage application advancement. - C++ is a protest situated programming dialect...

C and C++ programming with memory level debugging
Understanding C and C++ programming by using memory level debugging. Step 1: Understand the Memory map of C executable Step 2: Start memory level debugging using popular IDE Step 3: Find the memory...

File Handling in C++ : Lesson 1 Data Persistence
Data persistence refers to the existence of data in the program. In other words, it means the life of data. Data is the most significant part of a program and it must be stored properly and easily retrieved...
J

C++ Program[Working with structure]
//Header files #include<iostream>#include<cstring> //Namespace usedusing namespace std; //Structure definition struct store{ int p_id; char p_name; int quantity; float price; float amount; }; //...
S

Compare C And C++ In Terms Of Header Files.
1."stdio.h" in c program controls input-output functionalities and operations associated with it where as in c++ program iostream.h" controls input-output functionalities and operations associated with...
X

Looking for C++ Language Classes?

The best tutors for C++ Language Classes are on UrbanPro

  • Select the best Tutor
  • Book & Attend a Free Demo
  • Pay and start Learning

Learn C++ Language with the Best Tutors

The best Tutors for C++ Language Classes are on UrbanPro

This website uses cookies

We use cookies to improve user experience. Choose what cookies you allow us to use. You can read more about our Cookie Policy in our Privacy Policy

Accept All
Decline All

UrbanPro.com is India's largest network of most trusted tutors and institutes. Over 55 lakh students rely on UrbanPro.com, to fulfill their learning requirements across 1,000+ categories. Using UrbanPro.com, parents, and students can compare multiple Tutors and Institutes and choose the one that best suits their requirements. More than 7.5 lakh verified Tutors and Institutes are helping millions of students every day and growing their tutoring business on UrbanPro.com. Whether you are looking for a tutor to learn mathematics, a German language trainer to brush up your German language skills or an institute to upgrade your IT skills, we have got the best selection of Tutors and Training Institutes for you. Read more