- 1、原创力文档(book118)网站文档一经付费(服务费),不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。。
- 2、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考,付费前请自行鉴别。如您付费,意味着您自己接受本站规则且自行承担风险,本站不退款、不进行额外附加服务;查看《如何避免下载的几个坑》。如果您已付费下载过本站文档,您可以点击 这里二次下载。
- 3、如文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“版权申诉”(推荐),也可以打举报电话:400-050-0827(电话支持时间:9:00-18:30)。
查看更多
第5章继承与派生概要
* 设计类层次结构 类层次结构设计示例-飞机仪表盘模拟 step4:第三次迭代 将Dial分为单根指针(速度)和两根指针(时间和高度),将来还可扩充 Clock和Altimeter属于TwoHandDial的子类,分别为单向转动指针和双向转动指针 * Instrument Dial HorizonAttitude FuelGauge Rader Clock Altimeter Speed 带指针 的指示器 Indicator Display 不带指针 的指示器 单个值 多个值 固定数量 可变数量 TwoHandDial 两根指针 单根指针 单向 双向 * int main() { A aa(1,2); aa.show(); B bb(3,4,5,6); bb.fun(); bb.A::show(); bb.B::show(); bb.f1(); return 0; } 1. class A { public: A(int i, int j){ a=i; b=j; } void move(int x ,int y){ a +=x; b+=y; } void show() { cout(a,b)endl; } private: int a,b; }; class B: public A { public: B(int i,int j, int k, int l):A(i,j),x(k),y(l){} void show() { coutx,yendl; } void fun(){ move(3,5); } void f1(){ A::show(); } private: int x,y; }; * int main() { A aa(1,2); aa.show(); B bb(3,4,5,6); bb.fun(); bb.show(); bb.f1(); return 0; } 2. class A { public: A(int i, int j):a(i),b(j) { } void move(int x ,int y){a +=x; b+=y;} void show() { cout(a,b)endl; } private: int a,b; }; class B: private A { public: B(int i,int j, int k, int l):A(i,j),x(k),y(l){} void show() { coutx,yendl; } void fun(){move(3,5);} void f1(){A::show();} private: int x,y; }; * class subs:public base { base bobj; base1 bobj1; int x; public: subs(int a, int b, int c, int d) :base(a),bobj1(b),bobj(c) { coutconstructing sub classendl; x=d; coutx=xendl; } ~subs() { coutdestrucing sub classendl; } }; int main() { subs s(1,2,3,4); return 0; } 3. class base { int n; public: base(int a) { coutconstructing base classendl; n=a; coutn=nendl; } ~base() { coutdestrucing base
文档评论(0)