首页 > 试题广场 >

一个三口之家,大家都知道父亲会开车,母亲会唱歌。但是只有家里

[问答题]
一个三口之家,大家都知道父亲会开车,母亲会唱歌。但是只有家里人知道父亲还会修电视机。小孩既会开车又会唱歌也会修电视机,此外小孩还会打乒乓球。母亲瞒着家人在外面做小工以补贴家用 。
试编程,让这三口之家从事一天的活动:先是父亲开车出去,然后母亲出去工作(唱歌),母亲下班后去做两小时小工。小孩在俱乐部打球,父亲回家后,再开车玩,后又高兴地唱歌。晩上,小孩和父亲一起修理电视机。
后来父亲的修电视机技术让大家知道了,父亲也经常为邻居修电视机。这时,程序要做什么变动 。

推荐
基类描述的基本操作,如下所示:
//-----------------------------------
//Bate.h
//-----------------------------------
#ifndef HEADER_BASE
#define HEADER_BASE
#include<iostream>
using namespace std;
//-----------------------------------
class Bate{
public:
  void doMannulWork(){ cout<<"DoMannulWork...\n"; }
  void repairTVSet(){ cout<<"RepairTVSet...\n"; }
  void playPingPong(){ cout<<"PlayPingPong...\n"; }
  void driveACar(){ cout<<"DriveACar...\n"; }
  void dingSongs(){ cout<<"SingSongs...\n"; }
};//---------------------------------
#endif                        //HEADER_BASE
父类和母类并不继承所有的操作,而且一些操作还将转为私有和保护,因此,不应将所有操作从Bate类作为公有继承,否则,父母对象将自然地能够执行任何操作,有悖问题的初衷。这就是父母类为什么要虚拟保护性继承的原因。两者的代码如下:
//-----------------------------------
//Father.h
//-----------------------------------
#ifndef HEADER_FATHER
#define HEADER_FATHER
#include"Bate.h"
#include<iostream>
//-----------------------------------
class Father : virtual protected Base{
public:
  Father(){ std::cout<<"Father is created.\n"; }
  using Base::driveACar;
  void working(){ std::cout<<"Father: "; repairTVSet(); }
};//---------------------------------
#endif                     //HEADER_FATHER

//-----------------------------------
//Mother.h
//-----------------------------------
#ifndef HEADER_MOTHER
#define HEADER_MOTHER
#include"Bate.h"
#include<iostream>
//-----------------------------------
class Mother : virtual protected Base{
  using Bate::doMannulWork;
public:
  Mother(){ std::cout<<"Mother is created.\n"; }
  using Base::singSongs;
  void working(){ std::cout<<"Mother: "; doMannulWork(); }
};//---------------------------------
#endif                     //HEADER_MOTHER
小孩要继承父母类,而且是无条件全部继承。见如下代码:
//-----------------------------------
//Son.h
//-----------------------------------
#ifndef HEADER_SON
#define HEADER_SON
#include"Father.h"
#include"Mother.h"
#include<iostream>
//-----------------------------------
class Son : public Father, public Mother{
public:
  Son(){ std::cout<<"Son is created.\n"; }
  using Base::playPingPong;
  using Father::repairTVSet;
};//---------------------------------
#endif                     //HEADER_SON
完成了所有类的设计之后,应用程序的设计可以按时间顺序展开,分段在早上、下午和晚上进行活动。具体实例代码如下:
//-----------------------------------
//EX1006_1.cpp
//-----------------------------------
#include"Father.h"
#include"Mother.h"
#include"Son.h"
#include<iostream>
using namespace std;
//-----------------------------------
int main(){
  Father father;
  Mother mother;
  Son son:
  cout<<"\nMorning...\n";
  cout<<"Father: "; father.driveACar();
  cout<<"Mother: "; mother.singSongs();
  cout<<"\nAfternoon...\n";
  mother.working();
  cout<<"Son: ";   son.playPingPong();
  cout<<"After Father come back\n Son: "; son.driveACar();
  cout<<" Son: ";   son.singSongs();
  cout<<"\nEvening...\n";
  father.working();
  cout<<"Son: ";     son.repairTVSet();
}//----------------------------------

其运行结果为:
Father is created.
Mother is created.
Father is created.
Mother is created.
Son is created.

Morning...
Father: DriveACar...
Mother: SingSongs...

Afternoon...
Mother: DoMannulWork...
Son: PlayPingPong...
After Father come back
Son: DriveACar...
Son: SingSongs...

Evening...
Father: RepairTVSet...
Son: RepairTVSet...
如果父亲的修电视机技术让大家知道了,他也乐意为大家服务,也就是可以在应用程序中直接调用该操作。那么,只要将父类的修理电视机操作改为公有的就行了,而且一旦父类的修理操作成为公有,就可以在应用程序中直接调用,无需经成员函数 working 之手了。所以修改的两个文件代码分别为:
//-----------------------------------
//Father.h
//-----------------------------------
#ifndef HEADER_FATHER
#define HEADER_FATHER
#include"Bate.h"
#include<iostream>
//-----------------------------------
class Father : virtual protected Base{
public:
  Father(){ std::cout<<"Father is created.\n"; }
  using Base::driveACar;
  using Bate::repairTVSet;    //改变属性
  //废弃 void working(){ std::cout<<"Father: "; repairTVSet(); }
};//---------------------------------
#endif                     //HEADER_FATHER

//-----------------------------------
//EX1006_2.cpp
//-----------------------------------
#include"Father.h"
#include"Mother.h"
#include"Son.h"
#include<iostream>
using namespace std;
//-----------------------------------
int main(){
  Father father;
  Mother mother;
  Son son:
  cout<<"\nMorning...\n";
  cout<<"Father: "; father.driveACar();
  cout<<"Mother: "; mother.singSongs();
  cout<<"\nAfternoon...\n";
  mother.working();
  cout<<"Son: ";   son.playPingPong();
  cout<<"After Father come back\n Son: "; son.driveACar();
  cout<<" Son: ";   son.singSongs();
  cout<<"\nEvening...\n";
  cout<<"Father: ";  father.repairTVSet();        //废弃 father.working();
  cout<<"Son: ";     son.repairTVSet();
}//----------------------------------



发表于 2018-05-07 21:20:25 回复(0)
父亲的修电视机技术设为静态吧
发表于 2021-03-25 21:00:57 回复(0)