Answer to Question #220715 in C++ for Mudassar

Question #220715
Writes a class Triangle with the data member base , height , area and color . The base , height and area are double type and color is type c - string ( a c - string is defined as an array of type char ) . The member functions of the class is inputData for taking base , height and color values from the user and findArea member function to calculate the area of the rectangular using formula area = 1 / 2 * base * height . Create another class name Match Triangle that overloads operator for comparing class objects . Define appropriate constructors of the classes . Create two objects of in main ( ) , inputs the values and compare the objects . If the area and color of both objects are same then display a message in main Matching Triangle " otherwise display a message Non - Matching Triangle "
1
Expert's answer
2021-07-27T07:43:51-0400
#include <iostream>

using namespace std;

class Triangle
{
private:
    double base, height, area;
    char color[20];
public:
    Triangle() {}

    void inputData() {
        cout << "Input base:";
        cin >> base;
        cout << "Input height:";
        cin >> height;
        cout << "Input color:";
        cin >> color;

    }
    void findArea() {
        area = 0.5*base*height;
        cout << "The area of the triangle:" << area << '\n';
    }

    double getArea() const {
        return area;
    }

    const char *getColor() const {
        return color;
    }
};

class Match_Triangle
{
    Triangle t1, t2;
public:
    Match_Triangle(const Triangle &t1, const Triangle &t2) : t1(t1), t2(t2) {}
    void isMatch() {
        string s1 = t1.getColor(), s2 = t2.getColor();
        if ((t1.getArea() == t2.getArea()) && (s1 == s2)) {
            cout << "Matching Triangle\n";
        } else {
            cout << "Non-Matching Triangle\n";
        }
    }
};

int main() {
    Triangle t1, t2;
    t1.inputData();
    t2.inputData();
    t1.findArea();
    t2.findArea();
    Match_Triangle tr = Match_Triangle(t1, t2);
    tr.isMatch();

    return 0;
}

Output:
Input base:
4
Input height:
5
Input color:
white
Input base:
4
Input height:
5
Input color:
white
The area of the triangle:10
The area of the triangle:10
Matching Triangle

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