2 回答

TA貢獻1843條經(jīng)驗 獲得超7個贊
#include <iostream>
#include <math.h>
#define PI 3.14
using namespace std;
class Shape //Shape這個類里面重載了計算面積的函數(shù)
{
public:
float Shape::GetArea(float radious)//計算圓的面積
{
return float (PI*radious*radious);
}
float Shape::GetArea(float side1, float side2, float side3)//計算三角形面積
{
float average=(side1+side2+side3)/2;
return float (sqrt(average * (average-side1) * (average-side2) * (average-side3)));
}
float Shape::GetArea(float side1, float side2)//計算長方形面積
{
return float (side1 * side2);
}
};
class Triangle : public Shape
{
public:float side1, side2 ,side3;
public:
Triangle()
{
cout<<"輸入三角形三邊長度:"<<endl;
cin>>side1>>side2>>side3;
}
};
class Circle : public Shape
{
public:float radious;
public:
Circle()
{
cout<< "輸入圓的半徑:"<<endl;
cin>>radious;
}
};
class Rectangle : public Shape
{
public:float side1, side2;
public:
Rectangle()
{
cout<<"輸入長方形的長和寬:"<<endl;
cin>>side1>>side2;
}
};
int main()
{
int num;
cout<<"請選擇你要計算面積的圖形:"<<endl<<"1.三角形 2.圓形 3.矩形"<<endl;
do{
cin>>num;
switch(num){
case 1:{
Triangle* T = new Triangle;
cout<<"三角形的面積為:"<<T->GetArea(T->side1, T->side2, T->side3)<<endl;
delete T;
break;
}
case 2:{
Circle* C = new Circle;
cout<<"圓的面積為:"<<C->GetArea(C->radious)<<endl;
delete C;
break;
}
case 3:{
Rectangle* R = new Rectangle;
cout<<"長方形的面積為:"<<R->GetArea(R->side1, R->side2)<<endl;
delete R;
break;
}
default:cout<<"請在1-3之間選擇:";
}
}while (num != 1|| num != 2|| num != 3);
return 0;
}

TA貢獻1878條經(jīng)驗 獲得超4個贊
#include<iostream>
using namespace std;
class graph
{
protected:
float high,wide;
public:
graph();
graph(float h,float w)
{
high=h;wide=w;cout<<"高為:"<<h<<"\t寬為:"<<w<<endl;} };
class retangle:public graph
{
public:
retangle(float h,float w):graph(h,w){}
void area()
{ cout<<"矩形的面積是:"<<high*wide<<endl;}
};
class triangle:public graph
{
public:
triangle(float h,float w):graph(h,w){}
void area()
{ cout<<"等腰三角形的面積是:"<<high*wide/2<<endl;}
};
void main()
{ retangle g(2,3);
g.area();
triangle h(2,3);
h.area();
}
- 2 回答
- 0 關(guān)注
- 806 瀏覽
添加回答
舉報