题目如下: 10.9 练习10 第6题
一个三口之家,大家都知道父亲会开车,母亲会唱歌。但是只有家里人知道父亲还会修电视机。小孩既会开车又会唱歌也会修电视机,此外小孩还会打乒乓球。母亲瞒着家人在外面做小工以补贴家用。
试编程,让这三口之家从事一天的活动:先是父亲开车出去,然后母亲出去工作(唱歌),母亲下班后去做两小时小工。小孩在俱乐部打球,父亲回家后,再开车玩,后又高兴地唱歌。晚上,小孩和父亲一起修理电视机。
我的代码如下:
- #include<iostream>
- #include<iomanip>
- using namespace std;
- //-------------------------------------
- class father
- {
- protected:
- void Repair(){cout<<"repair TV."<<endl;}
- public:
- void Drvie(){cout<<"Drive out or play."<<endl;}
- void Sing(){cout<<"Singing."<<endl;}
- void visitR(){cout<<"Father ";Repair();}
- };//-----------------------------------
- class mother
- {
- private:
- void Daylab(){cout<<"Mother do two hours day laborers."<<endl;}
- public:
- void Sing(){cout<<"Singing or work."<<endl;}
- void visit(){Daylab();}
- };//-----------------------------------
- class child:public father,public mother
- {
- public:
- void Play(){cout<<"Play billiards."<<endl;}
- void visitCR(){cout<<"Child ";Repair();}
- };//-----------------------------------
- int main()
- {
- father f;
- mother m;
- child c;
- cout<<"This is the family for the day's activities:"<<endl;
- cout<<"Day:"<<endl;
- f.Drvie();
- m.Sing();
- m.visit();
- c.Play();
- f.Drvie();
- f.Sing();
- cout<<"Night:"<<endl;
- f.visitR();
- c.visitCR();
- return 0;
- }
基本功能实现了,但自认为有两点没弄好:
1、只有家里人知道父亲还会修电视机没实现,孩子由于继承知道父亲会修电视机,但母亲不知怎么实现,希望高手解答。
2、35---43行代码输出的活动安排中没有具体到人,也就是说只说干了那些事,没说明白谁干的。但如果在父亲母亲Sing等公有函数中打印“father do”之类的,小孩对象调用对应成员本应打印出“child do”但只会打印出“father do”那不出错,所以不好怎么实现。
欢迎拍砖 指教 交流!!!