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