Polymorphism-in-C++ .

In this session i have discussed polymorphism in C++ .


Polymorphism is a Greek word means having multiple form . In polymorphism name same and behaviour different . i.e A person is husband of a woman when he is in the company then he is employee of company . person one but functionalities different .

There are two types of polymorphism as given below .

1-Compile-Time-Polymorphism (Early binding or Static binding).

1-Run-Time-Polymorphism (Late binding or Dynamic binding).


1-Compile-Time-Polymorphism :

In compile time polymorphism the function link with object during compilation due to this it is called comipile time polymorphism . Everything will be decided in compile time . It can be implement by two ways given as follows .

1-Function-Overloading .

2-Operator-Overloading .

Function Overloading :

In function overloading there are many function with same name but their parameters (argument) differents .

Example : Write a program that demonstrate the compile time polymorphism .

    #include<iostream>
    using namespace std; 
    class A
    {
        public :
        int Sum (int x,int y) // passed two parameters
        {
            return x+y;
        }
        int Sum(int x,int y, int z) // passed three parameters
        {
            return(x+y+z);
        }
    };
    
    int main()
    {    
            A obj;
            int s1=obj.Sum(10,20);
            int s2=obj.Sum(10,20,30);
            cout<<"Sum : "<<s1;
            cout<<"Sum : "<<s2;
     
    }
    
    OUTPUT : S1 : 30
             S2 : 60
    

2-Run-Time-Polymorphism :

In run time polymorphism the function link with object at run time due to this , it is called run time polymorphism . It is also called dynmac binding or late binding . It is implement by the function overrriding .

Function overriding :

In function overriding the functions name same and arguments same but their behaviours differents . We use the virtual keyword . It is possible to define a function in derived class that has the same name , same argument and same return type in the base class .

Example : Write a program that demonstrate the run time polymorphism .

    #include<iostream>
    using namespace std; 
    class Base
    { 
        public: 
        virtual void disp () 
        { 
            cout<<"disp() of Base class" <<endl;
        } 
        void show () 
        {
             cout<<"show() of Base class"<<endl;
        } 
    }; 
   
    class Derived : public Base
    { 
        public: 
        void disp ()  
        { 
            cout<<"disp() of Derived class"<<endl; 
        } 
   
        void show () 
        { 
            cout<<"show() of Derived class"<<endl; 
        } 
    }; 
   
    int main()  
    { 
        Base *bptr; 
        Derived d; 
        bptr = &d; 
       //virtual function, binded at runtime (Runtime polymorphism) 
        bptr->disp();  
       // Non-virtual function, binded at compile time 
        bptr->show();  
       return 0; 
    }
    
    OUTPUT : disp() of Derived class
             show() of Base class
    

Welcome to AndroidATPoint.

back image                     next image