a. Define constructor to initialize the data members.
b. Define a virtual function to calculate area of the shape.
c. Create derive classes rectangle and circle and implement area function.
Create objects and show the implementation of defined operations.
![c++ programs examples](https://legalindiatech.com/wp-content/uploads/2023/04/C-legal-india-tech.png)
/* 9. Create a class Shape with two private data members.
a. Define constructor to initialize the data members.
b. Define a virtual function to calculate the area of the shape.
c. Create derive classes rectangle and circle and implement area
function. */
#include<iostream.h>
#include<conio.h>
#include<string.h>
#define pi 3.141
class Shape
{
private:
char s_name[15];
float s_area;
public:
Shape()
{
strcpy(s_name,” “);
s_area=0.0;
}
void init(float a)
{
s_area=a;
}
virtual void input() //virtual function
{
cout<<endl<<“Enter Shape Name=”<<endl;
cin>>s_name;
}
void display()
{
cout<<endl<<“*************************”<<endl<<endl;
cout<<“Shape name=”<<s_name<<endl;
cout<<s_name <<” “<<“Area=”<<s_area<<endl;
cout<<endl<<“*************************”<<endl<<endl;
}
};
class Rectangle:public Shape
{
private:
float length,width,r_area;
public:
Rectangle()
{
length=width=r_area=0.0;
}
void input()
{
Shape::input();
cout<<“Enter length=”<<endl;
cin>>length;
cout<<“Enter width=”<<endl;
cin>>width;
r_area=length*width;
Shape::init(r_area);
}
void display()
{
Shape::display();
}
};
class Circle:public Shape
{
private:
float radious,c_area;
public:
Circle()
{
radious=c_area=0.0;
}
void input()
{
Shape::input();
cout<<“Enter radious=”<<endl;
cin>>radious;
c_area=pi*radious*radious;
Shape::init(c_area);
}
void display()
{
Shape::display();
}
};
void main()
{
clrscr();
Rectangle r;
Circle c;
cout<<“>>>>> Rectangle <<<<<“<<endl<<endl;
r.input();
r.display();
cout<<“>>>>> Circle <<<<<“<<endl<<endl;
c.input();
c.display();
getch();
}