Area of Rectangle and Circle using Abstract Class and Virtual Functions || 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 gcc/g++ Compilers: -

#include <iostream>
using namespace std;

//Abstract Class
class Shape 
{
    public:
    virtual void Enter_data() = 0;
    virtual void Area() = 0; // pure virtual function
};

class Rectangle : public Shape // First Derived class
{
    private:
    float length;
    float breadth;

    public:
    void Enter_data()
    {
        cout << "\n Enter the data for the Rectangle........";
        cout << "\n\t Enter the length of the rectangle: ";
        cin >> length;
        cout << "\t Enter the breadth of the rectangle: ";
        cin >> breadth;
    }
    void Area()
    {
        cout << "\n\t The area of the rectangle = " << (length * breadth);
    }
};

class Circle : public Shape // Second Derived class
{
    private:
    float radius;

    public:
    void Enter_data()
    {
        cout << "\n\n Enter the data for the Circle...........";
        cout << "\n\t Enter the radius of the circle: ";
        cin >> radius;
    }
    void Area()
    {
        cout << "\n\t The area of the circle = " << (3.14 * radius * radius);
    }
};

int main()
{
    Shape *shp;    // pointer to the object of the base class Shape
    Rectangle rec; // object of class Rectangle
    shp = &rec;
    shp->Enter_data();
    shp->Area();
    Circle cir; // object of class Circle
    shp = &cir;
    shp->Enter_data();
    shp->Area();
    return 0;
}

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

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

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

//Abstract Class
class Shape 
{
    public:
    virtual void Enter_data() = 0;
    virtual void Area() = 0; // pure virtual function
};

class Rectangle : public Shape // First Derived class
{
    private:
    float length;
    float breadth;

    public:
    void Enter_data()
    {
        cout << "\n Enter the data for the Rectangle........";
        cout << "\n\t Enter the length of the rectangle: ";
        cin >> length;
        cout << "\t Enter the breadth of the rectangle: ";
        cin >> breadth;
    }
    void Area()
    {
        cout << "\n\t The area of the rectangle = " << (length * breadth);
    }
};

class Circle : public Shape // Second Derived class
{
    private:
    float radius;

    public:
    void Enter_data()
    {
        cout << "\n\n Enter the data for the Circle...........";
        cout << "\n\t Enter the radius of the circle: ";
        cin >> radius;
    }
    void Area()
    {
        cout << "\n\t The area of the circle = " << (3.14 * radius * radius);
    }
};

void main()
{
    clrscr();
    Shape *shp;    // pointer to the object of the base class Shape
    Rectangle rec; // object of class Rectangle
    shp = &rec;
    shp->Enter_data();
    shp->Area();
    Circle cir; // object of class Circle
    shp = &cir;
    shp->Enter_data();
    shp->Area();
    getch();
}

//.......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