3.3 对象数组与对象指针

上传人:飞*** 文档编号:53694999 上传时间:2018-09-04 格式:PPT 页数:16 大小:181KB
返回 下载 相关 举报
3.3 对象数组与对象指针_第1页
第1页 / 共16页
3.3 对象数组与对象指针_第2页
第2页 / 共16页
3.3 对象数组与对象指针_第3页
第3页 / 共16页
3.3 对象数组与对象指针_第4页
第4页 / 共16页
3.3 对象数组与对象指针_第5页
第5页 / 共16页
点击查看更多>>
资源描述

《3.3 对象数组与对象指针》由会员分享,可在线阅读,更多相关《3.3 对象数组与对象指针(16页珍藏版)》请在金锄头文库上搜索。

1、3.3,对象数组与对象指针,3.3.1 对象数组,1.概念:若一个类有若干个对象,我们把这一系列对象用一个数组来存放,这个数组就叫对象数组。 2.一维对象数组定义格式: 类名 数组名下标表达式; 例:point p4;p1.print();p2.print(); 说明: 对象数组的元素是对象,不仅具有数据成员,还有成员函数。,在使用对象数组时只能访问单个数组元素(对象),即通过对象访问公有成员。,p-print();,3.3.1 对象数组,例3-3-1: #include class exam int x; public:void set_x(int n)x=n;int get_x()retu

2、rn x; ;,main() exam ob4; int i; for(i=0;i4;i+)obi.set_x(i); for(i=0;i4;i+)coutobi.get_x() ; coutendl; return 0;,程序运行结果:0 1 2 3,例3-3-2:通过初始化表为对象数组赋值 #include class exam int x; public:exam()x=123;cout“constructed x=123”endl;exam(int n)x=n; cout“constructed x=”xendl;int get_x()return x; ; main() exam o

3、b14=11,22,33,44; exam ob24=55,66; exam ob34=exam(11),exam(22),exam(33),exam(44); exam ob44=exam(55),exam(66);,3.3.1 对象数组,ob10=11;输出constructed x=11 ob11=22;输出constructed x=22 ob12=33;输出constructed x=33 ob13=44;输出constructed x=44,ob20=11;输出constructed x=55 ob21=22;输出constructed x=66 ob22=123;输出constr

4、ucted x=123 ob23=123;输出constructed x=123,ob30=11;输出constructed x=11 ob31=22;输出constructed x=22 ob32=33;输出constructed x=33 ob33=44;输出constructed x=44,ob40=55;输出constructed x=55 ob41=66;输出constructed x=66 ob42=123;输出constructed x=123 ob43=123;输出constructed x=123,int i; for(i=0;i4;i+)coutob1i.get_x() ;

5、coutendl;for(i=0;i4;i+)coutob2i.get_x() ;coutendl;for(i=0;i4;i+)coutob3i.get_x() ;coutendl;for(i=0;i4;i+)coutob4i.get_x() ;coutendl;return 0;,3.3.1 对象数组 例3-3-2续,11 22 33 44,55 66 123 123,11 22 33 44,55 66 123 123,3.3.1 对象数组 例3-3-2续,运行结果: constructed x=11 constructed x=22 constructed x=33 constructed

6、 x=44 constructed x=55 constructed x=66 constructed x=123 constructed x=123 constructed x=11 constructed x=22 constructed x=33 constructed x=44,constructed x=55 constructed x=66 constructed x=123 constructed x=123 constructed x=123 11 22 33 44 55 66 123 123 11 22 33 44 55 66 123 123,例3-3-3:二维数组对象操作

7、#include class exampleint x,y; public: example(int n,int m)x=n;y=m; example()cout“Destructor called.n“; int get_x()return x; int get_y()return y; ;,main() example op32=example(1,2),example(3,4),example(5,6),example(7,8), example(9,10),example(11,12) ;,3.3.1 对象数组,x y op00 1 2 op01 3 4 op10 5 6 op11 7

8、 8 op20 9 10 op21 11 12,3.3.1 对象数组 例3-3-3续,int i; for(i=0;i3;i+) coutopi0.get_x() ; coutopi0.get_y()“n“; coutopi1.get_x() ; coutopi1.get_y()“n“; return 0; ,运行结果: 1 24 5 681012 Destructor called. Destructor called. Destructor called. Destructor called. Destructor called. Destructor called.,调用析构函数 exa

9、mple() cout对象成员;,:,#include class exeint x; public:void set_a(int a)x=a;void show_a()coutshow_a();return 0;,/定义对象ob,对象指针p,/对象指针p指向对象ob,/用对象指针访问对象成员,3.3.2 对象指针 例3-3-4,4.用对象指针引用对象数组 例3-3-5:改写例3-3-4 main()exe ob2,*p;ob0.set_a(10);ob1.set_a(20);p=ob;p-show_a();p+;p-show_a();return 0;,/定义对象数组ob和对象指针p,/p指

10、向对象数组ob的第一个元素ob0,/p指向对象数组ob的第二个元素ob1,3.3.2 对象指针,3.3.3 this指针,提出问题:当定义若干个对象时,每个对象都有属于自己的数据成员,但成员函数的代码是共用的。那么,成员函数是如何辨别出当前调用自己的那个对象?,引例,#include class Test private:int x; public:Test(int x1)x=x1;void disp()cout“x=“xendl; ; main()Test a(1),b(2);cout“a:“;a.disp();cout“b:“;b.disp();return 0; ,this,a.disp

11、(); /coutxendl;,b.disp(); /coutxendl;,a.x,b.x,this,Test a(1),b(2);,3.3.3 this指针,成员函数和一般函数的本质区别:成员函数带有this指针。即某个对象调用成员函数时,系统把this指针指向该对象。 说明: this指针是一个const指针,不能在程序中改变他的值。 this指针是一个局部数据,它的作用域仅在一个对象的内部。 this指针可以显式使用。,例3-3-6: #include class Test private:int x; public:Test(int x1)x=x1;void disp()coutxendl; ; main()Test a(1),b(2),c(3);a.disp(); /输出this=a的地址 when x=1b.disp(); /输出this=b的地址 when x=2c.disp(); /输出this=c的地址 when x=3return 0; ,3.3.3 this指针,this指向的对象的地址,this指向的对象的x的值,例3-3-7: #include class sampleint x,y; public:sample(int i=0,int j=0)x=i;y=j;void copy(sample ,3.3.3 this指针,

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

当前位置:首页 > 行业资料 > 其它行业文档

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