#include<iostream>
using namespace std;
class Shape
{
public:
virtual float Area(void) = 0; //求面积
virtual void Setdata(float, float = 0) = 0; //设置图形数据
};
class Triangle :public Shape
{
float W, H; //三角形边长为W,高为H
public:
Triangle(float w = 0, float h = 0){ W = w; H = h; }
float Area(void){ return W*H / 2; }
void Setdata(float w, float h = 0){ W = w; H = h; }
};
class Rectangle :public Shape
{
float W, H;
public:
Rectangle(float w = 0, float h = 0){ W = w; H = h; }
float Area(void){ return W*H; }
void Setdata(float w, float h){ W = w; H = h; }
};
class Square :public Shape
{
float S;
public:
float Area(void){ return S*S; }
void Setdata(float s,float){ S = s; }
};
class Circle :public Shape
{
float R;
public:
float Area(void){ return 3.1415926*R*R; }
void Setdata(float r, float){ R = r; }
};
int main()
{
Triangle tri;
int w, h;
cout << "请输入三角形的底和高:";
cin >> w >> h;
tri.Setdata(w, h);
cout << "三角形的面积为:" << tri.Area() << endl;
Rectangle rec;
cout << "请输入矩形的长和宽:";
cin >> w >> h;
rec.Setdata(w, h);
cout << "矩形的面积为:" << rec.Area() << endl;
Square squ;
int s;
cout << "请输入正方形的边长:";
cin >> s;
squ.Setdata(s,s);
cout << "正方形的面积为:" << squ.Area() << endl;
Circle cir;
int r;
cout << "请输入圆的半径:";
cin >> r;
cir.Setdata(r, r);
cout << "圆的面积为:" << cir.Area() << endl;
return 0;
}
建议看一下http://bbs.fishc.com/forum.php?mod=viewthread&tid=31380 |