#include<iostream>
#include<cmath>
using namespace std;
class Triangle
{
public:
double perimeter();//计算三角形的周长
double area();//计算并返回三角形的面积
void showMessage();
Triangle();
private:
double a, b, c; //三边为私有成员数据
};
void Triangle::showMessage()
{
cout << "三角形的三边长分别为:" << a << ‘ ‘ << b << ‘ ‘ << c << endl;
cout << "该三角形的周长为" << perimeter() << ",面积为:" << area() << endl << endl;
}
//(2)设计默认构造函数,即不指定参数时,默认各边长为1
Triangle::Triangle()
{
a = 1;
b = 1;
c = 1;
}
double Triangle::perimeter()
{
return(a + b + c);
}
double Triangle::area()
{
double p = (a + b + c) / 2;
return sqrt((p - a)*(p - b)*(p - c) + p);
}
int main()
{
Triangle Tri; //调用默认构造函数,不指定参数时,默认各边长为1;
Tri.showMessage();
getchar();
return 0;
}
时间: 2024-11-04 13:21:14