C++ Polymorphism

October 27, 2015 Posted by WithU Technologies
"Poly" means "many" and "morph" means "form".
·         Polymorphism is the ability of an object (or reference) to assume (be replaced by) or become many different forms of object.

·         Polymorphism is briefly described as "one interface, many implementations."

There are two types of polymorphism one is Compile-Time Polymorphism and the other is Run-Time Polymorphism.

Compile Time Polymorphism:
·         Operator Overloading,
·         Function Overloading

Run Time Polymorphism: 

·         Interface methods
·         abstract methods
·         Virtual member functions.
·         Inheritance

Example:

#include <iostream>
using namespace std;

// Base class

class Cars{

protected:                  //accessed by the derived class

 float Displacement;

 float Torque;

 float Power;

   public:

    void setDisplacement(float d){    //function to assign the value of d parameter to Displacement variable

      Displacement = d;

    }

    void setTorque(float t){        //function to assign the value of t parameter to Torque variable

      Torque = t;

    }

    void setPower(float p){        //function to assign the value of p parameter to Power variable

      Power = p;

    }

 };

// Derived class

class HyundaiCreta: public Cars    //HyundaiCreta is Derived Class and Cars is the Base class

{

   public:

    void SetHyundai(){   //All the three protected variable member are accessed and used within a sentence.

      cout<<"Hyundai's SUV is expected to come with a refined "<<Displacement<<"-litre diesel engine making "<<Power<<"bhp and "<<Torque<<"kgm of torque."<<endl;

    }

};

// Derived class

class MarutiS_cross: public Cars    //MarutiS_cross is Derived Class and Cars is the Base class

{

   public:

      void SetMaruti(){

            cout<<"The Maruti Suziki S-Cross will come with a Fiat-sourced "<<Power<<"bhp "<<Displacement<<"-litre, which develops "<<Torque<<"kgm of torque and will come."<<endl;

          }

};

// Derived class

class RenaultDuster: public Cars    //RenaultDuster is Derived Class and Cars is the Base class

{

   public:

      void SetRenault(){

                  cout<<"Renault offers a "<<Power<<"bhp, "<<Displacement<<"-litre petrol motor with a torque figure of "<<Torque<<"kgm."<<endl;

                }

};

int main()   //main function

{

      //Declared three Object for each class

    HyundaiCreta h;    

    MarutiS_cross m;   

    RenaultDuster r;

   

   //Three pointers of type Cars pointing them to the Derived Cars

   Cars *c1 = &h;     

   Cars *c2 = &m;

   Cars *c3 = &r;



   //Arrow selection operator is used

   //Each value is passed to its respective function

   //And then according to the pointer the values are used within their respective cars sentence

   c1->setDisplacement(1.6);

   c1->setTorque(26.5);

   c1->setPower(126);



   c2->setDisplacement(1.6);

   c2->setTorque(32.6);

   c2->setPower(118);



   c3->setDisplacement(1.5);

   c3->setTorque(25.3);

   c3->setPower(108.5);

  

   //To output the sentences the following functions are called by their classes through their respective objects

   h.SetHyundai();

   m.SetMaruti();

   r.SetRenault();



   return 0;    //return void

 }

/* Output:

Hyundai's SUV is expected to come with a refined 1.6-litre diesel engine making 126bhp and 26.5kgm of torque.

The Maruti Suziki S-Cross will come with a Fiat-sourced 118bhp 1.6-litre, which develops 32.6kgm of torque and will come.

Renault offers a 108.5bhp, 1.5-litre petrol motor with a torque figure of 25.3kgm.

 */

·         In the example shown above we created a class of Cars as the Base class and then derived three models of cars from that base class.

·         We initialized three float variables as protected member variables.

·         Those variables are then used by the base class to assign the parameter values to them. So that we use those values which are forwarded from the main function.

·         Then we use those values for each car to create a sentences which will use all three variables.

·          In the main function we declared three object and each object for each class.

·         Then we create three pointers and address them to each car models.

·         Then each pointer is used to set the values. Instead of calling the functions directly on the objects, it is faster and more efficient to use pointers.

·         After that we call each functions for each car models, which will display the sentence along with their respective values.