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.
(Initialize the class members using constructors and destroy the objects by using destructor)
#include <iostream>
#include <cmath>
using namespace std;
class Area
{
private:
double area;
public:
Area(double a)
{
area = a * a;
}
Area(double a, double b)
{
area = a * b;
}
Area(double a, double b, double c)
{
double p = (a + b + c) / 2;
area = sqrt(p * (p - a) * (p - b) * (p - c));
}
~Area() {}
double GetArea()
{
return area;
}
};
int main()
{
double sA, rA, rB, tA, tB, tC;
cout << "Enter the side of the square: ";
cin >> sA;
cout << "Enter the width of the rectangle: ";
cin >> rA;
cout << "Enter the height of the rectangle: ";
cin >> rB;
cout << "Enter the 1st side of the triangle: ";
cin >> tA;
cout << "Enter the 2nd side of the triangle: ";
cin >> tB;
cout << "Enter the 3rd side of the triangle: ";
cin >> tC;
cout << "The area of the square is " << Area(sA).GetArea();
cout << "\nThe area of the rectangle is " << Area(rA, rB).GetArea();
cout << "\nThe area of the triangle is " << Area(tA, tB, tC).GetArea();
return 0;
}
Comments
Leave a comment