Sunday, September 13, 2020

Polymorphism in C++

 


C++ Polymorphism

Polymorphism is an important concept of object-oriented programming. It simply means more than one form. That is, the same entity (function or operator) behaves differently in different scenarios. For example,

The + operator in C++ is used to perform two specific functions. When it is used with numbers (integers and floating-point numbers), it performs addition.

int a = 5;

int b = 6;

int sum = a + b;    // sum = 11

 

And when we use the + operator with strings, it performs string concatenation. For example,

string firstName = "abc ";

string lastName = "xyz";

// name = "abc xyz"

string name = firstName + lastName;

 

We can implement polymorphism in C++ using the following ways:

1.      Function overloading
2.     Operator overloading
3.     Function overriding
4.    Virtual functions
5.     Function Overriding

Function Overriding

The same function is defined in both the derived class and the based class. Now if we call this function using the object of the derived class, the function of the derived class is executed.

This is known as function overriding in C++. The function in derived class overrides the function in base class.

 // C++ program to demonstrate function overriding

#include <iostream>

using namespace std;

 class Base {

   public:

    void print() {

        cout << "Base Function" << endl;

    }

};

 class Derived : public Base {

   public:

    void print() {

        cout << "Derived Function" << endl;

    }

};

 int main() {

    Derived derived1;

    derived1.print();

    return 0;

}

 Output

Derived Function


 Note: // access print() function of the Base class

    Derived1.Base::print();

 

C++ Virtual Function
A C++ virtual function is a member function in the base class that you redefine in a derived class. It is declared using the virtual keyword.
·      It is used to tell the compiler to perform dynamic linkage or late binding on the function.
·       There is a necessity to use the single pointer to refer to all the objects of the different classes. So, we create the pointer to the base class that refers to all the derived objects. But, when base class pointer contains the address of the derived class object, always executes the base class function. This issue can only be resolved by using the 'virtual' function.
·       A 'virtual' is a keyword preceding the normal declaration of a function.
·       When the function is made virtual, C++ determines which function is to be invoked at the runtime based on the type of the object pointed by the base class pointer.

Rules of Virtual Function
·       Virtual functions must be members of some class.
·       Virtual functions cannot be static members.
·       They are accessed through object pointers.
·       They can be a friend of another class.
·       A virtual function must be defined in the base class, even though it is not used.
·       The prototypes of a virtual function of the base class and all the derived classes must be identical. If the two functions with the same name but different prototypes, C++ will consider them as the overloaded functions.
·       We cannot have a virtual constructor, but we can have a virtual destructor

 

Consider the situation when we don't use the virtual keyword.

#include <iostream>  

using namespace std;  

class A  

    public:  

    void display()  

    {  

        std::cout << "Base Class Invoked " <<  endl;  

    }  

};  

class B: public A  

{  

  public:  

    void display()  

    {  

        std::cout << "Derived Class Invoked " << endl;  

    }  

};  

int main()  

{  

    A *a;  

    B b;  

    a = &b;  

   a->display();  

   return 0;  

}  

Output:

Base Class Invoked 

In the above example, * a is the base class pointer. The pointer can only access the base class members but not the members of the derived class. Although C++ permits the base pointer to point to any object derived from the base class, it cannot directly access the members of the derived class. Therefore, there is a need for virtual function which allows the base pointer to access the members of the derived class.

 

C++ virtual function Example

Let's see the simple example of C++ virtual function used to invoke the derived class in a program.

#include <iostream>  

  using namespace std; 

class A   

{    

 public:    

 virtual void display()    

 {    

  cout << "Base class is invoked"<<endl;    

 }    

};    

class B:public A    

{    

 public:    

 void display()    

 {    

  cout << "Derived Class is invoked"<<endl;    

 }    

};    

int main()    

{    

 A* a;    //pointer of base class    

 B b;     //object of derived class    

 a = &b;    

 a->display();   //Late Binding occurs    

}    

Output:

Derived Class is invoked 

No comments:

Post a Comment