"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;}
(3) using a constructor with default parameters, the default side length is 1 when no arguments are given, note-This version requires only one constructor. The required test functions are:
int main () { Triangle Tri (7,8,9);//define an instance (object) of the Triangle Class tri.showmessage (); return 0;}
#include <iostream> #include <cmath>using namespace Std;class triangle{public: Triangle (Double x=1, Double Y=1, double z=1) { a=x; b=y; c=z; } 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 Tri3; Tri3.showmessage (); Triangle Tri4 (1.5); Tri4.showmessage (); Triangle Tri5 (1.5,1.5); Tri5.showmessage (); Triangle Tri6 (7,8,9); Tri6.showmessage (); return 0;}
Item 1-Constructors for Triangle classes-(2)