Project 1 default constructor and constructor with Parameters

Source: Internet
Author: User

[Cpp]
/*
* Copyright and version Declaration of the program
* Copyright (c) 2012, a student from the computer College of Yantai University
* All rightsreserved.
* File name: x. cpp
* Author: Xu Benxi
* Completion date: January 1, March 29, 2013
* Version: v1.0
* Input description:
 
* Problem description: default constructor and constructor with Parameters
 
* Program output:
 
*/
// My code:
 
# Include <iostream>
# Include <cmath>
Using namespace std;
Class Triangle
{
Public:
Triangle (double x, double y, double z );
Triangle ();
Double perimeter (void); // calculates the perimeter of a triangle.
Double area (void); // calculates and returns the area of the triangle.
Void showMessage ();
Private:
Bool isTriangle (); // determines whether a triangle is used.
Double a, B, c; // the three sides are private member data.
};
Bool Triangle: isTriangle () // determines whether a Triangle is used.
{
Double num; int I = 0; bool t = true;
Do
{
If (a + B> c & a-B <c)
{
Num = a; a = B; B = c; c = num;
I ++;
}
Else
{
Cout <"three sides cannot form a triangle! ";
T = false;
Break;
}
If (I = 3)
Break;
} While (I <= 3 );
Return t;
}
Double Triangle: perimeter ()
{
Return (a + B + c );
}
Double Triangle: area ()
{
Double z;
Z = (a + B + c)/2;
Return (sqrt (z * (z-a) * (z-B) * (z-c )));
}
Triangle: Triangle ()
{
A = 1; B = 1; c = 1;
}
Triangle: Triangle (double x, double y, double z)
{
A = x; B = y; c = z;
}
Void Triangle: showMessage ()
{
If (isTriangle ())
{
Cout <"the three sides of the triangle are respectively" <a <'\ t' <B <' \ t' <c <endl;
Cout <"the circumference of the triangle is:" <perimeter () <'\ t' <"area:" <area () <endl;

}
Else
Cout <"cannot be a triangle! "<Endl;
}
Void main (void)
{
Triangle Tri1; // defines an instance (object) of the Triangle class. A default constructor is required. The initial values of the three sides are 1.
Tri1.showMessage ();
Triangle Tri2 (, 9); // defines an instance of the Triangle class (object)
Tri2.showMessage ();
}

/*
* Copyright and version Declaration of the program
* Copyright (c) 2012, a student from the computer College of Yantai University
* All rightsreserved.
* File name: x. cpp
* Author: Xu Benxi
* Completion date: January 1, March 29, 2013
* Version: v1.0
* Input description:

* Problem description: default constructor and constructor with Parameters

* Program output:

*/
// My code:

# Include <iostream>
# Include <cmath>
Using namespace std;
Class Triangle
{
Public:
Triangle (double x, double y, double z );
Triangle ();
Double perimeter (void); // calculates the perimeter of a triangle.
Double area (void); // calculates and returns the area of the triangle.
Void showMessage ();
Private:
Bool isTriangle (); // determines whether a triangle is used.
Double a, B, c; // the three sides are private member data.
};
Bool Triangle: isTriangle () // determines whether a Triangle is used.
{
Double num; int I = 0; bool t = true;
Do
{
If (a + B> c & a-B <c)
{
Num = a; a = B; B = c; c = num;
I ++;
}
Else
{
Cout <"three sides cannot form a triangle! ";
T = false;
Break;
}
If (I = 3)
Break;
} While (I <= 3 );
Return t;
}
Double Triangle: perimeter ()
{
Return (a + B + c );
}
Double Triangle: area ()
{
Double z;
Z = (a + B + c)/2;
Return (sqrt (z * (z-a) * (z-B) * (z-c )));
}
Triangle: Triangle ()
{
A = 1; B = 1; c = 1;
}
Triangle: Triangle (double x, double y, double z)
{
A = x; B = y; c = z;
}
Void Triangle: showMessage ()
{
If (isTriangle ())
{
Cout <"the three sides of the triangle are respectively" <a <'\ t' <B <' \ t' <c <endl;
Cout <"the circumference of the triangle is:" <perimeter () <'\ t' <"area:" <area () <endl;

}
Else
Cout <"cannot be a triangle! "<Endl;
}
Void main (void)
{
Triangle Tri1; // defines an instance (object) of the Triangle class. A default constructor is required. The initial values of the three sides are 1.
Tri1.showMessage ();
Triangle Tri2 (, 9); // defines an instance of the Triangle class (object)
Tri2.showMessage ();
}

 

 

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.