#include <iostream>
#include <cmath>
using namespace std;
class Triangle
{
public:
Triangle(float x = 1, float y = 1, float z = 1)
{
a = x, b = y, c = z;
};
float Area(void);
float Perimemter(void);
void showMessage();
private:
float a, b, c;
};
void Triangle::showMessage()
{
cout << "三角形的三边分别为:" << a << '\t' << b << '\t' << c << endl;
cout << "三角形的周长为:" << Perimemter() << '\t' << "demianji:" << Area() << endl;
}
float Triangle::Perimemter(void)
{
float l;
l = a + b + c;
return (l);
}
float Triangle::Area(void)
{
float s, n;
n = (a + b + c) / 2;
s = sqrt((n - a) * (n - b) * (n - c) * n);
return (s);
}
void main(void)
{
Triangle Tri1; //定义三角形类的一个实例(对象)
Tri1.showMessage();
Triangle Tri2(7,8,9); //定义三角形类的一个实例(对象)
Tri2.showMessage();
}
截图: