//-----------------------------------
//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
//----------------------------------- //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//----------------------------------- //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.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(); }//----------------------------------