贼(Thief)偷窃行人,每个行人都有随身带的钱,贼偷窃一个行人,他的钱就增加相应的钱包里的钱数。 请给出Thief类和Walker类。 点击复制后,将打开C知道体验页 以下是Thief类和Walker类的示例代码:c++实现
时间: 2024-05-07 22:20:43 浏览: 17
```c++
#include <iostream>
#include <string>
using namespace std;
class Walker {
public:
Walker(int money) {
this->money = money;
}
int get_money() const {
return money;
}
private:
int money;
};
class Thief {
public:
Thief() {
this->money_in_pocket = 0;
}
void steal(Walker& walker) {
int money = walker.get_money();
money_in_pocket += money;
walker = Walker(0);
}
int get_money_in_pocket() const {
return money_in_pocket;
}
private:
int money_in_pocket;
};
int main() {
Walker walker1(100);
Walker walker2(200);
Thief thief;
thief.steal(walker1);
thief.steal(walker2);
cout << "Thief's money in pocket: " << thief.get_money_in_pocket() << endl;
cout << "Walker1's money: " << walker1.get_money() << endl;
cout << "Walker2's money: " << walker2.get_money() << endl;
return 0;
}
```
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)