⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 611.cpp

📁 C++实训教程
💻 CPP
字号:
/*
	611.CPP
	了解继承关系
	CopyRight by Feng.		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 D : public B
{
	protected:   int d1;
	public:
		D (int x,int y):B(x)
		{ 	d1=y;b1++;}   //May Access b1,but not b0
};

class E : B  //Note: private
{
	protected:int e1;
	public:
		E (int x,int y):B(x)
		{ 	e1=y + b1 + b;}
      void Show()
      { cout << "e1=" << e1; Disp(); }
};

class F : private B
{
	public:
		B::b;//specifically declare public
      //void B::Disp();  not allowed qualifier
		F (int x,int y):B(x+y)  {b1=b=y;}
};

class G : protected B
{
	public:
		B::b;//specifically declare public
		G (int x,int y):B(x+y)  {b=y; b1=y;}
};

main(void)
{
	D d(1,11);
	d.b=1;//public member of B are public in D
   d.Disp();
	E e(2,22);
   e.Show();
	//e.b = 1;  //cannot access
   //e.Disp(); //cannot access
	F f(3,33);
   //f.Disp();//cannot access
   cout<<" f.b="<<f.b<<endl;//private from B,but may access b!

   G g(4,44);
   cout << " g.b = " << (g.b = 999) <<endl;  //now can access
   //g.Disp(); //cannot access

	return 0;
}
/*
b0=1  b1=3  b=1
e1=29  b0=2  b1=3  b=4
f.b=33
g.b = 999

*/

⌨️ 快捷键说明

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