What is polymorphism in C++ ? || Basic Program for Polymorphism in C++ || Polymorphism || C++

In this program, we are going to Learn a basic program of Polymorphism in C++ Programming Language. 
 


The Code given below can be used in TURBO C++ Compilers: -

#include <iostream.h>
#include <conio.h>

class Overload
{
    public:
    void func(int x)
    {
        cout << "value of x is " << x << endl;
    }
    void func(double x)
    {
        cout << "value of x is " << x << endl;
    }
    void func(int x, int y)
    {
        cout << "value of x and y is " << x << ", " << y << endl;
    }
};

void main()
{
    clrscr();
    Overload obj1;
    obj1.func(7);
    obj1.func(3.142);
    obj1.func(69, 420);
    getch();
}

//.......Coded by RISHAB NAIR


The Code given below can be used in gcc/g++ Compilers: -

#include <iostream>
using namespace std;

class Shape
{
    public:
    virtual void draw()
    {
        cout << "drawing..." << endl;
    }
};

class Rectangle : public Shape
{
    public:
    void draw()
    {
        cout << "drawing rectangle..." << endl;
    }
};

class Circle : public Shape
{
    public:
    void draw()
    {
        cout << "drawing circle..." << endl;
    }
};

int main()
{
    Shape *s;
    Shape sh;
    Rectangle rec;
    Circle cir;
    s = &sh;
    s->draw();
    s = &rec;
    s->draw();
    s = &cir;
    s->draw();
    return 0;
}

//.......Coded by RISHAB NAIR


#ENJOY CODING

Post a Comment

FOR ANY DOUBTS AND ERRORS FEEL FREE TO ASK. YOUR DOUBTS WILL BE ADDRESSED ASAP

Previous Post Next Post