Question: In C++ Design a class named Triangle that extends GeometricObject. Please add the Triangle class definition in the file Triangle.h and add the implementation in
In C++
Design a class named Triangle that extends GeometricObject. Please add the Triangle class definition in the file Triangle.h and add the implementation in the file Triangle.cpp. The class contains the following:
Three double data fields named side1, side2, and side3 to denote three sides of the triangle.
A default constructor that creates a default triangle with each side 1.0.
A constructor that creates a triangle with the specified side1, side2, and side3.
A constructor that creates a triangle with the specified side1, side2, and side3, color, and filled.
The accessor functions for all three data fields.
A function named get_perimeter() that returns the perimeter of this triangle.
Implement the class Triangle. Write a test program that
prompts the user to enter three sides of the triangle, enter a color, and enter 1 or 0 to indicate whether the triangle is filled.
creates a Triangle object with these sides and set the color and filled properties using the input.
displays the perimeter, color, and true or false to indicate whether filled or not.
The expected result:
Please enter the three sides: 6 6 6 Please enter the color: blue Is the triangle filled (1: filled, 0: not filled): 1 The perimeter of the triangle is: 18 The color of the triangle is: blue Is the triangle filled? true
// Triangle.h // #ifndef TRIANGLE_H #define TRIANGLE_H #includeusing namespace std; class GeometricObject { public: GeometricObject(); GeometricObject(const string& color, bool filled); string get_color() const; void set_color(const string& color); bool is_filled() const; void set_filled(bool filled); string to_string() const; private: string color; bool filled; }; // Must place semicolon here // add Triangle class definition #endif
// // Triangle.cpp // #include "Triangle.h" GeometricObject::GeometricObject() { color = "white"; filled = false; } GeometricObject::GeometricObject(const string& color, bool filled) { this->color = color; this->filled = filled; } string GeometricObject::get_color() const { return color; } void GeometricObject::set_color(const string& color) { this->color = color; } bool GeometricObject::is_filled() const { return filled; } void GeometricObject::set_filled(bool filled) { this->filled = filled; } string GeometricObject::to_string() const { string f = (filled ? "Filled " : "Not filled "); return f + color + " Geometric Object."; } // add Triangle class implementation Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
