Polymorphism Write a simple polymorphic program of your choi
Polymorphism: Write a simple polymorphic program of your choice that contains at least one virtual function, one concrete function and one abstract class.
Solution
#include <iostream>
 using namespace std;
class Shape //virtual base class
 {
 public:
 virtual void Area()=0; //pure vrtual function Area()
 
 };
class Circle : public Shape //inheritance
 {
    private:
    double radius;
 public:
 Circle(double r) //constructor
 {
     radius =r;
 }
 double getRadius() //get methods
 {
     return radius;
 }
 void Area() //redefining Area() function
 {
     cout << \"\ Area of circle :\"<< (3.14*getRadius()*getRadius());
 }
 };
class Rectangle : public Shape //inheriting abstract base class Shape
 {
    private:
    double length,width;
 public:
 Rectangle(double l,double w) //constructor to set values
 {
     length =l;
     width = w;
 }
 double getLength() //get methods
 {
     return length;
 }
 double getWidth()
 {
     return width;
 }
 
 void Area() //redefining Area() function
 {
     cout << \"\ Area of rectangle : \"<< (getLength()*getWidth());
 }
 };
 class Sphere : public Shape //inheriting abstract base class
 {
    private:
    double radius;
    public:
    Sphere(double r) //constructor to set radius
    {
        radius=r;
    }
    double getRadius() //get method
    {
        return radius;
    }
    void Area() //redefining virtual method
    {
        cout<<\"\ Area of sphere :\"<<(4*3.14*getRadius()*getRadius());
    }
 };
 int main()
 {
 Rectangle rect(4.5,10.0); //objects of classes
 Circle cir(3.3);
 Sphere sphr(3.3);
   
 Shape *r = ▭ //base class pointer stores the address of derived class object
 Shape *c = ○
 Shape *s = &sphr;
   
 
 r->Area(); //base class pointer is used to call different methods of different classes ,polymorphism
 c->Area();
 s->Area();
   
 
 return 0;
 }
output:
Success time: 0 memory: 3468 signal:0


