611a.cpp
来自「C++实训教程」· C++ 代码 · 共 72 行
CPP
72 行
/*
611a.CPP
了解继承关系
CopyRight by Feng. 2004/7 2003/10 1997/10
*/
#include<iostream.h>
class B
{
private: int b0;
protected: int b1;
public:
int b;
B (int n){ b0=n;b1=n+1;b=n+2;}
void Disp()
{cout << " b0=" <<b0<<" b1="<<b1<<" b=" <<b<<endl;}
};
class E : private B
{
protected:int e1;
public:
E (int x,int y):B(x)
{ e1=y + b1 + b;}
};
class G : protected B
{
protected:int g1;
public:
G (int x,int y):B(x+y) {b=y; b1=y;g1=x;}
};
class EE : public E
{
protected:int ee1;
public:
EE (int x,int y):E(x,y)
{ ee1=y ;}
void Show()
{ cout << "Sum=" << (ee1 + e1)<<endl; }
//can not access b1,b ,Disp();
};
class GG : public G
{
protected:int gg1;
public:
GG (int x,int y):G(x,y) {b=y; b1=y;g1=x;}
void Show()
{ cout << "Sum=" << (gg1 + g1+b1+b)<<endl; Disp(); }
};
main(void)
{
EE e(2,22);
e.Show();
//e.b = 1; //cannot access
//e.Disp(); //cannot access
GG g(4,44);
//g.Disp(); //cannot access
return 0;
}
/*
Sum=51
*/
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?