Project 1-Constructor

Source: Internet
Author: User

Print? /*
* Copyright and version Declaration of the program
* Copyright (c) 2012, a student from the computer College of Yantai University
* All rightsreserved.
* File name: object. cpp
* Author: Yang shaoning
* Completion date: January 1, March 28, 2013
* Version: v1.0
* Input Description: None
* Problem description: design a class that calculates the circumference and area of a triangle.
* Program output: the circumference and area of a triangle
*/
# 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 (void); // calculates the perimeter of a triangle.
Double area (void); // calculates and returns the area of the triangle.
Void showMessage (void );
Private:
Double a, B, c; // the three sides are private member data.
};
Double Triangle: perimeter (void)
{
Double l;
L = a + B + c;
Return l;
}
Double Triangle: area (void)
{
Double s, p;
P = (a + B + c)/2;
S = sqrt (p * (p-a) * (p-B) * (p-c ));
Return s;
}
Void Triangle: showMessage ()
{
Cout <"three sides of the triangle are:" <a <'\ t' <B <' \ t' <c <endl;
Cout <"the triangle perimeter is:" <perimeter () <'\ t' <"area:" <area () <endl; // set the initial value for the three sides
}
Int main ()
{
Triangle tri1; // defines an instance (object) of the Triangle class)
Tri1.showMessage ();
Triangle tri2 (, 9); // defines an instance of the Triangle class (object)
Tri2.showMessage ();
Return 0;
}

/*
* Copyright and version Declaration of the program
* Copyright (c) 2012, a student from the computer College of Yantai University
* All rightsreserved.
* File name: object. cpp
* Author: Yang shaoning
* Completion date: January 1, March 28, 2013
* Version: v1.0
* Input Description: None
* Problem description: design a class that calculates the circumference and area of a triangle.
* Program output: the circumference and area of a triangle
*/
# 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 (void); // calculates the perimeter of a triangle.
Double area (void); // calculates and returns the area of the triangle.
Void showMessage (void );
Private:
Double a, B, c; // the three sides are private member data.
};
Double Triangle: perimeter (void)
{
Double l;
L = a + B + c;
Return l;
}
Double Triangle: area (void)
{
Double s, p;
P = (a + B + c)/2;
S = sqrt (p * (p-a) * (p-B) * (p-c ));
Return s;
}
Void Triangle: showMessage ()
{
Cout <"three sides of the triangle are:" <a <'\ t' <B <' \ t' <c <endl;
Cout <"the triangle perimeter is:" <perimeter () <'\ t' <"area:" <area () <endl; // set the initial value for the three sides
}
Int main ()
{
Triangle tri1; // defines an instance (object) of the Triangle class)
Tri1.showMessage ();
Triangle tri2 (, 9); // defines an instance of the Triangle class (object)
Tri2.showMessage ();
Return 0;
}
Feel: If you want to add tri1 .. If you call a member function, no more !! 1, right?

 

Related Article

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.