第五週作業1

#include<iostream>

#include<cmath>

using namespace std;

class Triangle
{
public:
	Triangle(float x, float y, float z) : a(x), b(y), c(z){};
	float Perimeter(void);//計算三角形的周長
	float Area(void);//計算並返回三角形的面積
	void showMessage();
private:
	float a, b, c; //三邊爲私有成員數據
};

void Triangle:: showMessage()
{
	cout << "三角形的三邊長分別爲:" << a << '\t' << b << '\t' << c << endl;
	cout << "該三角形的周長爲:" << Perimeter() << '\t' << "面積爲:" << Area() << endl << endl;
}

void main(void)
{
	Triangle Tri1(3, 4, 5);	//定義三角形類的一個實例(對象)
	Tri1.showMessage();
	Triangle Tri2(7, 8, 9);	//定義三角形類的一個實例(對象)
	Tri2.showMessage();

}


  
float Triangle :: Perimeter(void)//計算周長   
{  
    float d;  
  
    d = a + b + c;  
  
    return d;  
}  
  
  
float Triangle :: Area(void)//計算面積   
{  
    float p, s;  
  
    p = (a + b + c) / 2;  
    s = sqrt(p * (p - a) * (p - b) * (p - c));  
  
    return s;  
}  




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章