77范文网 - 专业文章范例文档资料分享平台

面向对象程序设计课后习题答案(4)

来源:网络收集 时间:2019-01-07 下载这篇文档 手机版
说明:文章内容仅供预览,部分内容可能不全,需要完整文档或者需要复制内容,请下载word后使用。下载word有问题请添加微信号:或QQ: 处理(尽可能给您提供完整文档),感谢您的支持与谅解。点击这里给我发消息

void main()

{ CTest fri,fri1;

fri.friend_f(fri); //错误,友元函数不是成员函数,所以不能用对象.函数名调 //用友元函数 friend_f(fri1); }

[4_11]答:

#include #include class CTest { public: CTest()

{ x=20; } void use_this(); private: int x; };

void CTest::use_this() { CTest y,*pointer;

this=&y; //错误,不能对this直接赋值

*this.x=10; //错误,按优先级原句的含义是*(this.x)=10,显然不对,正确的写 //法是(*this).x=10;或this->x=10; pointer=this; pointer=&y; }

void main() { CTest y;

this->x=235; //错误,this的引用不能在外部函数中,只能在内部函数中。 }

[4_12]答:运行结果是: 10,20 30,48 50,68 70,80 90,16 11,120

[4_13]答:运行结果是: Constructing 10

Destructing. 100

Destructing

[4_14]答:运行结果是: 3 objects in existence

4 objects in existence after allocation 3 objects in existence after deletion

说明:这个程序使用静态数据成员追踪记载创建对象的个数。完成这一工作的方 法就是每创建一个对象就调用构造函数一次。每调用构造函数一次,静态 数据成员total就增加1,每撤消一个对象就调用析构函数一次。每调用析 构函数一次,静态数据成员total就减少1。 [4_15] 运行结果是: Here’s the program output. Let’s generate some stuff… Counting at 0 Counting at 1 Counting at 2 Counting at 3 Counting at 4 Counting at 5 Counting at 6 Counting at 7 Counting at 8 Counting at 9

说明:在程序中main()只包括了一个return语句,但竟然有内容输出!什么时候

调用了构造函数?构造函数在对象被定义时调用。那么对象anObject是何时被调用的呢?是在main()之前,语句”test anObject”处。因此,anObject的构造函数是先于main()被调用的。在main()之前的所有全局变量都是在main()开始之前就建立了的。应该尽可能避免使用

全局变量,因为全局变量有可能引起名称冲突,使程序的执行结果和预想的不一样。 [4_16] [4_17]构建一个类book,其中含有2个私有数据成员qu和price,建立一 个有5个元素的数组对象,将qu初始化为1~5,将price初始化为qu的10 倍。显示每个对象的qu*price 答案见下: #include class book { public:

book(int a,int b)

{ qu=a; price=b; } void show_money()

{ cout<

int qu,price; }; main()

{ book ob[5]={ book(1,10),book(2,20),

book(3,30),book(4,40),book(5,50) }; //16题用下面语句 /*int i;

for(i=0;i<5;i++)

ob[i].show_money();

return 0;*/

//17题用下面的语句 int i; book *p; p=&ob[4]; for(i=0;i<5;i++)

{ p->show_money(); p--; }

return 0; }

[4_18]使用C++的 见书139页题 #include //#include class toy { public: toy(){ }

toy(int p,int c) { price=p; count=c; }

void input(int p,int c); void compute(); void print(); private:

int price; int count; long total; };

void toy::input(int p,int c) { price=p; count=c; }

void toy::compute()

{ total=(long)price*count; } void toy::print()

{ cout<<\void main()

{ toy te(2,100);//测试构造函数 toy *ob;

ob=new toy[6]; ob[0].input(25,130); ob[0].input(25,130); ob[1].input(30,35);

ob[2].input(15,20); ob[3].input(25,120); ob[4].input(45,10); ob[5].input(85,65); for(int i=0;i<6;i++)

ob[i].compute(); // clrscr();

for(i=0;i<6;i++)

ob[i].print(); delete ob; }

[4_19]构建一个类stock 见书139页 答案如下: #include #include #include const int SIZE=80; class stock { public: stock()

{ strcpy(stockcode,\ }

stock(char code[],int q=1000,float p=8.98) { strcpy(stockcode,code); quan=q; price=p; }

void print(void)

{ cout<stockcode;

cout<<\ } private:

char stockcode[SIZE]; int quan; float price; }; main() {

stock st1(\ st1.print(); stock st2;

char stockc[]=\ st2=stockc; st2.print(); return 0; }

说明:执行以下语句,可在定义对象的同时,给数据成员设置初值。但构造函数 的参数必须定义为缺省值: stock st2;

char stockc[]=”600002”; st2=stockc; st2.print();

定义不含第2和第3个参数的对象st2时,quan的值为1000,price的值 为8.98

[4_20]编写一个有关股票的程序,见书139页题 #include #include #include

class shen_stock; //深圳类 class shang_stock //上海类 { public:

shang_stock(int g,int s,int p); //构造函数

friend void shang_count(const shang_stock ss);//计算上海的

//股票总数 friend void count(const shang_stock ss,const shen_stock zs); //计算上海和深圳的股票总数 private:

int general; //普通股票个数 int st; //ST股票个数 int pt; //PT股票个数 };

shang_stock::shang_stock(int g,int s,int p)//构造函数 { general=g; st=s; pt=p; }

class shen_stock

{ int general; //普通股票个数 int st; //ST股票个数 int pt; //PT股票个数 public:

shen_stock(int g,int s,int p);//构造函数

friend void shen_count(const shen_stock es);//计算深圳的股票总数 friend void count(const shang_stock ss,const shen_stock zs); //计算上海和深圳的股票总数 };

shen_stock::shen_stock(int g,int s,int p)//构造函数 { general=g; st=s; pt=p;

百度搜索“77cn”或“免费范文网”即可找到本站免费阅读全部范文。收藏本站方便下次阅读,免费范文网,提供经典小说综合文库面向对象程序设计课后习题答案(4)在线全文阅读。

面向对象程序设计课后习题答案(4).doc 将本文的Word文档下载到电脑,方便复制、编辑、收藏和打印 下载失败或者文档不完整,请联系客服人员解决!
本文链接:https://www.77cn.com.cn/wenku/zonghe/409174.html(转载请注明文章来源)
Copyright © 2008-2022 免费范文网 版权所有
声明 :本网站尊重并保护知识产权,根据《信息网络传播权保护条例》,如果我们转载的作品侵犯了您的权利,请在一个月内通知我们,我们会及时删除。
客服QQ: 邮箱:tiandhx2@hotmail.com
苏ICP备16052595号-18
× 注册会员免费下载(下载后可以自由复制和排版)
注册会员下载
全站内容免费自由复制
注册会员下载
全站内容免费自由复制
注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: