(2)设计默认构造函数,即不指定参数时,默认各边长为1。需要的测试函数是:
int main()
{
Triangle Tri; //调用默认构造函数,不指定参数时,默认各边长为1;
Tri.showMessage();
return 0;
}
#include <iostream> #include <cmath> using namespace std; class Triangle { public: double perimeter(); double area(); void showMessage(); Triangle() { a=1; b=1; c=1; } private: double a,b,c; }; void Triangle::showMessage() { cout<<"三角形的三边长分别为:"<<a<<' '<<b<<' '<<c<<endl; cout<<"该三角形的周长为"<<perimeter()<<",面积为:"<<area()<<endl<<endl; } double Triangle::perimeter() { return a+b+c; } double Triangle::area() { double p,s; p=(a+b+c)/2; s=sqrt(p*(p-a)*(p-b)*(p-c)); return s; } int main() { Triangle Tri; Tri.showMessage(); return 0; }运行结果:
原文:http://blog.csdn.net/zs9528/article/details/44812191