#include <iostream> #include <cmath> using namespace std; class Triangle {public: double perimeter(void);//计算三角形的周长 double area(void);//计算并返回三角形的面积 void showMessage(); Triangle() { a = 1; b = 1; c = 1; } Triangle (double x ,double y,double z ):a(x),b(y),c(z){} double a,b,c; //三边为私有成员数据 }; void Triangle::showMessage() { cout<<"三角形的三边长分别为:"<<a<<'\t'<<b<<'\t'<<c<<endl; cout<<"该三角形的周长为:"<< perimeter()<<'\t'<<"面积为:"<< area()<<endl<<endl; } void main(void) { Triangle Tri1; //定义三角形类的一个实例(对象) Tri1.showMessage(); Triangle Tri2(7,8,9); //定义三角形类的一个实例(对象) Tri2 .showMessage(); system(" pause"); } double Triangle::perimeter() { return a + b + c; } double Triangle::area() { double ar,p; p = 0.5 * (a + b + c); ar = pow( (p - a)*(p - b)*(p - c) * p,0.5 ); return ar; }