Create a class by name Area and initialize the data members. Then, find areas of square [Area(a)], area of rectangle [Area(l, b)], area of triangle [Area(a, b, c)] using constructor overloading. Also define a destructor to destroy the object.
#include <iostream>
#include <math.h>
using namespace std;
class Area{
public:
double area;
Area(double a){
area = a*a;
}
Area(double l, double b){
area = b * l;
}
Area(double a, double b, double c){
float s = 0.5 * (a + b + c);
area = sqrt(s * (s - a)*(s - b)*(s - c));
}
~Area(){
};
};
int main()
{
Area triangle(2,3,4);
Area square(2);
Area rect(2,8);
cout<<triangle.area <<endl;
cout<< square.area << endl;
cout<<rect.area<< endl;
return 0;
}
Comments
Leave a comment