Project 1-triangle Constructor (2), 1-Constructor

Source: Internet
Author: User

Project 1-triangle Constructor (2), 1-Constructor

Project 1-triangle Constructor]
Design a triangle class. By adding constructor, You can initialize the object during definition. You can start with the following class declaration. You need to implement relevant member functions and add required constructor functions.

Class Triangle {public: double perimeter (); // calculate the Triangle perimeter double area (); // calculate and return the Triangle area void showMessage (); private: double, b, c; // the three sides are private member data}; void Triangle: showMessage () {cout <"the three sides of the Triangle are respectively: "<a <'' <B <''<c <endl; cout <" the circumference of the triangle is "<perimeter () <", area:" <area () <endl ;}

 

(2) design the default constructor. If no parameter is specified, the length of each side is 1 by default. The required test function is:

Int main ()

{

Triangle Tri; // call the default constructor. If no parameter is specified, the length of each side is 1 by default;

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 (); private: double a, B, c;}; double Triangle: perimeter () {return (a + B + c);} double Triangle: area () {double d = (a + B + c)/2; return sqrt (d * (d-a) * (d-B) * (d-c);} void Triangle: showMessage () {cout <"the three sides of the triangle are:" <a <''<B <'' <c <endl; cout <"the circumference of the triangle is" <perimeter () <", and the area is:" <area () <endl;} int main () {Triangle Tri2; Tri2.showMessage (); return 0 ;}


 

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.