Item 1-Constructors for Triangle classes-(2)

Source: Internet
Author: User

"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)

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.