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

Class and Objects C++
CLASS AND OBJECT IN C++ WITH EXAMPLE FOUNDATION Lets define a variable x of integer type and give it a value 12. How will you do it. You will simply write int x=12; This statement can also be written...

C++ Overview
Introduction: C++ is the first object oriented programming language used universally as an advanced version of C language. Many features such as Inheritance, Class & Object, Polymorphism(Function...
N

Namrata Y.

0 0
0

Understanding Computer Science Concepts with Images and Videos..
All Computer science concepts relating to programming and software development are only virtual. It cannot be practically shown as a hardware parts of a computer. But for better understanding it should...

Upcasting and Downcasting
Converting a derived-class reference or pointer to a base-class reference or pointer is called upcasting. It is always allowed for public inheritance without the need for an explicit type cast. The downcasting,...

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...
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