f1008.cpp
资源名称:c.rar [点击查看]
上传用户:puke2000
上传日期:2022-07-25
资源大小:912k
文件大小:1k
源码类别:

C#编程

开发平台:

Visual C++

  1. //=====================================
  2. // f1007.cpp
  3. // virtual inheritance
  4. //=====================================
  5. #include<iostream>
  6. using namespace std;
  7. //-------------------------------------
  8. class Furniture{
  9. protected:
  10.   int weight;
  11. public:
  12.   Furniture(){}
  13.   void setWeight(int i){ weight =i; }
  14.   int getWeight()const{ return weight; }
  15. };//-----------------------------------
  16. class Bed : virtual public Furniture{
  17. public:
  18.   Bed(){}
  19.   void sleep()const{ cout <<"Sleeping...n"; }
  20. };//-----------------------------------
  21. class Sofa : virtual public Furniture{
  22. public:
  23.   Sofa(){}
  24.   void watchTV()const{ cout <<"Watching TV.n"; }
  25. };//-----------------------------------
  26. class SleeperSofa : public Bed, public Sofa{
  27. public:
  28.   SleeperSofa() :Sofa(), Bed(){}
  29.   void foldOut()const{ cout <<"Fold out the sofa.n"; }
  30. };//-----------------------------------
  31. int main(){
  32.   SleeperSofa ss;
  33.   ss.setWeight(20);
  34.   cout<<ss.getWeight()<<endl;
  35. }//====================================
  36.