请用C++实现
时间: 2024-02-25 14:55:27 浏览: 84
好的,以下是用C++实现的代码:
```
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;
class Land {
protected:
double area;
double price;
public:
Land(double a, double p) {
area = a;
price = p;
}
virtual double getIncome() = 0; // 纯虚函数
};
class Circle : public Land {
private:
double radius;
public:
Circle(double r, double p) : Land(3.1415926535 * r * r, p) {
radius = r;
}
double getIncome() {
return area * price;
}
};
class Square : public Land {
private:
double side;
public:
Square(double s, double p) : Land(s * s, p) {
side = s;
}
double getIncome() {
return area * price;
}
};
class Accountant {
private:
double money;
public:
Accountant() {
money = 0;
}
void DevelopEstate(Land *land) {
money += land->getIncome();
}
double CheckMoney() {
return money;
}
};
int main(int argc, const char *argv[]) {
Accountant py;
Circle *a = new Circle(100, 10000);
Square *b = new Square(100, 50000);
py.DevelopEstate(a);
cout << setprecision(10) << py.CheckMoney() << endl;
py.DevelopEstate(b);
cout << setprecision(10) << py.CheckMoney() << endl;
return 0;
}
```
在这个程序中,Land是一个抽象类,Circle和Square是它的子类,Accountant是一个类用于计算Feng Gor的收入。其中,DevelopEstate函数接收一个Land的指针,计算并累加开发这块房地产带来的收入(这里用到了动态绑定),CheckMoney函数返回当前的收入。
阅读全文