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;}
This project will include multiple versions, before starting the program, make sure to figure out what is a constructor with parameters, a default constructor, a constructor with default arguments, and a parameter initialization table.
/** Copyright (c) 2015, Yantai University School of Computer * All right reserved.* Shao * file: demo.cpp* finish: April 01, 2015 * Version number: v1.0*/
(1) using a parametric constructor, which is triangle (double x, double y, double z), the three-edged length is given directly by the argument at the time of invocation. The required test functions are:
#include <iostream> #include <cmath>using namespace Std;class triangle{public: double perimeter (); Double area (); void ShowMessage (); Triangle (double x,double y,double z) { a=x; b=y; c=z; } Private: double a,b,c;}; Double Triangle::p erimeter () { return a+b+c;}; Double Triangle::area () { double p,s; p= (A+B+C)/2; S=sqrt (p* (p-a) * (p-b) * (p-c)); return s;}; void Triangle::showmessage () { cout<< "triangle has a length of three sides:" <<a<< "<<b<<" <<c< <endl; cout<< "The circumference of the triangle is:" <<perimeter () << ", Area:" <<area () <<endl;} int main () { Triangle Tri (7,8,9); Tri.showmessage (); return 0;}
(2) Design The default constructor, that is, when the parameter is not specified, the default side length is 1. The required test functions are:
#include <iostream> #include <cmath>using namespace Std;class triangle{public: double perimeter (); Double area (); void ShowMessage (); Triangle () { a=1; b=1; c=1; } Private: double a,b,c;}; Double Triangle::p erimeter () { return a+b+c;}; Double Triangle::area () { double p,s; p= (A+B+C)/2; S=sqrt (p* (p-a) * (p-b) * (p-c)); return s;}; void Triangle::showmessage () { cout<< "triangle has a length of three sides:" <<a<< "<<b<<" <<c< <endl; cout<< "The circumference of the triangle is:" <<perimeter () << ", Area:" <<area () <<endl;} int main () { Triangle Tri; Tri.showmessage (); return 0;}
(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:
#include <iostream> #include <cmath>using namespace Std;class triangle{public: double perimeter (); Double area (); void ShowMessage (); Triangle (double x=1,double y=1,double z=1) { a=x; b=y; c=z; } Private: double a,b,c;}; Double Triangle::p erimeter () { return a+b+c;}; Double Triangle::area () { double p,s; p= (A+B+C)/2; S=sqrt (p* (p-a) * (p-b) * (p-c)); return s;}; void Triangle::showmessage () { cout<< "triangle has a length of three sides:" <<a<< "<<b<<" <<c< <endl; cout<< "The circumference of the triangle is:" <<perimeter () << ", Area:" <<area () <<endl;} int main () { Triangle Tri1; Tri1.showmessage (); Triangle Tri2 (1.5); Tri2.showmessage (); Triangle Tri3 (1.5,1.5); Tri3.showmessage (); Triangle Tri4 (7,8,9); Tri4.showmessage (); return 0;}
(4) Initializing a data member with a parameter initialization table in a constructor is a more advocated notation. Test the function with the same (1).
#include <iostream> #include <cmath>using namespace Std;class triangle{public: double perimeter (); Double area (); void ShowMessage (); Triangle (double x,double y,double z): A (x), B (Y), C (z) {};p rivate: double a,b,c;}; Double Triangle::p erimeter () { return a+b+c;}; Double Triangle::area () { double p,s; p= (A+B+C)/2; S=sqrt (p* (p-a) * (p-b) * (p-c)); return s;}; void Triangle::showmessage () { cout<< "triangle has a length of three sides:" <<a<< "<<b<<" <<c< <endl; cout<< "The circumference of the triangle is:" <<perimeter () << ", Area:" <<area () <<endl;} int main () { Triangle Tri (7,8,9); Tri.showmessage (); return 0;}
@ Mayuko
Week 1-Constructors for triangle classes