Answer to Question #185665 in C++ for Jagadeeshwaran

Question #185665

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)


1
Expert's answer
2021-04-29T19:01:51-0400
#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;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS