程序填空题:爸爸、妈妈和我 - C/C++ 代码复用
一个家庭通常由多个家庭成员组成,请使用组合模式设计Family类。为Family类添加outdoorPicnic()函数模拟一次家庭野餐过程:各个家庭成员通力协作完成全部的工作,比如爸爸负责开车和搭帐篷,妈妈负责采购食物,弟弟负责搞笑,我负责拍照。
请将下述程序补充完整,使其能够正常工作。
c++
#include <iostream>
using namespace std;
class Daddy {
public:
string sName;
void {
cout << "Daddy " << sName << "'s duty: Driving & Building Shelter.\n";
}
};
{
string sName;
void contribute(){
cout << "Mummy " << sName << "'s duty: Prepare Food.\n";
}
};
class Brother {
public:
string sName;
void contribute(){
}
};
class MySelf {
public:
void contribute(){
cout << sName << "'s duty: Taking Photos.\n";
}
};
class Family {
public:
m;
Daddy ;
;
MySelf s;
void outdoorPicnic(){
printf("---------outdoor picnic------------\n");
d.;
m.contribute();
b.
s.contribute();
}
};
int main()
{
Family myFamily;
myFamily.m.sName = "Emily"; //妈妈的姓名
myFamily.m.contribute();
myFamily.d.sName = "Jack"; //爸爸的姓名
myFamily.d.contribute();
myFamily.b.sName = "Tom"; //弟弟的姓名
myFamily.b.contribute();
myFamily.s.sName = "Dora"; //我的姓名
myFamily.s.contribute();
myFamily.outdoorPicnic();
return 0;
}
### 感觉不会? 那试着听听**免费的B站网课**
[简洁的C和C++ - 重庆大学在线课程](https://www.bilibili.com/video/BV1it411d7zx/)
[Python编程基础及应用 - 重庆大学在线课程](https://www.bilibili.com/video/BV1kt411R7uW/)
![image.png](~/6e79c9e3-cb7f-486d-ab78-36b5a8f655c0.png)
答案:
第1空:contribute()
第2空:class Mummy
第3空:public:
第4空:cout << "Brother " << sName << "'s duty: Joking.\n";
第5空:string sName;
第6空:Mummy
第7空:d
第8空:Brother b
第9空:contribute()
第10空:contribute();
请将下述程序补充完整,使其能够正常工作。
c++
#include <iostream>
using namespace std;
class Daddy {
public:
string sName;
void {
cout << "Daddy " << sName << "'s duty: Driving & Building Shelter.\n";
}
};
{
string sName;
void contribute(){
cout << "Mummy " << sName << "'s duty: Prepare Food.\n";
}
};
class Brother {
public:
string sName;
void contribute(){
}
};
class MySelf {
public:
void contribute(){
cout << sName << "'s duty: Taking Photos.\n";
}
};
class Family {
public:
m;
Daddy ;
;
MySelf s;
void outdoorPicnic(){
printf("---------outdoor picnic------------\n");
d.;
m.contribute();
b.
s.contribute();
}
};
int main()
{
Family myFamily;
myFamily.m.sName = "Emily"; //妈妈的姓名
myFamily.m.contribute();
myFamily.d.sName = "Jack"; //爸爸的姓名
myFamily.d.contribute();
myFamily.b.sName = "Tom"; //弟弟的姓名
myFamily.b.contribute();
myFamily.s.sName = "Dora"; //我的姓名
myFamily.s.contribute();
myFamily.outdoorPicnic();
return 0;
}
### 感觉不会? 那试着听听**免费的B站网课**
[简洁的C和C++ - 重庆大学在线课程](https://www.bilibili.com/video/BV1it411d7zx/)
[Python编程基础及应用 - 重庆大学在线课程](https://www.bilibili.com/video/BV1kt411R7uW/)
![image.png](~/6e79c9e3-cb7f-486d-ab78-36b5a8f655c0.png)
答案:
第1空:contribute()
第2空:class Mummy
第3空:public:
第4空:cout << "Brother " << sName << "'s duty: Joking.\n";
第5空:string sName;
第6空:Mummy
第7空:d
第8空:Brother b
第9空:contribute()
第10空:contribute();