Week four 13 constructors for angular classes-4

Source: Internet
Author: User

(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) {};
Private
Double a,b,c;


};
void Triangle::showmessage ()
{
cout<< "Triangular three-side lengths are:" <<a<< "<<b<<" <<c<<endl;
cout<< "The circumference of the triangle is" <<perimeter () << ", the area is:" <<area () <<endl<<endl;
}
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;
}
int main ()
{
Triangle Tri1 (1,1,1);
Tri1.showmessage ();
Triangle Tri2 (1.5,1,1);
Tri2.showmessage ();
Triangle Tri3 (1.5,1.5,1);
Tri3.showmessage ();
Triangle Tri4 (7,8,9);
Tri4.showmessage ();
return 0;
}

Operation Result:


Week four 13 constructors for angular classes-4

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.