没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
第六章习题答案
一、选择填空
1、A 2、C 3、D 4、B 5、D 6、A 7、C 8、A 9、D 10、A 11、C 12、A
13、B 14、C 15、C 16、D 17、B 18、C 19、A 20、D
21、C 22、B
二、判断下列描述的正确性,对者划√,错者划×。
1、√ 2、× 3、× 4、× 5、√ 6、√ 7、× 8、√ 9、× 10、√
11、√ 12、√ 13、√ 14、√ 15、× 16、√ 17、√ 18、√ 19、√ 20、×
21、× 22、×
三、分析下列程序的输出结果。
1、运行该程序输出结果如下所示。
Default constructor called
Constructor called
a=0,b=0
a=4,b=8
2、运行该程序输出结果如下所示。
a=7,b=9
3、运行该程序输出结果如下所示。
104
4、运行该程序输出结果如下所示。
1035,789.504
5、运行该程序输出结果如下所示。
1
{}
{0,1,2,3,4,5,6,7,8}
1
{11,12,13,14,15,16,17,18,19}
{19,18,17,16,15,14,13,12,11}
6、运行该程序输出结果如下所示。
Starting1:
Default constructor called.
Default constructor called.
Default constructor called.Eding1:
Starting2:
Constructor: a=5,b=6
Constructor: a=7,b=8
Constructor: a=9,b=10
Ending2:
Destructor called.a=9,b=10
Destructor called.a=7,b=8
Destructor called.a=5,b=6
Destructor called.a=5,b=6
Destructor called.a=3,b=4
Destructor called.a=1,b=2
7、运行该程序输出结果如下所示。
Default constructor called.
Default constructor called.
Default constructor called.
Default constructor called.
Destructor called.
Constructor1 called.
Destructor called.
Constructor2 called.
Destructor called.
x=0,y=0
x=5,y=0
x=2,y=3
Destructor called.
Destructor called.
Destructor called.
8、运行该程序输出结果如下所示。
Constructor called.0
Constructor called.5
Destructor called.5
5
Destructor called.5
9、运行该程序输出结果如下所示。
Constructor called.5
5
Destructor called.5
10、运行该程序输出结果如下所示。
Default Constructor called.
Constructor:real=6.8,imag=0
Constructor:real=5.6,imag=7.9
0+0I
6.8+0I
5.6+7.9I
Constructor:real=1.2,imag=3.4
Constructor:real=5,imag=0
Default Constructor called.
1.2+3.4I
5+0I
0+0I
11、答:
⑴ 该程序中用 string.h 中所包含的函数有 3 种,它们是 strcpy( )、strcat( )和 strlen( ).
⑵ 该程序中使用了函数重载。它们是下述两个构造函数重载:String( )和 String(const
char * str)。
⑶ 类中成员函数 Setc( )的功能是用来设置字符数组中某位置的一个指定字符。
⑷ 类中成员函数 Getc( )的功能是用来从某个字符数组中获取指定位置的字符。
⑸ 类中成员函数 Append( )的功能是在一个字符数组中追加一个指定的字符串,即将指
定的字符串添加到已知串的后边。
⑹ 不行。
⑺ 该程序中有两处使用了 new 运算符。
⑻ 运行该程序输出结果如下所示。
empty.
a string.
9
a string.
i
this a string.
四、改正以下程序中的错误。
1、该程序中 point 类的构造函数定义不正确,在 main()中队数据成员的访问不正确,
修改如下:
#include <iostream.h>
class point
{
int x1,x2;
public:
point(int x,int y){point::x=x;point::y=y;}
void disp()
{
cout<<x1<<endl;
cout<<x2<<endl;
}
//……
};
void main()
{
point data(5,5);
data.disp();
}
2、在 main()函数中的 p.x+=5 和 p.y+=6 两个语句是错误的,因为保护数据仅能被类的
成员函数访问。
五、按下列要求编写程序。
1、程序内容如下所示。
#include <iostream.h>
class Test
{
public:
Test(){}
Test(int i,int j=20)
{
t1=i;
t2=j;
t+=j-i;
}
static int fun(Test&T);
friend int add(Test&T);
private:
int t1,t2;
static int t;
};
int Test::fun(Test&T)
{
t+=T.t;
return t;
}
int add(Test&T)
{
int n=T.t1+T.t2;
return n;
}
int Test::t=5;
void main()
{
Test a1,a2(10),a3(15,25);
cout<<add(a2)<<endl;
cout<<Test::fun(a2)<<endl;
}
2、程序内容如下所示。
#include <iostream.h>
#include <string.h>
class Product
{
char *name;
int price;
int quantity;
public:
Product(char *n,int p,int q)
{
name =new char[strlen(n)+1];
strcpy(name,n);
price=p;
quantity=q;
}
~Product()
{
if(name)
{
delete [] name;
name=0;
}
}
void buy(int money)
{
int n,r;
n=money/price;
if(n>quantity)
cout<<"数量不够"<<endl;
else
{
quantity-=n;
r=money%price;
cout<<"产品:"<<name<<"单价:"<<price<<"元 顾客";
cout<<money<<"元,买了"<<n<<"台,剩余"<<r<<"元"<<endl;
}
}
void get() const
{
cout<<"产品:"<<name<<"单价:"<<price<<"元 剩余"<<quantity<<"台"<<endl;
}
};
void main()
{
Product p1("电视机",2000,15);
p1.buy(7000);
p1.get();
p1.buy(4500);
p1.get();
}
3、程序内容如下所示。
#include <iostream.h>
#include <stdlib.h>
class CDate
{
private:
int m_nDay;
int m_nMonth;
int m_nYear;
剩余21页未读,继续阅读
资源评论
oligaga
- 粉丝: 50
- 资源: 2万+
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 笔记实验六,spark,大数据分析
- ####蓝桥杯python的详细的信息介绍
- 电子万年历软件仿真(经过多次修改,保证正确性)
- Unity XR 手势射击控制脚本(适用于任何可手势识别的设备)
- 机械设计全自动电表(NB和IC卡表)控制和上壳装配线sw16可编辑非常好的设计图纸100%好用.zip
- 基于matlab的EAN-13条形码识别系统GUI界面.zip代码53
- matlab基于bp神经网络交通信号标志识别GUI界面13个标志.zip代码54
- 电子万年历答辩实物展示视频mp4格式
- 基于python实现的程序,包括哈希感知算法cvHash,图像切割cvsplit,固定目标检测cvRec(附文档ppt)等
- 计算0-10000之间所有偶数的和
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功