第4章习题答案Word文档下载推荐.docx
- 文档编号:19047432
- 上传时间:2023-01-03
- 格式:DOCX
- 页数:13
- 大小:17.84KB
第4章习题答案Word文档下载推荐.docx
《第4章习题答案Word文档下载推荐.docx》由会员分享,可在线阅读,更多相关《第4章习题答案Word文档下载推荐.docx(13页珍藏版)》请在冰豆网上搜索。
答:
C++中的结构体与类的概念相同点:
结构体与类都属于用户自定义类型。
C++中的结构体与类的概念不同点:
结构体定义没有函数部分,而类定义包含数据部分和函数部分。
结构体定义中的数据都是公有权限,而类定义限制了成员的访问权限。
2.类中的公有(public)成员和私有(private)成员有什么区别?
类中的共有成员既可以被类的成员函数访问,也可以在类外的程序中通过对象被访问;
私有成员只能被该类的成员函数和友元函数来访问。
3.构造函数和析构函数的作用是什么?
构造函数的作用是创建对象时为数据成员分配存储空间并赋初值。
析构函数的作用是在撤销对象时清除并释放内存空间。
4.构造函数是否可以重载?
为什么?
析构函数呢?
答:
构造函数可以重载,因为对于不同的参数输入需要有相应的构造函数与之匹配;
而析构函数不可以重载。
5.拷贝构造函数的作用是什么?
何时调用拷贝构造函数?
拷贝构造函数的作用是对象之间的复制。
拷贝构造函数在以下三种情况下会被复制:
(1)用已经存在的对象去初始化创建同类的一个新对象。
(2)对象作为函数的参数(将实参对象拷贝给形参对象)。
(3)函数的返回值为一个对象(将返回对象拷贝给一个临时对象)。
6.分析下面的程序,写出运行结果。
程序(1):
#include<
iostream.h>
classexap
{intx,y;
public:
exap(inta,intb)
{
x=a;
y=b;
}
exap(exap&
P)
x=P.x;
y=P.y;
intset(intx1,inty1)
x=x1;
y=y1;
intgeta()
{returna;
}
intgetb()
{returnb;
};
voidmain()
exapA(1,2);
exapB=A;
cout<
<
"
A="
A.geta()<
"
A.getb()<
endl;
B="
B.geta()<
B.getb()<
B.set(10,20);
运行结果:
1,2
10,20
程序
(2):
string.h>
classexample1
example1()
constructingexample1."
~example1()
{cout<
destructingexample1."
classexample2
example2()
constructingexample2."
~example2()
destructingexample2."
example1stu1;
example2tea1;
endinmain"
constructingexample1.
constructingexample2.
destructingexample2.
destructingexample1.
endinmain
7.设类A的定义如下:
classA
inta;
……
若用类A定义了两个对象x1,x2,它们各自的数据成员a是同一个变量吗?
取值是否可以不同?
它们各自的数据成员a不是同一个变量,在内存中占有各自不同的存储空间,所以数值上是可以相同的。
8.下面是一个产品类Product的定义。
完成成员函数的定义,并用数据测试这个类。
classProduct
char*name;
//产品名称
intprice;
//产品单价
intquantity;
//剩余产品数量
Product(char*n,intp,intq);
~Product();
voidbuy(intmoney);
//购买产品
voidget()const;
//返回剩余产品数量
Product类成员函数及主函数定义如下:
Product:
:
Product(char*n,intp,intq)
{name=newchar[strlen(n)+1];
strcpy(name,n);
price=p;
quantity=q;
”constructingProduct”<
name<
~Product
{cout<
”destructingProduct”<
delete[]name;
voidProduct:
buy(intmoney)
{if(quantity<
=0)
”sorry,thequantityisempty.”<
return;
intnumber=money/price;
if(number>
quantity)
”thequantity”<
”thatusing”<
money<
”buysisnotenough!
”<
else
”the”<
”buys”<
number<
””<
quantity-=number;
get()const
”thequantityis”<
quantity<
{Productcup(“cup”,3,100);
cup.buy(120);
cup.get();
三、编程题
1.设计一个长方体类,有长、宽、高三个属性,用成员函数计算;
计算并输出长方体的体
积和表面积。
classcuboid
{intlength;
intwidth;
inthigh;
cuboid(intl=0,intw=0,inth=0)
{length=l;
width=w;
high=h;
intgetv()
{returnlength*width*high;
intgets()
{return2*(length*width+width*high+length*high);
voidprint()
”thecuboidis”<
length<
”*”<
width<
high<
”thevolumeis”<
getv()<
”thesurfaceareais”<
gets()<
{cuboidA(4,5,6);
A.print();
2.设计一个点类point,有x,y两个整型的数据成员,用成员函数返回x,y的值。
使用测试程序验证此类。
classpoint
{intx;
inty;
point(intpx,intpy)
{x=px;
y=py;
intgetx()
{returnx;
intgety()
{returny;
};
{pointA(3,5);
”thepointis(”<
A.getx()<
”,”<
A.gety()<
”)”<
3.编写一个程序,设计一个满足以下要求的data类,其数据成员为year,month和day;
类中有两个公有函数setdate()和display(),前一个用来设置日期,后一个用来显示日期。
在主程序中定义该类的两个对象,设置两个具体日期,然后输出这两个日期。
classdata
intyear;
intmonth;
intday;
data(inty=1900,intm=1,intd=1)
year=y;
month=m;
day=d;
”thedatais:
year<
”/”<
month<
day<
voidaddoneday()
inta[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
if((year%4==0&
&
year%100!
=0)||(year%400==0))
a[2]=29;
if(day==a[month])
if(month==12)
{year++;
month=1;
day=1;
{month++;
day++;
”thedayaddone.”<
voidsetdata(inty,intm,intd)
”thedataisreset.”<
{datad1(1965,2,28);
d1.print();
d1.addoneday();
4.设计一个学生类,包括学号、姓名、两门课成绩,要求输出学生数据,计算平均分。
若
在测试程序中定义了一个对象数组,对类的构造函数有什么要求?
classstudent
intnum;
intscore1,score2;
student(intn,char*na,ints1,ints2)
num=n;
name=newchar[strlen(na)+1];
strcpy(name,na);
score1=s1;
score2=s2;
student()
num=0;
name=’\0’;
score1=0;
score2=0;
voidinit(intn,char*na,ints1,ints2)
~student()
{delete[]name;
intgetavg()
{return(score1+score2)/2;
”学生学号:
num<
”姓名:
”平均成绩:
getavg()<
{studentx(1001,”王明”,79,82);
x.print();
5.在上题程序的基础上,定义一个拷贝构造函数,进行学生类对象的复制,并在主函数中测试拷贝构造函数。
student(student&
A)
{num=A.num;
name=newchar[strlen(A.name)+1];
strcpy(name,A.name);
score1=A.score1;
score2=A.score2;
{studentX(1001,”王明”,79,82);
X.print();
studentY(X);
Y.print();
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 习题 答案