7_11.cpp

来自「c++书籍的源代码」· C++ 代码 · 共 60 行

CPP
60
字号
#include<iostream.h>
class A
{
 public:
   A(int x);
 protected:
   int a;
 
};
 A::A(int x)
{
   a=x;
}
class B:virtual public A
{
 public:
   B(int x,int y);
 protected:
   int b;
};
B::B(int x,int y):A(x)
{
  b=y; 
}
class C:virtual public A
{
 public:
   C(int x,int y);
 protected:
   int c;
};
C::C(int x,int y):A(x)
{
  c=y; 
}
class D:public B,public C
{
 public:
   D(int w,int x,int y,int z);
   void display(void);
 private:
   int a;
 
};
D::D(int w,int x,int y,int z):A(w),B(w,x),C(w,y)
{
  a=z;
}
void D::display(void)
{
   cout<<"A::a="<<A::a<<endl;
   cout<<"a="<<a<<" b="<<b<<" c="<<c<<endl;
}
int main()
{
   D s(1,2,3,4);
   s.display();  
   return(0);
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?