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

Macro v/s Inline Function
Disadvantages of Macro compared to functions: Works on text substituiton mechanism by the preprocessor unlike normal functions where assembly-language Call and Return are used. Preprocessor macros...

Templates
Templates Advantages of templates: Code reuse Allows container classes (e.g. lists, arrays, etc.) to be simply defined without loss of static type checking or run-time efficiency. Allows definition...

Variables and Storage Classes
Variables and Storage Classes auto default variable type local to a function static retains the values between function calls initialized only once unavailable outside the scope of the function...

Looking for C++ Language Classes?

Learn from Best Tutors on UrbanPro.

Are you a Tutor or Training Institute?

Join UrbanPro Today to find students near you
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