第五周实验报告 1-1
2012-03-21 18:25
316 查看
#include <iostream> #include <Cmath> using namespace std; class Triangle {public: Triangle(float x, float y, float z); float perimeter(void);//计算三角形的周长 float area(void);//计算并返回三角形的面积 void showMessage(); private: float a,b,c; //三边为私有成员数据 }; void main(void) { Triangle Tri1(4,5,6);//定义三角形类的一个实例(对象) Tri1.showMessage(); Triangle Tri2(7,8,9); //定义三角形类的一个实例(对象) Tri2.showMessage(); system ("Pause"); } Triangle::Triangle(float x, float y, float z) { a = x; b = y; c = z; } //计算三角形的周长 float Triangle::perimeter(void) { return (a + b + c); } //计算并返回三角形的面积 float Triangle::area(void) { double t,s; t = (a + b + c) / 2; s = sqrt(t * (t - a) * (t - b) * (t - c)); return s; } void Triangle:: showMessage() { cout<<"三角形的三边长分别为:"<< a << '\t' << b << '\t' << c << endl; cout<<"该三角形的周长为:"<< perimeter() << '\t' <<"面积为:"<< area() << endl; } #include <iostream> #include <Cmath> using namespace std; class Triangle {public: Triangle(float x, float y, float z); float perimeter(void);//计算三角形的周长 float area(void);//计算并返回三角形的面积 void showMessage(); private: float a,b,c; //三边为私有成员数据 }; void main(void) { Triangle Tri1(4,5,6);//定义三角形类的一个实例(对象) Tri1.showMessage(); Triangle Tri2(7,8,9); //定义三角形类的一个实例(对象) Tri2.showMessage(); system ("Pause"); } Triangle::Triangle(float x, float y, float z) { a = x; b = y; c = z; } //计算三角形的周长 float Triangle::perimeter(void) { return (a + b + c); } //计算并返回三角形的面积 float Triangle::area(void) { double t,s; t = (a + b + c) / 2; s = sqrt(t * (t - a) * (t - b) * (t - c)); return s; } void Triangle:: showMessage() { cout<<"三角形的三边长分别为:"<< a << '\t' << b << '\t' << c << endl; cout<<"该三角形的周长为:"<< perimeter() << '\t' <<"面积为:"<< area() << endl; }