首页 vc第二讲 this指针

vc第二讲 this指针

举报
开通vip

vc第二讲 this指针第二讲一构造函数例1.3.1//l_3_2.cpp:Definestheentrypointfortheconsoleapplication.#includeHstdafx.h"#includc#includeclassCPcrson(public:CPerson(char*stnfloath,floatw)//A:构造函数(strcpy(natne,str);height=h;weight=w;)CPerson(char*str)//B:构造函数(strcpy(name,str);ICPcrson(floath,...

vc第二讲  this指针
第二讲一构造函数例1.3.1//l_3_2.cpp:Definestheentrypointfortheconsoleapplication.#includeHstdafx.h"#includc#includeclassCPcrson(public:CPerson(char*stnfloath,floatw)//A:构造函数(strcpy(natne,str);height=h;weight=w;)CPerson(char*str)//B:构造函数(strcpy(name,str);ICPcrson(floath,floatw=120);//C:构造函数public:voidprint()("«weight«endl;coutvv"姓名:"vvnamevv、身高:Nvheightvv飞体重:Iprivate:charnamcl20];//姓名floatheight;//身高floatweight;//体重);CPerson::CPerson(floath.floatw)(height=h;weight=w;)intmain(intargc,char*argv[])|CPersonone("DING");one.print();CPersontwo(l70,130);two.print();CPersonthree("DING",170,130);three.piint();p->nan[e="Wngfang”;p->num=10123;p->sex='m';cout«p->name«endl«p->num«endl«p->sex«endl;printf("%d\n",sizeof(dd));printf("%d\n",sizeof(p->name));printf("%d\n'\sizcof(p->num));printf("%d\n",sizeof(p->sex));printf(',%x,%x,%x,%x\n",p,&(p->name),&(p->num),&(p->sex));//deletep;cout«endl«endl;student*p1;p1=newstudent;p1->name=",Wngfang";pl->num=IOI23;pl->sex-m';cout«pI->name«endl«pI->num«endl«pI->sex«endl;printf("%d\n",sizeof(dd));printf("%d\n",sizeof(p1->name));printf("%d\n'\sizcof(pl->num));prinlf("%d\n",sizeof(p1->sex));printf("%x,%x,%x,%x\n",p1,&(p1->name),&(pl->num),&(p1->sex));//deletepl;cout«endl«endl;student*p2;p2=newstudent;p2->namc="Wngfang";p2->num=10l23;p2->sex='m';cout«p2->name«endl«p2->num«endl«p2->sex«endl;printf("%d\n",sizeof(dd));printf("%d\n",sizeof(p2->name));printf("%d\n'\sizcof(p2->num));printf("%d\n",sizeof(p2->sex));printf("%x,%x,%x,%x\n",p2.&(p2->name),&(p2->num),&(p2->sex));//deletep2;return0;知识点:I从程序运行结果来看,p3的地址与p2的地址不相同,说明p2所指的存储空间没被释放。资料:sirlen所作的仅仅是一个计数器的工作,它从内存的某个位置(可以是字符串开头,中间某个位置,甚至是某个不确定的内存区域)开始扫描,直到碰到第一个字符串结束符'\0'为止,然后返回计数器值。川#include#includeintmain(void)(char*s=z,Go1denG1oba1View,z;printf(,,%shas%dchars/,,s,strlen(s));getchar();returnO;return0;DING-1.073?4e*008烫烫烫烫烫烫随烫烫烫DING身高:170anykeytocontinue.DING-1.073?4e*008烫烫烫烫烫烫随烫烫烫DING身高:170anykeytocontinue.-1.07374e+008体重:130运行结果:名名名殍姓姓体重:1?0130XLdIbJ知识点:1类名以大写字母C开头( 规范 编程规范下载gsp规范下载钢格栅规范下载警徽规范下载建设厅规范下载 性规定)如CPoinl。2头文件<string.h>可以写成〈string〉。3S(rcopy(x,y)等价于x=y;4构造函数重载。5成员函数可在类体中声明,在类体外定义。6类A的对象只能直接访问类A的公有成员。7初始化函数就是为对象的变量赋值。8在源程序中有变量x及其值,在目标函数中对应为变量x的地址及该地址所指存储空间中的值。9编译后,生成的目标函数中只有存储空间地址及对应存储空间的值。10头文件"stdafx.h"为构造consoleapplication类型的工程提供支持。11vioslream.h>头文件为构造命令cout提供支持。二this指针例1.3.2(this指针)//l_3_4.cpp:Definestheentrypointfortheconsoleapplication.#include"stdafx.h"#includc<iostrcam.h>classCPoint{public:CPoint(intx=0,inty=0){xPos=x;yPos=y;}public:voidCopy(CPointone)(*(his=one;//直接通过*this给调用者pt1赋值voidPrint()cout«"Point(,'«xPos<<,',"<classCPointpublic:CPoinl(inix=0,inty=0){xPos=x;yPos=y;}public:voidCopy(CPointone){cout«"CPointoneaddrcss:"«&onc«endl;coul«"onesize"«sizeof(one)«endl;*this=one;coul«"(hispointeraddress:"«this«endl«endl;}//直接通过this赋值voidPrint()(cout«"Point(',«xPos<<","<CPointoneaddress:0X0012FF14onesize8thispointeraddress:0x0012FF78Point<30,40>CPointoneaddress:0x0012FF14onesize8thispointeraddress:0x0012FF70point<50,60〉[Pressanykeytocontinue一、变量地址显示,变量的字节测试,指针变量,指针常量,指针常量pa所指地址存储空间的取值运算*pa.New.delete,例1.3.4#include"stdafx.h"#includc"stdio.h"intmain(intargc,char*argv[]){inta,b,c;a=l;b=4,c=6;int*pa,*pb,*pc;pa=&a;pb=&b;pc=&c;printf("%d,%d,%d\n",a,b,c);prinH^d^d^dH'/pa/pblpc);printf("%d,%d,%d\n",pa,pb,pc);printf("%x,%x,%x\n'\pa,pb,pc);printf("%d\n",sizeof(a));printf(”%x,%x,%x\n”,&a,&b,&c);return0;运行结果:|c\"C:\Docu>entsandSettings.・・日回1,4,61,4,61245052,1245048,124504412ff7c,12f£78,12ff74412ff?c,12ff78,12ff74■色2d■色2dPressanykeytocontinue二、结构体,结构体对象,结构体指针,动态分配结构体存储空间并返回首地址,测试结构体字节数,测试结构体中各变量的字节数,释放动态结构体存储空间。例#include"stdafx.h"#include"stdio.h"#inckide"string"usingnamespacestd;#include"iostreani"structstudent(stringname;intnum;charsex;}dd;intmain(intargc,char*argv[]){student*p;p=newstudent;p->name="Wngfang";p->nuin=10123;p->sex='m';cout«p->namc«cndl«p->num«cndl«p->scx«cndl;printf("%d\n",sizeof(dd));printf("%d\n",sizeof(p->name));printf("%d\n",sizeof(p->niim));printf("%d\n",sizeof(p->sex));printf("%x,%x,%x,%x\n",p,&(p->nanie),&(p->num),&.(p->sex));deletep;return0;因,C:\DocimentsandSettines\Administrator\nevDelete\nevPro2\Debug\...Ungfang10123m241641371d00,371ds0,371dl0.371dl4P1*essanykeytocontinue三、为结构体动态分配存储空间,释放存储空间,观察内在分配情况例1.3・6#include"stdafx.h"#include"stdio.h"#include"string"usingnamespacestd;#includcniostrcam"structstudent(stringname;intnum;charsex;}dd;intmain(intargc,char*argv[]){student*p;p=ncwstudent;p->name="Wngfang,';p->num=10123;p->sex='in';cout«p->name«endl«p->num«endl«p->sex«endl;printf("%d\n'\sizeof(dd));printf("%d\n",sizeof(p->name));printf("%d\n'\sizeof(p->num));printf("%d\n",sizeof(p->sex));printf("%x,%x,%x,%x\]i",p,&(p->naine),&(p->nuin),&(p->sex));deletep;cout«endl«endl;student*pI;p1=newstudent;p1->namc="Wngfang";pl->num=lOI23;pl->scx-m';cout«p1->name«endl«pI->num«endl«pI->sex«endl;printf("%d\n",sizeof(dd));printf("%d\n",sizeof(p1->name));printf("%d\n'\sizeof(pl->num));printf("%d\n",sizeof(p1->sex));printf("%x,%x,%x,%x\n",p1,&(p1->name),&(pl->num),&(pI->sex));deletepl;cout«endl«endl;student*p2;p2=newstudent;p2->name="Wngfang";p2->num=lOI23;p2->scx='m';cout«p2->name«endl«p2->num«endl«p2->sex«endl;printf("%d\n",sizeof(dd));printf("%d\n",sizeof(p2->name));printf("%d\n'\sizcof(p2->num));printf("%d\n';sizeof(p2->sex));printf("%x,%x,%x,%x\nn,p2,&(p2->name),&(p2->num),&(p2->sex));deletep2;return0;知识点:student*pl;//定义指针变量plpl=newstudent;//动态分配结构体存储student的•个对象的存储空间,并返回其首地址并赋给plpl->name="Wngfang";〃以指针访问结构体变量name并赋值。deletep;〃释放指针p所指的结构体对象的存储空间3从程序运行结果来看,p3的地址与p2的地址相同,说明p2所指的存储空间被释放了。例1.3.7#include"stdafx.hn#include"stdio.h"#include"string"usingnamespacestd;#includc"iostrcam"structstudent{stringname;intnum;charsex;}dd;intmain(intargc,char*argv[]){student*p;p=newstudent;
本文档为【vc第二讲 this指针】,请使用软件OFFICE或WPS软件打开。作品中的文字与图均可以修改和编辑, 图片更改请在作品中右键图片并更换,文字修改请直接点击文字进行修改,也可以新增和删除文档中的内容。
该文档来自用户分享,如有侵权行为请发邮件ishare@vip.sina.com联系网站客服,我们会及时删除。
[版权声明] 本站所有资料为用户分享产生,若发现您的权利被侵害,请联系客服邮件isharekefu@iask.cn,我们尽快处理。
本作品所展示的图片、画像、字体、音乐的版权可能需版权方额外授权,请谨慎使用。
网站提供的党政主题相关内容(国旗、国徽、党徽..)目的在于配合国家政策宣传,仅限个人学习分享使用,禁止用于任何广告和商用目的。
下载需要: 免费 已有0 人下载
最新资料
资料动态
专题动态
is_598372
暂无简介~
格式:doc
大小:152KB
软件:Word
页数:10
分类:
上传时间:2019-11-18
浏览量:3