"Project 1-Constructors for triangle classes"
The Triangle class is designed by adding constructors that enable the object to be initialized at definition, starting with the following class declaration, implementing its own related member functions, and adding the required constructors
Class Triangle{public: double perimeter ();//Calculates the perimeter of a triangle double area ();//calculates and returns the size of the triangle, void showmessage (); Private: double a,b,c;//three sides for private member Data};void Triangle::showmessage () { cout<< "triangle for three sides:" <<a< < ' <<b<< ' <<c<<endl; cout<< "The circumference of the triangle is" <<perimeter () << ", the area is:" <<area () <<endl<<endl;}
(2) Design The default constructor, that is, when the parameter is not specified, the default side length is 1. The required test functions are:
int main ()
{
Triangle Tri; //Call the default constructor, when no parameters are specified, the default side length is 1;
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 ();p rivate: double a,b,c;}; Double Triangle::p erimeter () { return (a+b+c);} Double Triangle::area () { double d= (a+b+c)/2; return sqrt (d* (d-a) * (d-b) * (d-c)); The three-edged lengths of void Triangle::showmessage () { cout<< "triangles are:" <<a<< "<<b<<" <<c< <endl; cout<< "The circumference of the triangle is" <<perimeter () << ", the area is:" <<area () <<endl<<endl;} int main () { Triangle Tri2; Tri2.showmessage (); return 0;}
Item 1-Constructors for Triangle classes (2)