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

Lets learn why OOPS is popular?
1. Supports reusability- It means that once a class is designed it works just like a template which can be inherited. 2. Provides better management- It means that abstract classes and interfaces guide...
R

Working with C/C++ applications
Inorder to learn C and C++ programming languages one can work with various editors available.To name a few are the most popular one is turbo c++, DEV C++, Eclipse, NetBeans. Here are the screen shots...

Compiler vs Interpreter
Compiler Interpreter Convert all the code into binary format and execute. Convert one statement at a time and execute, then Convert next statement and execute. After conversion, source...

Difference Of Inheritance In C++ And Java
In Java , multiple inheritance is not applicable directly but we can implement the concept by using the interfaces. In c++ and Java, the common types of inheritances are: Single Multi level Hybrid Hierarchical

Why do pointers have a datatype?
Before we start with pointers you must know what is a variable and a datatype. int a; This is the basic line in every program in 'C' . It means that we are asking the compiler to give us 2 bytes of space...
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