欢迎来到天天文库
浏览记录
ID:59008489
大小:101.50 KB
页数:61页
时间:2020-09-26
《类和动态内存分配ppt课件.ppt》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、第7章类和动态内存分配提要本章介绍对类使用动态分配技术,以及由此引起的问题的处理。动态内存的使用将影响构造函数,析构函数的设计和操作符的重载7.1动态内存和类一个使用动态内存的例子classStringBad{private:char*str;//pointertostringintlen;//lengthofstringstaticintnum_strings;//numberofobjectspublic:StringBad(constchar*s);//constructorStringBa
2、d();//defaultconstructor~StringBad();//destructor//friendfunctionfriendstd::ostream&operator<<(std::ostream&os,constStringBad&st);};以下是其实现(stringbad.cpp)静态类成员的初始化intStringBad::num_strings=0;///注意:静态类成员位于静态存储区,并不是类对象的组成部分;在类声明中声明,不可初始化(静态const整型或枚举除外除外
3、);在类方法实现文件中初始化。使用类型名和定义域限制操作符,不用关键字static。然后是构造函数StringBad::StringBad(constchar*s){len=std::strlen(s);//setsizestr=newchar[len+1];//allotstoragestd::strcpy(str,s);//initializepointernum_strings++;//setobjectcountcout<4、tcreated";//ForYourInformation}注意:字符串并没有保存在对象中,而是在单独的堆内存中。对象中存储字符串的地址。析构函数StringBad::~StringBad()//necessarydestructor{cout<<"""<5、的。在构造函数中用new分配内存,必须在析构函数中用delete释放内存文件vegnews.cpp是使用上述类的主程序。其中定义了两个函数:一个传引用作参数,一个传值作参数voidcallme1(StringBad&rsb){cout<<"Stringpassedbyreference:";cout<<"""<6、;}1:"CeleryStalksatMidnight"objectcreated2:"LettucePrey"objectcreated3:"SpinachLeavesBowlforDollars"objectcreatedheadline1:CeleryStalksatMidnightheadline2:LettucePreysports:SpinachLeavesBowlforDollarsStringpassedbyreference:"CeleryStalksatMidnight"hea7、dline1:CeleryStalksatMidnightStringpassedbyvalue:"LettucePrey""LettucePrey"objectdeleted,2leftheadline2:葺葺葺葺葺葺葺葺葺葺葺葺葺葺葺葺Initializeoneobjecttoanother:sailor:SpinachLeavesBowlforDollarsAssignoneobjecttoanother:3:"C++"defaultobjectcreatedknot:CeleryStalk8、satMidnightEndofmain()"CeleryStalksatMidnight"objectdeleted,2left"SpinachLeavesBowlforDollars"objectdeleted,1left"葺葺葺葺葺葺葺葺葺葺葺葺葺葺葺葺葺葺葺葺?objectdeleted,0left未处理的异常:System.NullReferenceException:未将对象引用设置到对象的实例。atdelete[](Void*)atStringBad.__dtor(S
4、tcreated";//ForYourInformation}注意:字符串并没有保存在对象中,而是在单独的堆内存中。对象中存储字符串的地址。析构函数StringBad::~StringBad()//necessarydestructor{cout<<"""<5、的。在构造函数中用new分配内存,必须在析构函数中用delete释放内存文件vegnews.cpp是使用上述类的主程序。其中定义了两个函数:一个传引用作参数,一个传值作参数voidcallme1(StringBad&rsb){cout<<"Stringpassedbyreference:";cout<<"""<6、;}1:"CeleryStalksatMidnight"objectcreated2:"LettucePrey"objectcreated3:"SpinachLeavesBowlforDollars"objectcreatedheadline1:CeleryStalksatMidnightheadline2:LettucePreysports:SpinachLeavesBowlforDollarsStringpassedbyreference:"CeleryStalksatMidnight"hea7、dline1:CeleryStalksatMidnightStringpassedbyvalue:"LettucePrey""LettucePrey"objectdeleted,2leftheadline2:葺葺葺葺葺葺葺葺葺葺葺葺葺葺葺葺Initializeoneobjecttoanother:sailor:SpinachLeavesBowlforDollarsAssignoneobjecttoanother:3:"C++"defaultobjectcreatedknot:CeleryStalk8、satMidnightEndofmain()"CeleryStalksatMidnight"objectdeleted,2left"SpinachLeavesBowlforDollars"objectdeleted,1left"葺葺葺葺葺葺葺葺葺葺葺葺葺葺葺葺葺葺葺葺?objectdeleted,0left未处理的异常:System.NullReferenceException:未将对象引用设置到对象的实例。atdelete[](Void*)atStringBad.__dtor(S
5、的。在构造函数中用new分配内存,必须在析构函数中用delete释放内存文件vegnews.cpp是使用上述类的主程序。其中定义了两个函数:一个传引用作参数,一个传值作参数voidcallme1(StringBad&rsb){cout<<"Stringpassedbyreference:";cout<<"""<6、;}1:"CeleryStalksatMidnight"objectcreated2:"LettucePrey"objectcreated3:"SpinachLeavesBowlforDollars"objectcreatedheadline1:CeleryStalksatMidnightheadline2:LettucePreysports:SpinachLeavesBowlforDollarsStringpassedbyreference:"CeleryStalksatMidnight"hea7、dline1:CeleryStalksatMidnightStringpassedbyvalue:"LettucePrey""LettucePrey"objectdeleted,2leftheadline2:葺葺葺葺葺葺葺葺葺葺葺葺葺葺葺葺Initializeoneobjecttoanother:sailor:SpinachLeavesBowlforDollarsAssignoneobjecttoanother:3:"C++"defaultobjectcreatedknot:CeleryStalk8、satMidnightEndofmain()"CeleryStalksatMidnight"objectdeleted,2left"SpinachLeavesBowlforDollars"objectdeleted,1left"葺葺葺葺葺葺葺葺葺葺葺葺葺葺葺葺葺葺葺葺?objectdeleted,0left未处理的异常:System.NullReferenceException:未将对象引用设置到对象的实例。atdelete[](Void*)atStringBad.__dtor(S
6、;}1:"CeleryStalksatMidnight"objectcreated2:"LettucePrey"objectcreated3:"SpinachLeavesBowlforDollars"objectcreatedheadline1:CeleryStalksatMidnightheadline2:LettucePreysports:SpinachLeavesBowlforDollarsStringpassedbyreference:"CeleryStalksatMidnight"hea
7、dline1:CeleryStalksatMidnightStringpassedbyvalue:"LettucePrey""LettucePrey"objectdeleted,2leftheadline2:葺葺葺葺葺葺葺葺葺葺葺葺葺葺葺葺Initializeoneobjecttoanother:sailor:SpinachLeavesBowlforDollarsAssignoneobjecttoanother:3:"C++"defaultobjectcreatedknot:CeleryStalk
8、satMidnightEndofmain()"CeleryStalksatMidnight"objectdeleted,2left"SpinachLeavesBowlforDollars"objectdeleted,1left"葺葺葺葺葺葺葺葺葺葺葺葺葺葺葺葺葺葺葺葺?objectdeleted,0left未处理的异常:System.NullReferenceException:未将对象引用设置到对象的实例。atdelete[](Void*)atStringBad.__dtor(S
此文档下载收益归作者所有