面向对象系统分析与设计(2)

上传人:wm****3 文档编号:57215774 上传时间:2018-10-20 格式:PPT 页数:28 大小:326.50KB
返回 下载 相关 举报
面向对象系统分析与设计(2)_第1页
第1页 / 共28页
面向对象系统分析与设计(2)_第2页
第2页 / 共28页
面向对象系统分析与设计(2)_第3页
第3页 / 共28页
面向对象系统分析与设计(2)_第4页
第4页 / 共28页
面向对象系统分析与设计(2)_第5页
第5页 / 共28页
点击查看更多>>
资源描述

《面向对象系统分析与设计(2)》由会员分享,可在线阅读,更多相关《面向对象系统分析与设计(2)(28页珍藏版)》请在金锄头文库上搜索。

1、面向对象系统分析与设计,C+中的面向对象特征 1、了解C+中的面向对象实现机制 2、从中得到一些解决问题的方法,C+简介,C+中新增非面向对象特征,C+中的面向对象特征,内联,引用,new/delete,重载,类定义,对象定义(静态对象),对象定义(对象指针和无名对象),构造方法,构造方法(续),MyClassC(int a=0,int b=0)x=a;y=b;/合并之后的构造函数,拷贝构造方法,MyClassC(MyClassC ,MyClassC a(10,20); MyClassC b(a);/调用拷贝构造函数,用对象a产生对象b MyClassC c=b;/调用拷贝构造函数的另一种形式

2、,用对象b产生对象c MyClassC *p,*q; p=new MyClassC(a);/调用拷贝构造函数,用对象a产生一个无名对象,构造方法实例分析,class MyClassCOUT int x; int y; public:MyClassCOUT()x=0;y=0;cout“OUT1“endl;/构造函数1MyClassCOUT(int a)x=a;y=0;cout“OUT2“endl;/构造函数2MyClassCOUT(int a,int b)x=a;y=b;cout“OUT3“set(11,431); coutgety()endl; cout(*r).gety()x=x; (*th

3、is).y=y;thisexample getx( ) coutxendl; return *this; thisexample *gety( ) coutygetx();/等价于(te.gety()-getx();,友元,class FriendClassint x;int y; public: /其它成员函数friend void f(int,int);friend A;friend void B:f (FriendClass ,class Aint x; int y; public:void f(FriendClass ,void f(int a)FriendClass fc;fc.x=

4、a;fc.y=fc.x+10;coutfc.xendl;coutfc.yendl; ,class B int x; int y; public:void f(FriendClass ,static成员,class staticexampleint x;static int y;/定义一个私有static数据成员 public:staticexample(int x1)x=x1;/构造函数,不初始化static成员static int z;/定义一个公有static数据成员int getx()return x;static int gety()return y;/获取私有static数据成员的值

5、static void sety(int y1)y=y1;/对私有static数据成员赋值 ;,int staticexample:y=0; /私有static数据成员初始化 int staticexample:z=10; /公有static数据成员初始化,void staticapp()staticexample a(1),b(2),c(3);/定义对象staticexample:sety(3);/调用static函数b.z=b.z+30;/使用static数据成员coutb.gety()endl; /调用static函数couta.zendl; /使用static数据成员 ,继承,继承的本

6、质从现有的类生成新类 分类层次继承和组合继承 继承机制单/多继承(重复继承/虚继承) 继承方式公有/私有/受保护继承 继承过程中应该考虑到的相关问题: 不同继承机制的物理意义及其实现 不同继承方式下父类成员在子类中的特征 同名问题与方法的覆盖机制 子类构造方法与父类构造方法之间的关系 父类指针与子类对象 层次继承和组合继承的概念及实现上的异同,不同的继承机制,继承方式及其父类成员特征变迁,单继承,class Aint x; protected:int y; public:int z;/公有数据成员A(int a=0,int b=0,int c=0)x=a; y=b; z=c;cout“Cons

7、tructor“endl; int getx()return x;int gety()return y;int getz()return z; ;,class B :public Aint bx; protected:int by; public:int bz;B(int a,int b,int c,int d,int e,int f):A(d,e,f)bx=a; by=b; bz=c;cout“B_Constructor“endl;int getx()return bx;/覆盖int gety()return by;int getz()return bz; ;,class C :public

8、 B,class B :private A,class B :protected A,B(int a,int b,int c,int d,int e,int f ):A( ),B(int a,int b,int c,int d,int e,int f ) ,多继承,class D:public A1,protected A2,private A3;,D():A1(),A2(),A3(),c1.A1:getx() c1.A2:getx() c1.B1:getx()c1.B2:getx(),虚继承,class Aprotected: int x; int y;public:A(int a,int

9、b)x=a;y=b;int getx()return x;int gety()return y; ;,class B:public virtual Aint a1;public:B(int a,int b,int c):A(a,b) a1=c;int geta1()return a1;,class C:public virtual Aint a2;public:C(int a,int b,int c):A(a,b) a2=c;int geta2()return a2;,class D:public B,public Cprotected: int x; int y;public:D (int

10、a1,int b1,int c1,int a2,int b2,int c2,int a,int b): B(a1,b1,c1),C(a2,b2,c2),A(10,20) x=a;y=b;int getx( )return x;int gety( )return y; ;,组合,class PART1int x,y; public:double z1;PART1(int a,int b)x=a;y=b;void get()coutx“,“yendl; ;,class WHOLE PART1 a1; PART1 a2; PART2 a3; double x; public: PART2 a4; W

11、HOLE(int a,int b,int c,int d,int e,int f,double g,int h,int k): a1(a,b),a2(c,d),a3(e,f),a4(h,k)x=g;double getx()return x;void geta1()a1.get(); void geta2()a2.get();void geta3()a3.get(); ;,class PART2int x,y; public:double z2;PART2(int a,int b)x=a;y=b;void get()coutx“,“yendl; ;,多态与虚函数,void main()base b, *pb ;son_1 s1;son_2 s2, *ps2;b.who();s1.who();pb = ,纯虚函数与抽象类,class Shape private:ColorType Color; public:Shape(ColorType c)Color=c;/构造函数virtual void Draw()=0;/纯虚函数char *getcolor(); ;,Shape s1(Red);,Shape *s1;,

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 生活休闲 > 社会民生

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号