Polymorphism in C++

Polymorphism in C++

C++ polymorphism means that a call to a member function will cause a different function to be executed depending on the type of object that invokes the function.

Polymorphism in c++ AndroWep-Tutorials
Polymorphism in c++ AndroWep-Tutorials

Runtime Polymorphism Example

#include <iostream>    
using namespace std;    
class Animal {    
    public:    
void eat(){      
cout<<"Eating...";      
    }        
};     
class Dog: public Animal      
{      
 public:    
 void eat()      
    {           cout<<"Eating bread...";      
    }      
};    
int main(void) {    
   Dog d = Dog();      
   d.eat();    
   return 0;    
}    
Eating bread...
#include <iostream>    
using namespace std;    
class Shape {                                        //  base class  
    public:    
virtual void draw(){                             // virtual function  
cout<<"drawing..."<<endl;      
    }        
};     
class Rectangle: public Shape                  //  inheriting Shape class.  
{      
 public:    
 void draw()      
   {      
       cout<<"drawing rectangle..."<<endl;      
    }      
};    
class Circle: public Shape                        //  inheriting Shape class.  
  
{      
 public:    
 void draw()      
   {      
      cout<<"drawing circle..."<<endl;      
   }      
};    
int main(void) {    
    Shape *s;                               //  base class pointer.  
    Shape sh;                               // base class object.  
       Rectangle rec;    
        Circle cir;    
      s=&sh;    
     s->draw();     
        s=&rec;    
     s->draw();      
    s=?    
    s->draw();     
}    
drawing...
drawing rectangle...
drawing circle...

Runtime Polymorphism with Data Members

#include <iostream>    
using namespace std;    
class Animal {                                          //  base class declaration.  
    public:    
    string color = "Black";      
};     
class Dog: public Animal                       // inheriting Animal class.  
{      
 public:    
    string color = "Grey";      
};    
int main(void) {    
     Animal d= Dog();      
    cout<<d.color;     
}    

This code generates a pseudorandom number at run time. If it generates an even number, it directs p to point to a plain Text object; Which method will this code call, Text::get or FancyText::get?

#include <iostream>
 // A class with no virtual methods
class NoVTable {
    int data;
    public:
        void set(int d) {
            data = d;
        }
    int get() {
        return data;
    }
};
// A class with virtual methods
class HasVTable {
    int data;
    public:
        virtual void set(int d) {
            data = d;
        }
    virtual int get() {
        return data;
    }
};
int main() {
    NoVTable no_vtable;
    HasVTable has_vtable;
    no_vtable.set(10);
    has_vtable.set(10);
    std::cout << "no_vtable size = " << sizeof no_vtable << '\n';
    std::cout << "has_vtable size = " << sizeof has_vtable << '\n';
}
no_vtable size = 4
has_vtable size = 8

Dynamic binding enables a powerful technique in object-oriented programming called polymorphism

#include <string>

#include <vector>

#include <iostream>
 // Base class for all Text derived classes
class Text {
    std::string text;
    public:
        Text(const std::string & t): text(t) {}
    virtual std::string get() const {
        return text;
    }
};
// Provides minimal decoration for the text
class FancyText: public Text {
    std::string left_bracket;
    std::string right_bracket;
    std::string connector;
    public:
        FancyText(const std::string & t,
            const std::string & left,
                const std::string & right,
                    const std::string & conn):
        Text(t), left_bracket(left), right_bracket(right),
        connector(conn) {}
    std::string get() const override {
        return left_bracket + Text::get() + right_bracket;
    }
};
// The text is always the word FIXED
class FixedText: public Text {
    public: FixedText(): Text("FIXED") {}
};
int main() {
    std::vector < Text * > texts {
        new Text("Wow"),
            new FancyText("Wee", "[", "]", "-"),
            new FixedText,
            new FancyText("Whoa", ":", ":", ":")
    };
    for (auto t: texts)
        std::cout << t - > get() << '\n';
}

We know from Section 11.1 that a vector is a collection of homogeneous elements. Homogeneous means the elements in a vector must all be of the same type. In the declared type of the texts vector is std::vector.

Wow
[Wee]
FIXED
:Whoa:

What is polymorphism and example?

The word polymorphism means having many forms. … Real life example of polymorphism: A person at the same time can have different characteristic. Like a man at the same time is a father, a husband, an employee. So the same person posses different behaviour in different situations. This is called polymorphism.