




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、作业作业第4章习题一、选择题(1-16)二、填空题(1-7)三、简答题(1-5)四、程序分析题(1-2)五、程序设计题(1-3)第一到第四大题做在课本上,老师下周三随机抽查第五大题的程序请提交到http:/ 名胜名胜 人人 物品物品 动物动物 ,植物,植物 身边的对象布兰尼布兰尼朱丽叶朱丽叶顾客顾客姓名:朱丽叶姓名:朱丽叶年龄:年龄:28体重:体重:52千克千克操作:操作:购买商品购买商品姓名:布兰尼姓名:布兰尼职衔:收银员职衔:收银员年龄:年龄:35体重:体重:60千克千克操作:操作:收款收款打印账单打印账单对象的特征属性属性对象具有的各种特征每个对象的每个属性都拥有特定值 例如:布兰尼和朱
2、丽叶的体重不一样 60kg 布兰尼布兰尼属性属性 35岁岁收银员布兰尼收银员布兰尼 对象的特征方法方法对象执行的操作 打单打单 收银收银 方法方法 刷卡刷卡 收银员布兰尼收银员布兰尼对象的属性和方法列出尼古拉斯凯奇驾驶的这辆法拉利F360 Spider的属性和方法列出小狗对象的属性和方法属性:属性: 品牌:法拉利品牌:法拉利 型号:型号:F360 Spider 颜色:黄色颜色:黄色 价格:价格:380万元万元方法:方法: 发动发动 停止停止 加速加速属性:属性: 颜色:白色颜色:白色方法:方法: 叫叫 跑跑 吃吃封装对象同时具有属性和方法两项特性对象的属性和方法通常被封装封装在一起,共同体现事
3、物的特性, 二者相辅相承,不能分割谁看见过只有谁看见过只有“完好的零件和完好的零件和颜色颜色”而不能开而不能开动的汽车?动的汽车?从对象抽象出“类”抽取出下列对象的属性和方法的共同特征抽取出下列对象的属性和方法的共同特征 顾客顾客 类顾客类轿车类 类是模子,确定对象将会拥有的类是模子,确定对象将会拥有的特征(属性)和行为(方法)特征(属性)和行为(方法) 类是对象的类型类是对象的类型 不同于不同于int类型:具有方法类型:具有方法 各种口味的球状冰淇淋各种口味的球状冰淇淋 球状冰淇淋模子球状冰淇淋模子类和对象有什么区别呢? 类是抽象的概念,仅仅是模板,比如说:类是抽象的概念,仅仅是模板,比如说
4、:“人人” 对象是一个你能够看得到、摸得着的具体实体,比如:对象是一个你能够看得到、摸得着的具体实体,比如:“张丽张丽”张丽张丽王勇王勇刘晨刘晨“人人”类类特征(属性)特征(属性)年龄年龄体重体重行为(方法)行为(方法)衣衣食食住住行行面向对象(OO)的优点便于程序模拟现实世界中的实体 用“类”表示实体的特征和行为隐藏细节 对象的行为和属性被封装在类中,外界通过调用类的方法来获得,不需关注内部细节如何实现可重用 可以通过类的模板,创建多个类的对象例题声明一个银行账号类Account,该类有账号(id)、余额(balance)两个数据成员,有获取账号、获取余额、存款和取款的函数,以及必要的构造函
5、数。请按上述要求声明该银行账号类并在main函数中定义该类的多个对象,然后对它们进行存取款和查询余额的操作。#include #include /包含头文件命令包含头文件命令usingusing namespacenamespace std; std;/使用名字空间使用名字空间stdstd#include #include classclass AccountAccount publicpublic: : Account( Account(stringstring Id ); Id ); /构造函数构造函数 Account(Account(stringstring Id, Id, double
6、double b); b); /构造函数的重载构造函数的重载 stringstring getIdgetId(); (); /获取账号获取账号 doubledouble getBalancegetBalance(); (); /返回当前账户余额返回当前账户余额 voidvoid depositdeposit( (doubledouble amount); amount); /存款操作存款操作 boolbool withdrawwithdraw( (doubledouble amount); amount); /取款操作取款操作privateprivate: : stringstring id;
7、 id; /账号账号doubledouble balance; balance;/余额余额 ;Account:Account(Account:Account(stringstring Id ) Id ) /构造函数构造函数 id=Id; balance= id=Id; balance=0 0; ; Account:Account(Account:Account(stringstring Id, Id, doubledouble Balance) Balance) /构造函数构造函数 id=Id; balance=Balance; id=Id; balance=Balance; stringst
8、ring Account:getId() Account:getId() returnreturn id; id; doubledouble Account:getBalance() Account:getBalance() returnreturn balance; balance; voidvoid Account:deposit(Account:deposit(doubledouble amount) amount)/存款操作存款操作 balance+=balance+=amount;amount; coutcout 存款成功!存款成功! endl;endl; cout cout 存款金
9、额:存款金额: amountendlamountamount)( balanceamount)/如果余额大于取款金额如果余额大于取款金额 balance-=amount; balance-=amount; cout cout 取款成功!取款成功! endl;endl; cout cout 取款金额:取款金额: amountendl;amountendl;returnreturn truetrue; ; elseelse/如果余额小于取款金额如果余额小于取款金额 cout cout 余额不足!余额不足! endl; endl; returnreturn falsefalse; ; intint
10、mainmain()() Account Account account1account1( (123456789123456789, ,20002000);); cout cout 当前账户信息:当前账户信息: endl;endl; cout cout 账号:账号: account1.getId()endl;account1.getId()endl; cout cout 余额:余额: account1.getBalance()endlendl;account1.getBalance()endlendl; account1.deposit( account1.deposit(200200);)
11、; cout cout 余额:余额: account1.getBalance()endlendl;account1.getBalance()endlendl; account1.withdraw( account1.withdraw(100100);); cout cout 余额:余额: account1.getBalance()endlendl;account1.getBalance()endlendl; Account Account account2account2( (987654321987654321);); cout cout 当前账户信息:当前账户信息: endlendl;en
12、dlendl; cout cout 账号:账号: account2.getId()endlendl;account2.getId()endlendl; cout cout 余额:余额: account2.getBalance()endlendl;account2.getBalance()endlendl; account2.withdraw( account2.withdraw(100100);); cout cout 余额:余额: account2.getBalance()endlendl;account2.getBalance()endlendl; account2.deposit( ac
13、count2.deposit(200200);); cout cout 余额:余额: account2.getBalance()endlendl;account2.getBalance()endlendl; account2.withdraw( account2.withdraw(100100);); cout cout 余额:余额: account2.getBalance()account2.getBalance()length=source.length; this-width=source.width; this-height=source.height; return *this; 例
14、如:Box a,b(10); a=b;.2 对象的拷贝在创建对象时使用已有对象快速拷贝出完全相同的对象对象拷贝的一般形式 类名类名 对象对象2(2(对象对象1);1); 类名类名 对象对象2 = 2 = 对象对象1;1;拷贝构造函数只有一个形参,这个形参就是本类对象的引用拷贝构造函数的代码主要是将形参中对象引用的各数据成员值赋给自己的数据成员Box(const Box &c) /Box类类的的拷贝拷贝构造构造函数函数4.5.2 对象的复制以以Box类为例,看看复制构造函数的形式:类为例,看看复制构造函数的形式:Box:Box(const Box &c) /Box类类的的拷贝拷贝
15、构造构造函数函数 length =c.length; width = c.width; height = c.height; #include #include usingusing namespacenamespace std; std;classclass BoxBox publicpublic: : Box() Box()/无参数的构造函数无参数的构造函数 coutcout 无参数的构造函数被调用!无参数的构造函数被调用! endlendl; ; Box( Box(floatfloat L, L,floatfloat w, w,floatfloat h) h) /带有参数的构造函数带有参
16、数的构造函数 length =L; width = w; height = h; length =L; width = w; height = h; cout cout 有参数的构造函数被调用!有参数的构造函数被调用! endlendl; ; Box( Box(constconst Box &c) Box &c) /Box/Box类类的拷贝构造的拷贝构造函数函数 length =c.length; length =c.length; width = c.width; width = c.width; height = c.height; height = c.height; coutcoutC
17、opy Copy 构造函数被调用!构造函数被调用! endlendl; ; floatfloat VolumeVolume() () returnreturn length length * * width width * * height; height; privateprivate: : floatfloat length,width,height length,width,height; ; ;intint mainmain() () Box Box box1box1( (4 4, ,2 2, ,3 3);); Box box2(box1); Box box2(box1); Box b
18、ox3 Box box3; ; box3=box1; box3=box1; cout coutbox1box1的体积为:的体积为: box1.Volume()endl;box1.Volume()endl; cout coutbox2box2的体积为:的体积为: box2.Volume()endl;box2.Volume()endl; cout coutbox3box3的体积为:的体积为: box3.Volume()endl;box3.Volume()endl; returnreturn 0 0;四种不同的构造对象的方法4.5.2 对象的拷贝在没有涉及指针类型的数据成员时,默认拷贝构造函数能够很
19、好地工作。当一个类有指针类型的数据成员时,默认拷贝构造函数常会产生问题。【例4-8】默认拷贝构造函数引起的问题#include #include #include #include usingusing namespacenamespace std; std;classclass BoxBox publicpublic: : Box(); Box(); Box( Box(charchar * *Name,Name,floatfloat L, L,floatfloat w, w,floatfloat h); h); Box operator=(const Box &source Box oper
20、ator=(const Box &source);/);/赋值运算符重载赋值运算符重载 charchar * * getName(); getName();/获取名字获取名字 voidvoid setName( setName(charchar * *Name);Name);/设置名字设置名字 floatfloat Volume(); Volume();/计算体积计算体积 Box();Box();privateprivate: :floatfloat length,width,height; length,width,height;charchar * *name;name;增加了Name数据
21、程序BoxBox:Box()Box()/无参数的构造函数无参数的构造函数 namename= =newnew charchar 10+110+1; length length =0=0; width ; width = = 0 0; height ; height = = 0 0; ; cout cout 无参数的构造函数被调用!无参数的构造函数被调用! endl;endl; BoxBox:Box(Box(charchar * *Name,Name,floatfloat L, L,floatfloat w, w,floatfloat h) h) /带有参数带有参数的构造函数的构造函数 name
22、name= =newnew charcharstrlen(Name)strlen(Name)+1+1; strcpy(name,Name); strcpy(name,Name); length length = =L; width L; width = = w; height w; height = = h; h; cout cout 有参数的构造函数被调用!有参数的构造函数被调用! endl;endl; /赋值构造函数赋值构造函数, ,赋值运算符重载,可以省略赋值运算符重载,可以省略/若省略系统会调用默认的赋值运算符重载函数若省略系统会调用默认的赋值运算符重载函数 Box BoxBox Bo
23、x:operatoroperator= =( (constconst Box Box & &source) source) length length= =source.length;source.length; width width= =source.width;source.width; height height= =source.height;source.height; strcpy( name,); strcpy( name,); cout cout 赋值构造函数被调用赋值构造函数被调用 endl;endl; returnreturn
24、* *thisthis; ; charchar * * Box Box: getName() getName() returnreturn name; name; voidvoid Box Box:setName(setName(charchar * *Name)Name) strcpy(name,Name); strcpy(name,Name);floatfloat Box Box:Volume() Volume() returnreturn length length * * width width * * height; height; Box Box:Box() Box() intin
25、t main() main() Box box1( Box box1( 长方体长方体1 1号号 , ,4 4, ,2 2, ,3 3);); Box box2(box1Box box2(box1); ); /调用默认的拷贝构造函数调用默认的拷贝构造函数 Box box3; Box box3; box3 box3= =box1;box1; box2.setName( box2.setName( 长方体长方体2 2号号 );); box3.setName(box3.setName( 长方体长方体3 3号号 );); coutcoutbox1.getName()box1.getName()box1.
26、Volume()box1.Volume()endl;endl; cout coutbox2.getName()box2.getName()box2.Volume()box2.Volume()endl;endl; cout coutbox3.getName()box3.getName()box3.Volume()box3.Volume()endl;endl; returnreturn 0 0;长方体1的Name也被改变了nameheight3box2nameheight3box1长方体1号namenameBox box1(Box box1( 长方体长方体1 1号号 , ,4 4, ,2 2, ,
27、3 3););Box Box box2(box1); box2(box1); /调用默认的拷贝构造函数调用默认的拷贝构造函数nameheight3box2nameheight3box1长方体2号namename解决的方法:重写解决的方法:重写拷贝构造函数拷贝构造函数Box box1(长方体1号,4,2,3);Box box2(box1); /调用默认的拷贝构造调用默认的拷贝构造函数函数 box2.setName(box2.setName(长方体长方体2 2号号););#include #include #include #include usingusing namespacenamespac
28、e std; std;classclass BoxBox publicpublic: : Box(); Box(); Box( Box(charchar * *Name,Name,floatfloat L, L,floatfloat w, w,floatfloat h h);); Box (const Box &c); /Box类的拷贝构造函数 Box operator=(const Box &source); Box operator=(const Box &source); charchar * * getName(); getName();/获取名字获取名字 voidvoid setNa
29、me( setName(charchar * *Name);Name);/设置名字设置名字 floatfloat Volume(); Volume();/计算体积计算体积 Box();Box();privateprivate: :floatfloat length,width,height; length,width,height;charchar * *name;name;增加了拷贝构造函数【例例4-9】 自定义拷贝构造函数自定义拷贝构造函数BoxBox:Box()Box()/无参数的构造函数无参数的构造函数 namename= =newnew charchar 10+110+1; leng
30、th length =0=0; width ; width = = 0 0; height ; height = = 0 0; ; cout cout 无参数的构造函数被调用!无参数的构造函数被调用! endl;endl; BoxBox:Box(Box(charchar * *Name,Name,floatfloat L, L,floatfloat w, w,floatfloat h h) ) /带有参带有参数的构造数的构造函数函数 namename= =newnew charcharstrlen(Name)strlen(Name)+1+1; strcpy(name,Name); strcpy
31、(name,Name); length length = =L; width L; width = = w; height w; height = = h; h; cout cout 有参数的构造函数被调用!有参数的构造函数被调用! endlendl; ; Box Box:Box (Box (constconst Box Box & &c) c) /Box/Box类的复制构造函数类的复制构造函数 name name= =newnew charcharstrlen()strlen()+1+1; strcpy( name,); strcpy( name,c.na
32、me); length length = =c.length; c.length; width width = = c.width; c.width; height height = = c.height; c.height; cout cout 复制构造函数被调用!复制构造函数被调用! endlendl; ; 增加了拷贝构造函数/赋值构造函数赋值构造函数, ,赋值运算符重载,可以省略赋值运算符重载,可以省略/若省略系统会调用默认的赋值运算符重载函数若省略系统会调用默认的赋值运算符重载函数 Box BoxBox Box:operatoroperator= =( (constconst Box
33、Box & &source) source) length length= =source.length;source.length; width width= =source.width;source.width; height height= =source.height;source.height; strcpy( name,); strcpy( name,); cout cout 赋值构造函数被调用赋值构造函数被调用 endl;endl; returnreturn * *thisthis; ; charchar * * Box Box: ge
34、tName() getName() returnreturn name; name; voidvoid Box Box:setName(setName(charchar * *Name)Name) strcpy(name,Name); strcpy(name,Name);floatfloat Box Box:Volume() Volume() returnreturn length length * * width width * * height; height; Box:Box() Box:Box() delete name;name=NULLdelete name;name=NULL;
35、; 析构函数将动态申请的空间释放intint main() main() Box box1( Box box1( 长方体长方体1 1号号 , ,4 4, ,2 2, ,3 3);); Box box2(box1Box box2(box1); ); /调用调用自定义自定义的拷贝构造函数的拷贝构造函数 Box box3; Box box3; box3 box3= =box1;box1; box2.setName( box2.setName( 长方体长方体2 2号号 );); box3.setName(box3.setName( 长方体长方体3 3号号 );); coutcoutbox1.getNa
36、me()box1.getName()box1.Volume()box1.Volume()endl;endl; cout coutbox2.getName()box2.getName()box2.Volume()box2.Volume()endl;endl; cout coutbox3.getName()box3.getName()box3.Volume()box3.Volume()endl;endl; returnreturn 0 0;长方体长方体1的的Name没有没有被修改被修改内存里的情况内存里的情况4.5.3 对象的赋值与复制的比较相同点主要有:相同点主要有:(1)对象的赋值)对象的赋值
37、和和拷贝拷贝大部分大部分情况下都是把一个对象的情况下都是把一个对象的数据成员依次赋给另外一个同类对象的相应数据成员。数据成员依次赋给另外一个同类对象的相应数据成员。(2)如果不重载赋值运算符或不)如果不重载赋值运算符或不提供提供拷贝拷贝构造构造函数,系函数,系统都可以提供缺省的代码。统都可以提供缺省的代码。(3)系统会根据情况自动的调用对象的赋值运算符重载)系统会根据情况自动的调用对象的赋值运算符重载函数或对象函数或对象的的拷贝拷贝构造构造函数。函数。不同点主要有:不同点主要有:(1)对象的赋值是在两个对象都已经创建的基础上)对象的赋值是在两个对象都已经创建的基础上进行的;而对象进行的;而对象
38、的的拷贝拷贝则则在用一个已有在用一个已有对象对象拷贝拷贝一一个个新对象时进行的。新对象时进行的。(2)它们两个所对应调用的函数不同,对象的赋值)它们两个所对应调用的函数不同,对象的赋值系统调用的是赋值运算符重载函数;而对象系统调用的是赋值运算符重载函数;而对象的的拷贝拷贝系系统统调用的调用的是是拷贝拷贝构造构造函数。函数。4.5.3 对象的赋值与复制的比较 对于AB类,其拷贝构造函数的原型为_。A) AB:AB();B) AB:AB(AB);C) AB:AB(AB &);D) AB:AB(const AB &);提交单选题ABCD依据已存在的对象建立一个新的对象时,会自动调用该类的_。A) 构
39、造函数B) 赋值构造函数C) 拷贝构造函数D) 析构函数提交单选题ABCD类与对象类的声明和对象的定义类的成员函数对象成员的访问构造函数和析构函数对象的赋值和拷贝对象对象数组数组指向对象的指针向函数传递对象对象与const静态成员与友元【例例4-10】创建含有创建含有3个长方体元素的长方体数组,个长方体元素的长方体数组,并显示长方体构造函数的调用情况。并显示长方体构造函数的调用情况。4.6.1 对象数组#include #include #include #include usingusing namespacenamespace std; std;classclass BoxBox publ
40、icpublic: : BoxBox( ) ( ) /无参数的构造函数无参数的构造函数 length length = = 1 1; width ; width = = 1 1; height ; height = = 1 1; ; cout cout Box(Box( length length , , width width , , height height ); ; cout cout is constructed! is constructed! endl; endl; Box(Box(floatfloat L, L, floatfloat W, W, floatfloat H H)
41、 ) /带有带有3 3个形参的构造函数个形参的构造函数 length length = =L; width L; width = = W; height W; height = = H; H; cout cout Box(Box( length length , , width width , , height height ); ; cout cout is constructed! is constructed! endl; endl; void setValue(void setValue(floatfloat L, L, floatfloat W, W, floatfloat H H)
42、 ) /给数据成员赋值给数据成员赋值 length length = =L; width L; width = = W; height W; height = = H H;floatfloat Volume( ) Volume( ) returnreturn length length* *widthwidth* *heightheight; ; /求体积求体积 Box( )Box( ) cout cout Box(Box( length length , , width width , , height height ); ; cout cout is destructed! is dest
43、ructed! endl; endl; privateprivate: : floatfloat length, width, height; length, width, height; ; ;intint main( ) main( ) Box Box boxsboxs3 3;/创建含有创建含有3 3个元素的对象数组个元素的对象数组boxsboxs forfor ( (intint i i=0=0;i;i33;i;i+) ) boxsi.setValue(i,i boxsi.setValue(i,i+10+10,i,i+20+20);); forfor ( i ( i=0=0;i;i33;
44、i;i+) ) cout coutboxsboxsi i 的体积为:的体积为: boxsi.Volume()boxsi.Volume()endl;endl; return return 0 0;4.6.2 对象数组的初始化对象数组在建立时还可以给出实参以实现对数组元素进行不同的初始化 类有只有一个参数一个参数的构造函数,可以使用如下形式的数组初始化形式: Box boxs3 = 10,20,30; 如果对象的构造函数需要多个参数,则在初始化的花括号里要分别写明构造函数,并指定实参 【例例4-11】定义对象数组并初始化,观察对象数组定义对象数组并初始化,观察对象数组建立的情况。建立的情况。4.6
45、.2 对象数组的初始化#include #include #include #include usingusing namespacenamespace std; std;classclass BoxBox publicpublic: : BoxBox( ) ( ) /无参数的构造函数无参数的构造函数 length length = = 1 1; width ; width = = 1 1; height ; height = = 1 1; ; cout cout Box(Box( length length , , width width , , height height ); ; cou
46、t cout is constructed! is constructed! endl; endl; Box(Box(floatfloat L, L, floatfloat W, W, floatfloat H H) ) /带有带有3 3个形参的构造函数个形参的构造函数 length length = =L; width L; width = = W; height W; height = = H; H; cout cout Box(Box( length length , , width width , , height height ); ; cout cout is constructe
47、d! is constructed! endl; endl; void setValue(void setValue(floatfloat L, L, floatfloat W, W, floatfloat H) H) length length = =L; width L; width = = W; height W; height = = H; H;floatfloat Volume( ) Volume( ) returnreturn length length* *widthwidth* *height; height; Box( )Box( ) cout cout Box(Box( l
48、ength length , , width width , , height height ); ; cout cout is destructed! is destructed! endl; endl; privateprivate: : floatfloat length, width, height; length, width, height; ; ;intint main() main() Box boxsBox boxs3 3 = = Box(Box(1 1, ,3 3, ,5 5),Box(),Box(2 2, ,4 4, ,6 6),Box(),Box(3 3, ,6 6,
49、,9 9) ) ; ; /创建含有三个元素的对象数组创建含有三个元素的对象数组boxsboxs forfor( (intint i i=0=0;i;i33;i;i+) ) cout coutboxsboxsi i的体积的体积为为 boxsi.Volume()boxsi.Volume()endl;endl; returnreturn 0 0;int main() Box boxs3= Box(1,3,5), Box(),Box(3,6,9); /创建含有三个元素的对象数组创建含有三个元素的对象数组boxs for(int i=0;i3;i+) coutboxsi的体积为的体积为boxsi.Vol
50、ume()endl; return 0;采用默认构造函数来构造对象#include #include usingusing namespacenamespace std; std;classclass TestTest publicpublic: : Test(); Test();/默认构造函数默认构造函数 Test(Test(intint n); n); /带一个参数构造函数带一个参数构造函数 privateprivate: : intint num num; ; ;TestTest:Test()Test()coutcoutInit defaInit defaendl;endl;numnum
51、=0=0; ; TestTest:Test(Test(intint n) n) coutcoutInitInit n nendl;endl;numnum= =n;n;intint main() main()Test xTest x2 2; ; /语句语句1 1Test y(Test y(1515); ); /语句语句2 2returnreturn 0 0; ; 写出程序的输出结果,并分析结果写出程序的输出结果,并分析结果对象数组的动态创建与释放#include #include usingusing namespacenamespace std; std;classclass TestTest
52、 publicpublic: : Test(); Test();/默认构造函数默认构造函数 Test(Test(intint n); n); /带一个参数构造函数带一个参数构造函数 set_num(set_num(intint Num)num Num)num= =Num;Num; get_num() get_num()returnreturn num num; Test()coutObject is destructing!endl; Test()coutObject is destructing!endl;privateprivate: : intint num; ; num; ;TestT
53、est:Test()Test()coutcout 调用默认构造函数调用默认构造函数 endl;endl;numnum=0=0; ; TestTest:Test(Test(intint n) n) coutcout 调用带参数的构造函数调用带参数的构造函数 n nendl;endl;numnum= =n;n;intint main() main()coutcout 用用newnew动态创建一个对象动态创建一个对象, ,此对象由默认构造函数初此对象由默认构造函数初始化始化 endl;endl;Test Test * *p p= =newnew Test; Test; cout cout 用用new
54、new动态创建一个对象动态创建一个对象, ,此对象由有参数的构造函数此对象由有参数的构造函数初始化初始化 endl;endl;Test Test * *q q= =newnew Test( Test(1010););coutcout 用用newnew动态创建一个有动态创建一个有5 5个对象元素组成的对象数组个对象元素组成的对象数组, ,数组中的对象由默认构造函数初始化数组中的对象由默认构造函数初始化 endl;endl;Test Test * *s s= =newnew Test Test5 5;/Test /Test * *t=new Test5(10); t=new Test5(10);
55、错误错误intint i; i;coutcout 调用对象数组中每个对象的调用对象数组中每个对象的set_numset_num成员函数成员函数 endl;endl;Test Test * *s2s2= =s;s;forfor(i(i=0=0;i;i5-set_num(i);set_num(i); s2 s2+; ; coutcout 调用对象数组中每个对象的调用对象数组中每个对象的get_numget_num成员函数成员函数 endl;endl;forfor(i(i=0=0;i;i55;i;i+) )coutcout 第第 i i+1+1 个对象的个对象的num=num=si.get_num(
56、)si.get_num()”操作符。如:box1.Volume( ); box1.Volume( ); 与与q-Volume() ;q-Volume() ;等价xobobset_x(a)show_x()2 2?p p&ob运行结果:22 2【例例4-124-12】用指针引用单个对象的成员#include #include classclass exeexe publicpublic: :voidvoid set_x( set_x(intint a) xa) x= =a;a;voidvoid show_x() show_x()coutcoutx x-show_x();show_x(); 对象的动
57、态创建和释放使用new和delete运算符实现对对象的动态创建与释放 在程序执行到new语句时,系统会从内存堆中分配一块内存空间,存放类的对象,调用构造函数初始化对象。如果内存分配成功,返回分配的内存的首地址;如果分配内存失败,则会返回一个NULL。Box *pc=new Box;对象的动态创建和释放可以在使用new运算符创建对象时给出实参,调用带有参数的构造函数初始化对象 如:Box Box * *pc = new Box(2,2,2); pc = new Box(2,2,2); 为了保险起见,在使用对象指针之前一般先判断指针的值是否为NULL 。如下所示: Box Box * *pc =
58、new Box(2,2,2);pc = new Box(2,2,2); if(pc!=NULL)if(pc!=NULL) pc-Volume(); pc-Volume(); 对象的动态创建和释放使用delete运算符释放该对象 delete运算符的使用格式是: delete delete 指针变量名指针变量名; ;如:如: delete pc;delete pc; 通过new运算符动态创建的对象只能通过delete运算符动态的释放 #include #include #include #include usingusing namespacenamespace std; std;classcl
59、ass C C string string namename; ; /私有的数据成员私有的数据成员publicpublic: :C()C()namename= =anonymousanonymous; ; coutcoutnamename Constructing. Constructing.endlendl; ; C(C(constconst charchar * *n)n)namename= =n;n; coutcoutnamename Constructing. Constructing.endlendl; ; C()C()coutcoutnamename Destructing. De
60、structing.endlendl; ; ;intint main main()() C C c0(c0(hortensehortense);); C c1C c1; ; C C c2(c2(foofoo);); C C * *ptrptr= =newnew C(); C();deletedelete ptr;ptr; return return 0 0;给出此程序的输出结果给出此程序的输出结果输出结果:输出结果:hortense Constructing.anonymous Constructing.foo Constructing.foo Destructing.anonymous De
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 电铲初级工练习题(附参考答案)
- 2025员工拒绝签订劳动合同企业应对策略全解析
- 商业信息咨询和辅导服务协议规定事项
- 智能硬件产品设计与制造合同协议
- 知识产权转让合同协议书要求专业版
- 设备采购合同协议条款
- 经济师专业试题及答案
- 2025湖南省低空经济发展集团有限公司招聘12人(第二次)笔试参考题库附带答案详解
- 2025江西南昌市信阳鼎信产业投资集团有限公司及所属二级公司招聘24人笔试参考题库附带答案详解
- 2025广西旅发大健康产业集团有限公司招聘278人笔试参考题库附带答案详解
- 中小学学生规范汉字书写比赛硬笔格式
- 跳绳市场调研报告
- 《大学生的情绪》课件
- 铁道概论(第八版)佟立本主编
- 全国各省市名称大全
- 202305青少年软件编程(图形化)等级考试试卷四级(含答案)
- 光储充车棚技术方案设计方案
- 土壤重金属源调查分析投标方案
- 植筋锚固深度表
- 幼儿园家长会会议记录三篇
- 《心房颤动诊断和治疗中国指南2023》解读
评论
0/150
提交评论