例44保护成员的例子 void main0 #include <iostream.h> class sampt i samp obj(20, 30) int a, 0b:=88;/法,类的私有成员不能对外 protected: obb=99;/排法,类的保护成员相当于私 int b public 有成员不能对外 int c: obc=50/合法,类的公有成员能对外 samp(int n, int m cout < obj geta(<< a=n;b=m;}∥构造函数 int getae return a; j cout < obj getb( <<<<obj. c<<endl int geto return b; ))
void main () { samp obj(20,30); obj.a=88; //非法,类的私有成员不能对外 obj.b=99; //非法,类的保护成员相当于私 有成员不能对外 obj.c=50 //合法,类的公有成员能对外 cout << obj.geta() << ' '; cout << obj.getb() << ' ' <<obj.c<<endl; } 例4.4 保护成员的例子 #include <iostream.h> class samp{ int a; protected: int b; public: int c; samp(int n, int m ) { a=n; b=m; } //构造函数 int geta() { return a; } int getb() { return b; } }; 11
例45保护成员被公有派生后的例子 12 #include <iostream.h> class base i protected: inta,b;∥保护成员 public void stab(int n, int m) a=n; b=m;) class derive: public base i int c. public void setc(int n)i c=n; void showabo(/a,b相当于派生类的私有成员成员函数可访问 i cout <<a<<<<b<<k<<c<<endl; y void maino i derive obj obj. stab2,4;/基类的公有成员相当于派生类的公有成员可被外 访问 obj. setc) obj. Showabc0;
例4.5 保护成员被公有派生后的例子 #include <iostream.h> class base { protected: int a,b; //保护成员 public: void setab(int n, int m) { a=n; b=m; } }; class derive:public base { int c; public: void setc(int n) { c=n; } void showabc()//a,b 相当于派生类的私有成员,成员函数可访问 { cout <<a<<' '<<b<<' '<<c<<endl; } }; void main() { derive obj; obj.setab(2,4); //基类的公有成员相当于派生类的公有成员,可被外 界访问 obj.setc(3); obj.showabc(); } 12
例46保护成员被私有派生后的例子 #include <iostream h> lass base class drivel: private base protected protected: inta;/保护成员 int b: public public void seta (int sa) a=sa; j void setb(int sb)ib=sb;) void maino class derive2: public derive i i base opl; int c: public 0p1eta(1): void setc(int sc) c=sc;) derive op2: void showo 0p2etb(2); i cout<<"a=<<a<<endl; //F 法,保护成员a被 deri derive20p3; 成为 derive的私有成员, derie2中 op3. setc(3) cou<“b=”<b<<endl;∥合法 c0ut<“c=”<c<<end;}∥合法 op3. showO
例4.6 保护成员被私有派生后的例子 #include <iostream.h> class base { protected: int a; //保护成员 public: void seta(int sa) { a=sa; } }; class derive2:public derive1 { int c; public: void setc(int sc) { c=sc; } void show() { cout<<"a="<<a<<endl; //非法,保护成员a 被derive1私有派生后 成为derive1的私有成员,dervie2中不能//直接访问 cout<<“b=”<<b<<endl; //合法 cout<<“c=”<<c<<endl; } //合法 }; void main() { base op1; op1.seta(1); derive1 op2; op2.setb(2); derive2 op3; op3.setc(3); op3.show(); } 13 class derive1:private base { protected: int b; public: void setb(int sb) { b=sb; } };
表4.2基类成员在派生类中和外部函数中的访问权限 派生方式基类中的访问权限派生类中的访问权限 public public public protecte protected (公有派生) private private public private private protected private (私有派生) private
表4.2 基类成员在派生类中和外部函数中的访问权限 派 生 方 式 基类中的访问权限 派生类中的访问权限 public (公有派生) public protected private public protected private private (私有派生) public protected private private private 14
保护成员的这种性质还可传播给派生的派生。 15 例保护成员的性质传播给派生的派生的例子。 class base int value protected void Set value(int v) value=v;) class first: public base i int total public void SetTotal(int t) i total-t; Set value(t;使用基类的保护成员,相当于本类的保护成员 class Second: public First int count: HInd void Set Count(int c) i count=c; Set value(c);/使用基类的保护成员,相当于本类的保护成员
保护成员的这种性质还可传播给派生的派生。 例 保护成员的性质传播给派生的派生的例子。 class Base { int value; protected: void SetValue(int v) { value=v; } }; class First : public Base { int total; public: void SetTotal(int t) { total=t; SetValue(t); //使用基类的保护成员,相当于本类的保护成员 } }; class Second : public First { int count; public: void SetCount(int c) { count=c; SetValue(c); //使用基类的保护成员,相当于本类的保护成员 } }; 15