第五周项目1.4

#include <iostream>
#include<cmath>
using namespace std;
class Triangle
{
public:
    Triangle(double x=1.0,double y=1.0,double z=1.0);//构造函数
    double perimeter();//计算三角形的周长
    double area();//计算并返回三角形的面积
    void showMessage();
private:
    double a,b,c;//三边为私有成员数据
};
Triangle::Triangle(double x,double y,double z)
{
    a=x;
    b=y;
    c=z;
}
void Triangle::showMessage()
{
    cout<<"三角形的周长为:"<<perimeter()<<'\t'<<"面积为:"<<area()<<endl;
}
int main()
{
    Triangle Tri1;
    Tri1.showMessage();
    Triangle Tri2(1.5);
    Tri2.showMessage();
    Triangle Tri3(1.5,1.5);
    Tri3.showMessage();
    Triangle Tri4(7,8,9);
    Tri4.showMessage();
    return 0;
}
double Triangle::perimeter()
{
    return a+b+c;;
}
double Triangle::area()
{
    double x,y;
    x=(a*a+b*b-c*c)/(2*a*b);
    y=sqrt(1-x*x);
    return 0.5*a*b*y;
}


第五周项目1.4_第1张图片

你可能感兴趣的:(C++)