- //默认构造函数
- #include<iostream>
- #include<cmath>
- using namespace std;
- class Triangle
- {
- public:
- Triangle(float x = 1, float y = 1, float z = 1);
- // Triangle(float x, float y, float z);
- float perimeter(void);//计算三角形的周长
- float area(void);//计算并返回三角形的面积
- void showMessage();
- private:
- float a,b,c; //三边为私有成员数据
- };
- void Triangle::showMessage()
- {
- cout<<"三角形的三边长分别为:"<< a << " " << b << " " << c << endl;
- if((a + b > c && a + c > b && b + c > a) && (a - b < c && a - c < b && b - c < a))
- {
- cout<<"该三角形的周长为:"<< perimeter()<<'\t'<<"面积为:"<< area()<<endl;
- }
- else
- {
- cout<< "三边无法构成三角形!!!" << endl;
- }
- }
- float Triangle::perimeter(void)
- {
- float d;
- d = a + b + c;
- return d;
- }
- float Triangle::area(void)
- {
- float p = (a + b + c) / 2;
- float m = sqrt(p*(p-a)*(p-b)*(p-c));
- return m;
- }
- Triangle::Triangle(float x, float y, float z)
- {
- a = x;
- b = y;
- c = z;
- }
- void main(void)
- {
- Triangle Tri1; //定义三角形类的一个实例(对象)
- Tri1.showMessage();
- Triangle Tri2(7,8,9); //定义三角形类的一个实例(对象)
- Tri2.showMessage();
- Triangle Tri3(2);
- Tri3.showMessage();
- }
- //带参数的构造函数
- #include<iostream>
- #include<cmath>
- using namespace std;
- class Triangle
- {
- public:
- /* Triangle()
- {
- a = 1;
- b = 1;
- c = 1;
- }
- */
- Triangle(float x, float y, float z);
- float perimeter(void);//计算三角形的周长
- float area(void);//计算并返回三角形的面积
- void showMessage();
- private:
- float a,b,c; //三边为私有成员数据
- };
- void Triangle::showMessage()
- {
- cout<<"三角形的三边长分别为:"<< a << " " << b << " " << c << endl;
- if((a + b > c && a + c > b && b + c > a) && (a - b < c && a - c < b && b - c < a))
- {
- cout<<"该三角形的周长为:"<< perimeter()<<'\t'<<"面积为:"<< area()<<endl;
- }
- else
- {
- cout<< "三边无法构成三角形!!!" << endl;
- }
- }
- float Triangle::perimeter(void)
- {
- float d;
- d = a + b + c;
- return d;
- }
- float Triangle::area(void)
- {
- float p = (a + b + c) / 2;
- float m = sqrt(p*(p-a)*(p-b)*(p-c));
- return m;
- }
- Triangle::Triangle(float x, float y, float z)
- {
- a = x;
- b = y;
- c = z;
- }
- void main(void)
- {
- // Triangle Tri1; //定义三角形类的一个实例(对象)
- // Tri1.showMessage();
- Triangle Tri2(7,8,9); //定义三角形类的一个实例(对象)
- Tri2.showMessage();
- }
- //使用参数初始化表对成员函数初始化
- #include<iostream>
- #include<cmath>
- using namespace std;
- class Triangle
- {
- public:
- Triangle()
- {
- a = 1;
- b = 1;
- c = 1;
- }
- Triangle(float x, float y, float z):a(x), b(y), c(z){}
- float perimeter(void);//计算三角形的周长
- float area(void);//计算并返回三角形的面积
- void showMessage();
- private:
- float a,b,c; //三边为私有成员数据
- };
- void Triangle::showMessage()
- {
- cout<<"三角形的三边长分别为:"<< a << " " << b << " " << c << endl;
- if((a + b > c && a + c > b && b + c > a) && (a - b < c && a - c < b && b - c < a))
- {
- cout<<"该三角形的周长为:"<< perimeter()<<'\t'<<"面积为:"<< area()<<endl;
- }
- else
- {
- cout<< "三边无法构成三角形!!!" << endl;
- }
- }
- float Triangle::perimeter(void)
- {
- float d;
- d = a + b + c;
- return d;
- }
- float Triangle::area(void)
- {
- float p = (a + b + c) / 2;
- float m = sqrt(p*(p-a)*(p-b)*(p-c));
- return m;
- }
- /*Triangle::Triangle(float x, float y, float z)
- {
- a = x;
- b = y;
- c = z;
- }*/
- void main(void)
- {
- Triangle Tri1; //定义三角形类的一个实例(对象)
- Tri1.showMessage();
- Triangle Tri2(7,8,9); //定义三角形类的一个实例(对象)
- Tri2.showMessage();
- }