#include<iostream>
#include<cmath>
using namespace std;
float sum=0;
float s[5];
class Shape
{public:
virtual void printArea()const{}
virtual void shapeName()const=0;
};
class Circle:public Shape
{public:
Circle(float);
virtual void shapeName()const{cout<<"Area of Circle is: ";}
virtual void printArea()const;
private:
float radius;
};
Circle::Circle(float r)
{
radius=r;
}
void Circle::printArea()const
{
s[0]=3.14*radius*radius;
sum+=s[0];
cout<<s[0]<<endl;
}
class Rectangle:public Shape
{public:
Rectangle(float ,float);
virtual void shapeName()const{cout<<"Area of Rectangle is: ";}
virtual void printArea()const;
private:
float length;
float width;
};
Rectangle::Rectangle(float l,float w)
{
length=l;
width=w;
}
void Rectangle::printArea()const
{
s[1]=length*width;
sum+=s[1];
cout<<s[1]<<endl;
}
class Triangle:public Shape
{public:
Triangle(float ,float,float);
virtual void shapeName()const{cout<<"Area of Rectangle is: ";}
virtual void printArea()const;
private:
float a;
float b;
float c;
};
Triangle::Triangle(float m,float n,float k)
{
a=m;
b=n;
c=k;
}
void Triangle::printArea()const
{
float p;
p=(a+b+c)/2;
s[2]=sqrt(p*(p-a)*(p-b)*(p-c));
sum+=s[2];
cout<<s[2]<<endl;
}
class Trapezoid:public Shape
{public:
Trapezoid(float ,float ,float);
virtual void shapeName()const{cout<<"Area of Trapezoid is: ";}
virtual void printArea()const;
private:
float up;
float down;
float height;
};
Trapezoid::Trapezoid(float u,float d,float h)
{
up=u;
down=d;
height=h;
}
void Trapezoid::printArea()const
{
s[3]=0.5*(up+down)*height;
sum+=s[3];
cout<<s[3]<<endl;
}
class Square:public Shape
{public:
Square(float);
virtual void shapeName()const{cout<<"Area of Square is: ";}
virtual void printArea()const;
private:
float side;
};
Square::Square(float s)
{
side=s;
}
void Square::printArea()const
{
s[4]=side*side;
sum+=s[4];
cout<<s[4]<<endl;
}
int main()
{
Circle c(10);
Rectangle r(5,2);
Triangle t(3,2,4);
Trapezoid z(1,2,3);
Square s(5);
Shape *p[5];
int i;
p[0]=&c;
p[0]->shapeName();
p[0]->printArea();
p[1]=&r;
p[1]->shapeName();
p[1]->printArea();
p[2]=&t;
p[2]->shapeName();
p[2]->printArea();
p[3]=&z;
p[3]->shapeName();
p[3]->printArea();
p[4]=&s;
p[4]->shapeName();
p[4]->printArea();
cout<<"The total area is: "<<sum<<endl;
return 0;
}
#include<cmath>
using namespace std;
float sum=0;
float s[5];
class Shape
{public:
virtual void printArea()const{}
virtual void shapeName()const=0;
};
class Circle:public Shape
{public:
Circle(float);
virtual void shapeName()const{cout<<"Area of Circle is: ";}
virtual void printArea()const;
private:
float radius;
};
Circle::Circle(float r)
{
radius=r;
}
void Circle::printArea()const
{
s[0]=3.14*radius*radius;
sum+=s[0];
cout<<s[0]<<endl;
}
class Rectangle:public Shape
{public:
Rectangle(float ,float);
virtual void shapeName()const{cout<<"Area of Rectangle is: ";}
virtual void printArea()const;
private:
float length;
float width;
};
Rectangle::Rectangle(float l,float w)
{
length=l;
width=w;
}
void Rectangle::printArea()const
{
s[1]=length*width;
sum+=s[1];
cout<<s[1]<<endl;
}
class Triangle:public Shape
{public:
Triangle(float ,float,float);
virtual void shapeName()const{cout<<"Area of Rectangle is: ";}
virtual void printArea()const;
private:
float a;
float b;
float c;
};
Triangle::Triangle(float m,float n,float k)
{
a=m;
b=n;
c=k;
}
void Triangle::printArea()const
{
float p;
p=(a+b+c)/2;
s[2]=sqrt(p*(p-a)*(p-b)*(p-c));
sum+=s[2];
cout<<s[2]<<endl;
}
class Trapezoid:public Shape
{public:
Trapezoid(float ,float ,float);
virtual void shapeName()const{cout<<"Area of Trapezoid is: ";}
virtual void printArea()const;
private:
float up;
float down;
float height;
};
Trapezoid::Trapezoid(float u,float d,float h)
{
up=u;
down=d;
height=h;
}
void Trapezoid::printArea()const
{
s[3]=0.5*(up+down)*height;
sum+=s[3];
cout<<s[3]<<endl;
}
class Square:public Shape
{public:
Square(float);
virtual void shapeName()const{cout<<"Area of Square is: ";}
virtual void printArea()const;
private:
float side;
};
Square::Square(float s)
{
side=s;
}
void Square::printArea()const
{
s[4]=side*side;
sum+=s[4];
cout<<s[4]<<endl;
}
int main()
{
Circle c(10);
Rectangle r(5,2);
Triangle t(3,2,4);
Trapezoid z(1,2,3);
Square s(5);
Shape *p[5];
int i;
p[0]=&c;
p[0]->shapeName();
p[0]->printArea();
p[1]=&r;
p[1]->shapeName();
p[1]->printArea();
p[2]=&t;
p[2]->shapeName();
p[2]->printArea();
p[3]=&z;
p[3]->shapeName();
p[3]->printArea();
p[4]=&s;
p[4]->shapeName();
p[4]->printArea();
cout<<"The total area is: "<<sum<<endl;
return 0;
}