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;}

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

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.