Source code:
#include<iostream>
#include<cmath>
#include<iomanip>
using namespace std;
class polygon
{protected:
double area;
double perimeter;
virtual void calcarea()=0;
virtual void calcperi()=0;
public:
polygon(){
}
void printarea()
{cout<<"area of figure is : "<<area<<endl;
}
void printperi()
{cout<<"perimeter of figure is : "<<perimeter<<endl;
}
};
class rectangle:public polygon
{public:
double length;
double breadth;
rectangle(double l,double b){length=l;
breadth = b;
}
void calcarea()
{
cout<<"area is "<<length*breadth;
}
void calcperi()
{
cout<<"perimeter is "<<(length+breadth)*2;
}
};
class square : public polygon
{public:
double side;
square(double s){side = s;}
void calcarea(){
cout<<"Area is : "<<side*side<<endl;
}
void calcperi(){
cout<<"perimeter is : "<<4*side<<endl;
}
};
class triangle:public polygon
{public:
double side_a;
double side_b;
double side_c;
triangle(double a,double b,double c ){side_a = a;
side_b = b, side_c = c;
}
void calcarea()
{
float s=(side_a+side_b+side_c)/2;
cout<<"area is "<<sqrt(s*(s-side_a)*(s-side_b)*(s-side_c));
}
void calcperi()
{
cout<<"perimeter is "<<(side_a=side_b=side_c)*2;
}
};
int main()
{rectangle s1(16,4);
square s2(6);
triangle s3(88,55,99);
cout<<"for rectangle: "<<endl;
s1.calcarea();
cout<<endl;
s1.calcperi();
cout<<endl;
cout<<"for square : "<<endl;
s2.calcarea();
s2.calcperi();
cout<<"for triangle: "<<endl;
s3.calcarea();
cout<<endl;
s3.calcperi();
cout<<endl;
return 0;
}
0 Comments
Post a Comment