求不同图形的面积
#include <iostream>using namespace std;
class Figure {
protected:
double x,y;
public:
Figure(double a,double b) { x = a; y = b; }
virtual void showArea() {
cout<<"No area computation defined";
cout<<"For this class. \n";
}
};
class Triangle:public Figure {
public:
Triangle(double a,double b):Figure(a,b) {};
void showArea() {
cout<<"Triangle with height "<<x;
cout<<" and base "<<y;
cout<<x*y/2<<endl;
}
};
class Square:public Figure {
public:
Square(double a,double b):Figure(a,b) {};
void showArea() {
cout<<"Square with dimension "<<x;
cout<<" * "<<y<<" has an area of ";
cout<<x*y<<endl;
}
};
class Circle:public Figure {
public:
Circle(double a):Figure(a,a) {};
void showArea() {
cout<<"Circle with radius "<<x;
cout<<" has an area of ";
cout<<x*x*3.1416<<endl;
}
};
class Trapezoid:public Figure {
protected:
double z;
public:
Trapezoid(double a,double b,double c):Figure(a,b) { z = c; };
void showArea() {
cout<<"Trapezoid area is: "<<endl;
cout<<(x + y) * z / 2<<endl;
}
};
int main() {
int m;
double base,high;
double length,wide;
double radius;
double b1,b2,h1;
for(int j = 0; j < 3; j++) {
cout<<" _______________________ "<<endl;
cout<<" | 请选择操作 | "<<endl;
cout<<" | 1.求三角形面积 | "<<endl;
cout<<" | 2.求矩形面积 | "<<endl;
cout<<" | 3.求圆面积 | "<<endl;
cout<<" | 4.求梯形面积 | "<<endl;
cout<<" | 请输入操作 | "<<endl;
cout<<" |_____________________| "<<endl;
cin>>m;
cout<<"请输入三角形底与高"<<endl;
cin>>base>>high;
Triangle t(base , high);
cout<<"请输入矩形长与宽"<<endl;
cin>>length>>wide;
Square s(length , wide);
cout<<"请输入圆的半径";
cin>>radius;
Circle c(radius);
cout<<"请输入梯形上下底与高"<<endl;
cin>>b1>>b2>>h1;
Trapezoid p(b1 , b2 , h1);
switch(m) {
case 1:
t.showArea();
break;
case 2:
s.showArea();
break;
case 3:
c.showArea();
break;
case 4:
p.showArea();
break;
}
}
return 0;
}
每次出现操作界面,无论按什么操作都会出现“输入三角形底与高”等,这怎么改?