Project 1-triangle Constructor (2), 1-Constructor
Project 1-triangle Constructor]
Design a triangle class. By adding constructor, You can initialize the object during definition. You can start with the following class declaration. You need to implement relevant member functions and add required constructor functions.
Class Triangle {public: double perimeter (); // calculate the Triangle perimeter double area (); // calculate and return the Triangle area void showMessage (); private: double, b, c; // the three sides are private member data}; void Triangle: showMessage () {cout <"the three sides of the Triangle are respectively: "<a <'' <B <''<c <endl; cout <" the circumference of the triangle is "<perimeter () <", area:" <area () <endl ;}
(2) design the default constructor. If no parameter is specified, the length of each side is 1 by default. The required test function is:
Int main ()
{
Triangle Tri; // call the default constructor. If no parameter is specified, the length of each side is 1 by default;
Tri. showMessage ();
Return 0;
}
# Include <iostream> # include <Cmath> using namespace std; class Triangle {public: Triangle () {a = 1; B = 1; c = 1 ;} double perimeter (); double area (); void showMessage (); private: double a, B, c;}; double Triangle: perimeter () {return (a + B + c);} double Triangle: area () {double d = (a + B + c)/2; return sqrt (d * (d-a) * (d-B) * (d-c);} void Triangle: showMessage () {cout <"the three sides of the triangle are:" <a <''<B <'' <c <endl; cout <"the circumference of the triangle is" <perimeter () <", and the area is:" <area () <endl;} int main () {Triangle Tri2; Tri2.showMessage (); return 0 ;}