正在加载图片...
例12=1(公有派生 void maino include<iostream.h> Bb class a b setx(2); b. sety (3); public cout<<X"<<.getxo<<"\tY=" void setx(int a)x=a; kb gety o<<endl; void sety(int b)y=b; 3 cout<<"X+Y=<<b getsum(<<endl; int getxO constreturn x; int getyO constreturn y; 3 protected: int x private: y }; class b: public a public: int getsumo{ return x+gety);}∥直接访间从基类继承来的保护成员x;但是基 类的私有成员y,只能通过接口函数访问。#include<iostream.h> class A { public: void setx(int a){x=a;} void sety(int b){y=b;} int getx() const{return x;} int gety() const{return y;} protected: int x; private: int y; }; class B:public A { public: int getsum(){return x+gety());} //直接访问从基类继承来的保护成员x;但是基 //类的私有成员y,只能通过接口函数访问。 }; 例 12-1 公有派生 void main() { B b; b.setx(2); b.sety(3); cout<<″X=″<<b.getx()<<″\tY=″ <<b.gety()<<endl; cout<<″X+Y=″<<b.getsum()<<endl; }
<<向上翻页向下翻页>>
©2008-现在 cucdc.com 高等教育资讯网 版权所有