2 回答
TA贡献1843条经验 获得超7个赞
#include <iostream>
#include <math.h>
#define PI 3.14
using namespace std;
class Shape //Shape这个类里面重载了计算面积的函数
{
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条经验 获得超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 关注
- 775 浏览
添加回答
举报