/*Univercity:烟台大学
*@Class</A>计134~4
*@Author:薛富磊
*@Time:2014-3-25
*@Function:三角形类的构造函数初始化
设计默认构造函数,即不指定参数时,默认各边长为1
*@Args:
*@Return:
*/
#include <iostream>
#include<Cmath>
using namespace std;
class Triangle
{
public:
Triangle(double x=1,double y=1,double z=1):a(x),b(y),c(z){};
double perimeter();//计算三角形的周长
double area();//计算并返回三角形的面积
void showMessage();
private:
double a,b,c; //三边为私有成员数据
};
double Triangle::perimeter()
{
return a+b+c;
}
double Triangle::area()
{
double q;
q=(a+b+c)/2;
return sqrt(q*(q-a)*(q-b)*(q-c));
}
void Triangle::showMessage()
{
cout<<"三角形的三边长分别为:"<<a<<' '<<b<<' '<<c<<endl;
cout<<"该三角形的周长为"<<perimeter()<<",面积为:"<<area()<<endl<<endl;
}
int main()
{
Triangle Tri1; //定义边长为1 1 1的三角形类实例
Tri1.showMessage();
Triangle Tri2(1.5);//定义边长为1.5 1 1的三角形类实例
Tri2.showMessage();
Triangle Tri3(1.5,1.5);//定义边长为1.5 1.5 1的三角形类实例
Tri3.showMessage();
Triangle Tri4(7,8,9); //定义边长为7 8 9的三角形类实例
Tri4.showMessage();
return 0;
}
/*心得体会:
懵懵懂懂
*/
Unit5_problem1.3多输出默认你
最新推荐文章于 2014-05-27 15:24:13 发布