Week five "project-triangular prototypes 2, 3, 4, 5"

Source: Internet
Author: User
Tags cmath

2) on the basis of the above program, redefine the Triangle class, where the logic is particularly simple set and get member functions, to be processed as inline member functions, defined directly within the class.

<code class= "Hljs cpp has-numbering" style= "display:block; padding:0px; Color:inherit; Box-sizing:border-box; font-family: ' Source Code Pro ', monospace;font-size:undefined; White-space:pre; border-radius:0px; Word-wrap:normal; background:transparent; " ><span class= "Hljs-keyword" style= "Color:rgb (0, 0, 136); Box-sizing:border-box; "  >int</span> Main () {Triangle tri1; <span class= "hljs-comment" style= "Color:rgb (136, 0, 0); Box-sizing:border-box; " >//defines an instance of the Triangle Class (object) </span> <span class= "Hljs-keyword" style= "Color:rgb (0, 0, 136); Box-sizing:border-box; "      >double</span> x, y, Z; <span class= "hljs-built_in" style= "Color:rgb (102, 0, 102); Box-sizing:border-box; " >cout</span><<<span class= "hljs-string" style= "Color:rgb (0, 136, 0); Box-sizing:border-box; "      > "Please enter the three sides of the triangle:" </span>; <span class= "hljs-built_in" style= "Color:rgb (102, 0, 102); Box-sizing:border-box; " >cin</span>>>x>>y>>z;      Tri1.seta (x);      Tri1.setb (y);   Tri1.setc (z); <span class= "hljs-comment" style= "Color:rgb (136, 0, 0); Box-sizing:border-box; " >//is a three-side initial value </span> <span class= "Hljs-keyword" style= "Color:rgb (0, 0, 136); Box-sizing:border-box; " >if</span> (Tri1.istriangle ()) <span class= "hljs-comment" style= "Color:rgb (136, 0, 0); Box-sizing:border-box; " The >//istriangle () return value is Boolean and is used to determine whether the given three-side can form a triangle </span> {<span class= "hljs-built_in" style= "Color:rgb ( 102, 0, 102); Box-sizing:border-box; " >cout</span><<<span class= "hljs-string" style= "Color:rgb (0, 136, 0); Box-sizing:border-box; " > "Three edges for:" </span><<tri1.geta () <<<span class= "hljs-string" style= "Color:rgb (0, 136, 0); Box-sizing:border-box; " > ', ' </span><<tri1.getb () <<<span class= "hljs-string" style= "Color:rgb (0, 136, 0); Box-sizing:border-box; "          > ', ' </span><<tri1.getc () <<endl; &ltSpan class= "hljs-built_in" style= "Color:rgb (102, 0, 102); Box-sizing:border-box; " >cout</span><<<span class= "hljs-string" style= "Color:rgb (0, 136, 0); Box-sizing:border-box; " > "The perimeter of the triangle is:" </span><< tri1.perimeter () <<<span class= "hljs-string" style= "Color:rgb (0, 136, 0 ); Box-sizing:border-box; " > ' t ' </span><<<span class= "hljs-string" style= "Color:rgb (0, 136, 0); Box-sizing:border-box; "      > "Area:" </span><< tri1.area () <<endl; } <span class= "Hljs-keyword" style= "Color:rgb (0, 0, 136); Box-sizing:border-box; " >else</span> <span class= "hljs-built_in" style= "Color:rgb (102, 0, 102); Box-sizing:border-box; " >cout</span><<<span class= "hljs-string" style= "Color:rgb (0, 136, 0); Box-sizing:border-box; "      > "Cannot form a triangle" </span><<endl; <span class= "Hljs-keyword" style= "Color:rgb (0, 0, 136); Box-sizing:border-box; " >return</span> <span Class= "Hljs-number" style= "Color:rgb (0, 102, 102); Box-sizing:border-box; "  >0</span>; </code><p><code class= "Hljs cpp has-numbering" style= "display:block; padding:0px; Color:inherit; Box-sizing:border-box; font-family: ' Source Code Pro ', monospace;font-size:undefined; White-space:pre; border-radius:0px; Word-wrap:normal; background:transparent; " } </code><code class= "Hljs cpp has-numbering" style= "display:block; padding:0px; Color:inherit; Box-sizing:border-box; font-family: ' Source Code Pro ', monospace;font-size:undefined; White-space:pre; border-radius:0px; Word-wrap:normal; background:transparent; " ></code><code class= "Hljs cpp has-numbering" style= "display:block; padding:0px; Color:inherit; Box-sizing:border-box; font-family: ' Source Code Pro ', monospace;font-size:undefined; White-space:pre; border-radius:0px; Word-wrap:normal; background:transparent; " ></code><pre name= "code" class= "CPP" > #include &LT;iostream> #include <cmath>using namespace Std;class triangle{public:void SetA (double x) {a=x;    };    void Setb (double y) {b=y;    };    void SetC (double z) {c=z;    };    Double Geta () {return A;    } double Getb () {return b;    } double GetC () {return C; } bool Istriangle ();//Determines whether the triangle is composed of double perimeter (void);//computes the perimeter of the Triangle double area (void);//calculates and returns the size of the triangle Private:doub Le A,b,c;  The three sides are private member data};int main () {Triangle tri1;    Defines an instance (object) of a triangle class double X, Y, Z;    cout<< "Please enter three sides of the triangle:";    cin>>x>>y>>z;    Tri1.seta (x);    Tri1.setb (y);   Tri1.setc (z); For the three-edged initial value if (Tri1.istriangle ()) {cout<< "Three edges are:" <<tri1.geta () << ', ' <<tri1.getb () <<        ', ' <<tri1.getc () <<endl;    cout<< "The perimeter of the triangle is:" << tri1.perimeter () << ' \ t ' << "area:" << tri1.area () <<endl; } else cout<< "cannot form a triangle" <<endl;    return 0;} Define the individual member functions in the Triangle class below, bool Triangle::istriangle () {return (a+b>c&&a+c>b&&b+c>a);} Double Triangle::p erimeter () {return a+b+c;}    Double Triangle::area () {double p= (a+b+c)/2; return sqrt (p* (p-a) * (p-b) * (p-c));






3) Design The constructor of the Triangle class with parameters, namely triangle (double x, double y, double z), and the three-side length is given directly by the argument at invocation, so that the object can be initialized at the time of definition. The required test functions are:

<code class= "HLJS cs has-numbering" style= "display:block; padding:0px; Color:inherit; Box-sizing:border-box; font-family: ' Source Code Pro ', monospace;font-size:undefined; White-space:pre; border-radius:0px; Word-wrap:normal; background:transparent; " ><span class= "Hljs-keyword" style= "Color:rgb (0, 0, 136); Box-sizing:border-box; " >int</span> Main () {Triangle Tri (<span class= "Hljs-number" style= "Color:rgb (0, 102, 102); Box-sizing:bord Er-box; " >7</span>,<span class= "Hljs-number" style= "Color:rgb (0, 102, 102); Box-sizing:border-box; " >8</span>,<span class= "Hljs-number" style= "Color:rgb (0, 102, 102); Box-sizing:border-box; "    >9</span>); <span class= "hljs-comment" style= "Color:rgb (136, 0, 0); Box-sizing:border-box; " >//defines an instance (object) of a triangle class </span> tri.showmessage (); <span class= "hljs-comment" style= "Color:rgb (136, 0, 0); Box-sizing:border-box; " >//defines a member function that can display triangle information </span> <span class= "Hljs-keyWord "style=" Color:rgb (0, 0, 136); Box-sizing:border-box; " >return</span> <span class= "Hljs-number" style= "Color:rgb (0, 102, 102); Box-sizing:border-box; " >0</span>;} </code>
#include <iostream> #include <cmath>using namespace Std;class triangle{public:    Triangle (Double x, Double y, double z)//(1) using the parameter constructor    {        a=x;        b=y;        c=z;    }    Double perimeter ();//Calculate the perimeter of the Triangle    double area ();//calculate and return the areas of the triangle    void ShowMessage ();p rivate:    double a,b,c; The three sides are private member data};d ouble Triangle::p erimeter () {    return (A + B + c);} Double Triangle::area () {    double s = (A + B + c)/2;    return sqrt (S * (s-a) * (s-b) * (S-C));} The three-edged lengths of void Triangle::showmessage () {    cout<< "triangles are:" <<a<< "<<b<<" <<c< <endl;    cout<< "The circumference of the triangle is" <<perimeter () << "with an area of" <<area () <<endl<<endl;} int main () {    Triangle Tri (7,8,9);    Defines an instance (object) of a triangle class    tri.showmessage ();    return 0;}



(4) using a constructor with default parameters, the default side length is 1 when no argument is given; note-only one constructor is required. The required test functions are:

<code class= "Hljs scss has-numbering" style= "display:block; padding:0px; Color:inherit; Box-sizing:border-box; font-family: ' Source Code Pro ', monospace;font-size:undefined; White-space:pre; border-radius:0px; Word-wrap:normal; background:transparent; " >int <span class= "hljs-function" style= "Box-sizing:border-box;"   >main () </span>{Triangle Tri1; <span class= "hljs-comment" style= "Color:rgb (136, 0, 0); Box-sizing:border-box; " >//defines a triangle class instance with a side length of 1 1 1 </span> tri1<span class= "Hljs-class" style= "Box-sizing:border-box;"    >.showMessage</span> (); Triangle <span class= "hljs-function" style= "Box-sizing:border-box;" >tri2 (<span class= "Hljs-number" style= "Color:rgb (0, 102, 102); box-sizing:border-box;" >1.5</span>) </span>;<span class= "hljs-comment" style= "Color:rgb (136, 0, 0); Box-sizing:border-box; " >//defines a triangle class instance with a side length of 1.5 1 1 </span> tri2<span class= "Hljs-class" style= "Box-sizing:border-box;" >.showmEssage</span> (); Triangle <span class= "hljs-function" style= "Box-sizing:border-box;" >tri3 (<span class= "Hljs-number" style= "Color:rgb (0, 102, 102); box-sizing:border-box;" >1.5</span>,<span class= "Hljs-number" style= "Color:rgb (0, 102, 102); Box-sizing:border-box; " >1.5</span>) </span>;<span class= "hljs-comment" style= "Color:rgb (136, 0, 0); Box-sizing:border-box; " >//defines a triangle class instance with a side length of 1.5 1.5 1 </span> tri3<span class= "Hljs-class" style= "Box-sizing:border-box;"    >.showMessage</span> (); Triangle <span class= "hljs-function" style= "Box-sizing:border-box;" >tri4 (<span class= "Hljs-number" style= "Color:rgb (0, 102, 102); box-sizing:border-box;" >7</span>,<span class= "Hljs-number" style= "Color:rgb (0, 102, 102); Box-sizing:border-box; " >8</span>,<span class= "Hljs-number" style= "Color:rgb (0, 102, 102); Box-sizing:border-box; " >9</span>) </span>; <span class= "Hljs-comment" StylE= "Color:rgb (136, 0, 0); Box-sizing:border-box; " >//defines a triangle class instance with a side length of 7 8 9 </span> tri4<span class= "Hljs-class" style= "Box-sizing:border-box;"    >.showMessage</span> (); return 0;} </code>
#include <iostream> #include <cmath>using namespace Std;class triangle{public:triangle (double x=1, double y    =1, double z=1); Double perimeter ();//Calculate the perimeter of the Triangle double area ();//calculate and return the areas of the triangle void ShowMessage ();p rivate:double a,b,c; };    Triangle::triangle (double x, double y, double z) {a=x;    B=y; C=z;} Double Triangle::p erimeter () {return (A + B + c);}    Double Triangle::area () {Double S = (A + B + c)/2; return sqrt (S * (s-a) * (s-b) * (S-C));} The three-edged lengths of void Triangle::showmessage () {cout<< "triangles are:" <<a<< "<<b<<" <<c<<    Endl cout<< "The circumference of the triangle is" <<perimeter () << "with an area of" <<area () <<endl<<endl;}   int main () {Triangle Tri1;    Defines a triangle class instance tri1.showmessage () with an edge length of 1 1 1;    Triangle Tri2 (1.5);//defines a triangle class instance tri2.showmessage () with 1.5 1 1 edge length;    Triangle Tri3 (1.5,1.5);//define a triangle class instance tri3.showmessage () with a side length of 1.5 1.5 1; Triangle Tri4 (7,8,9);    Defines a triangle class instance tri4.showmessage () with an edge length of 7 8 9; return 0;}



(5) Using the copy constructor, you can copy a new Triangle object with an existing Triangle object:

<code class= "Hljs scss has-numbering" style= "display:block; padding:0px; Color:inherit; Box-sizing:border-box; font-family: ' Source Code Pro ', monospace;font-size:undefined; White-space:pre; border-radius:0px; Word-wrap:normal; background:transparent; " >int <span class= "hljs-function" style= "Box-sizing:border-box;" >main () </span>{Triangle <span class= "hljs-function" style= "Box-sizing:border-box;" >tri1 (<span class= "Hljs-number" style= "Color:rgb (0, 102, 102); box-sizing:border-box;" >3</span>,<span class= "Hljs-number" style= "Color:rgb (0, 102, 102); Box-sizing:border-box; " >4</span>,<span class= "Hljs-number" style= "Color:rgb (0, 102, 102); Box-sizing:border-box; "   >5</span>) </span>; <span class= "hljs-comment" style= "Color:rgb (136, 0, 0); Box-sizing:border-box; " >//defines a triangle class instance with a side length of 1 1 1 </span> tri1<span class= "Hljs-class" style= "Box-sizing:border-box;"    >.showMessage</span> (); TrianglE <span class= "hljs-function" style= "Box-sizing:border-box;" >tri2 (TRI1) </span>; <span class= "hljs-comment" style= "Color:rgb (136, 0, 0); Box-sizing:border-box; " >//Call copy constructor </span> Tri2<span class= "Hljs-class" style= "Box-sizing:border-box;"    >.showMessage</span> (); return 0;} </code>
#include <iostream> #include <cmath>using namespace Std;class triangle{public:triangle (double x=1, double y    =1, double z=1);    Triangle (const Triangle &t);  Double perimeter ();   Computes the perimeter of the Triangle double area (); Calculates the area of the return triangle void ShowMessage ();p rivate:double a,b,c;};    Triangle::triangle (double x, double y, double z) {a=x;    B=y; C=z;}    Triangle::triangle (const Triangle &t) {a=t.a;    b=t.b; c=t.c;} Double Triangle::p erimeter () {return (A + B + c);}    Double Triangle::area () {Double S = (A + B + c)/2; return sqrt (S * (s-a) * (s-b) * (S-C));} The three-edged lengths of void Triangle::showmessage () {cout<< "triangles are:" <<a<< "<<b<<" <<c<<    Endl cout<< "The circumference of the triangle is" <<perimeter () << "with an area of" <<area () <<endl<<endl;}   int main () {Triangle Tri1 (3,4,5);    Defines a triangle class instance tri1.showmessage () with an edge length of 1 1 1; Triangle Tri2 (TRI1);    Call the copy constructor Tri2.showmessage (); return 0;}


Week five "project-triangular prototypes 2, 3, 4, 5"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.