编写一个抽象类Shape,在此基础上派生出类Rectangle和Circle,两者都有计算对象面积
#include <iostream>
using namespace std;
class Shape
{
public:
virtual double GetArea() = 0;
virtual double GetP() = 0;
};
class Rectangle : public Shape
{
private:
double a;
double b;
public:
Rectangle(double aa, double bb) {a=aa;b=bb;cout<<"长"<<a<<"宽"<<b<<endl;}
virtual double GetArea() { return a * b; }
virtual double GetP() { return a + a + b + b; }
};
class Circle : public Shape
{
private:
double r;
public:
Circle(double rr) {r=rr;cout<<"半径"<<r<<endl; }
virtual double GetArea() { return r * r * 3.14; }
virtual double GetP() { return 2 * r * 3.14; }
};
void main()
{
double length, width;
cout << "输入长和宽: ";
cin >> length >> width;
Rectangle rect(length, width);
cout << "面积是:"<< rect.GetArea() << endl<<"周长是:"<<rect.GetP()<<endl;
double rr;
cout << "输入半径: ";
cin >> rr;
Circle cir(rr);
cout << "面积是:"<<cir.GetArea() << endl<<"周长是:"<<cir.GetP()<<endl;
}
不能运行啊