Implement Inheritance with C++ with respect to shape: Two-dimensional shape
Answers
Program:
#include<iostream>
using namespace std;
class Shape
{
protected:
double a,b;
public:
Shape(double aa, double bb)
{
a = aa;
b = bb;
}
Shape(double aa)
{
a = aa;
b = 0.0;
}
virtual void getArea() = 0;
};
class Square : public Shape
{
private:
double Area;
public:
Square(double aa) : Shape(aa)
{
Area = 0.0;
}
void getArea()
{
Area = a * a;
cout<<"Area of Square = "<<Area<<endl;
}
};
class Rectangle : public Shape
{
private:
double Area;
public:
Rectangle(double aa, double bb) : Shape(aa, bb)
{
Area = 0.0;
}
void getArea()
{
Area = a * b;
cout<<"Area of Rectangle = "<<Area<<endl;
}
};
int main()
{
double side;
cout<<"Enter side: ";
cin>>side;
Square s(side);
s.getArea();
cout<<endl;
double length, breadth;
cout<<"Enter length: ";
cin>>length;
cout<<"Enter breadth: ";
cin>>breadth;
Rectangle r(length, breadth);
r.getArea();
}