第三章第四章习题答案.docx

上传人:b****1 文档编号:13241175 上传时间:2023-06-12 格式:DOCX 页数:42 大小:47.31KB
下载 相关 举报
第三章第四章习题答案.docx_第1页
第1页 / 共42页
第三章第四章习题答案.docx_第2页
第2页 / 共42页
第三章第四章习题答案.docx_第3页
第3页 / 共42页
第三章第四章习题答案.docx_第4页
第4页 / 共42页
第三章第四章习题答案.docx_第5页
第5页 / 共42页
第三章第四章习题答案.docx_第6页
第6页 / 共42页
第三章第四章习题答案.docx_第7页
第7页 / 共42页
第三章第四章习题答案.docx_第8页
第8页 / 共42页
第三章第四章习题答案.docx_第9页
第9页 / 共42页
第三章第四章习题答案.docx_第10页
第10页 / 共42页
第三章第四章习题答案.docx_第11页
第11页 / 共42页
第三章第四章习题答案.docx_第12页
第12页 / 共42页
第三章第四章习题答案.docx_第13页
第13页 / 共42页
第三章第四章习题答案.docx_第14页
第14页 / 共42页
第三章第四章习题答案.docx_第15页
第15页 / 共42页
第三章第四章习题答案.docx_第16页
第16页 / 共42页
第三章第四章习题答案.docx_第17页
第17页 / 共42页
第三章第四章习题答案.docx_第18页
第18页 / 共42页
第三章第四章习题答案.docx_第19页
第19页 / 共42页
第三章第四章习题答案.docx_第20页
第20页 / 共42页
亲,该文档总共42页,到这儿已超出免费预览范围,如果喜欢就下载吧!
下载资源
资源描述

第三章第四章习题答案.docx

《第三章第四章习题答案.docx》由会员分享,可在线阅读,更多相关《第三章第四章习题答案.docx(42页珍藏版)》请在冰点文库上搜索。

第三章第四章习题答案.docx

第三章第四章习题答案

第三章习题答案

一、填空题

1.类的成员包括两类成员,一类是代表对象属性的_数据成员_,另一类是实现对象行为的___成员函数____。

2.C++对类的成员提供了_public(公有类型)、private(私有类型)和protected(保护类型)三种不同的访问权限。

3.C++中,___main___是主函数名,一个项目中___一个___名为main的函数,它表示程序执行的___开始点__。

4.在C++中,构造函数的名字必须与____类名___相同,它可以有任意类型的__参数__,但没有_返回值类型__,也不能指定为_void_类型。

定义对象时,系统会_自动__调用构造函数。

5.在C++中,析构函数的名字必须由_~__和_类名_组成,它没有_参数_,也没有_返回值_,也不能_被重载_。

6.在C++中,函数的参数传递有___三种_方式,即__值传递__、_指针传递__和_引用传递_。

7.对象数组是指每一数组元素都是___对象_的数组。

对象数组的元素不仅具有___数据成员__,而且具有__成员函数__。

二、判断正误

1.若没有明确的声明,则类中的成员的访问权限为protected。

(X)

2.类中的任何成员函数都可以被定义为内联函数。

(X)

3.构造函数必须定义,不能默认。

(X)

4.在类中定义的函数默认为内联函数。

(√)

5.声明为protected的类成员,只能被它所在类及从该类派生的子类的成员函数及友元函数访问。

(√)

6.在声明类的同时,不能直接定义对象。

(X)

7.对象数组的元素是对象,但只能有数据成员。

(X)

8.C++语言中,函数的参数和返回值的传递方式只有值传递和引用传递两种。

(X)

9.拷贝构造函数的形参只能是本类对象的引用。

(√)

三、选择题

1.下面关于重载函数说法中正确的是(D)。

A.重载函数必须具有不同的返回类型B.重载函数的参数个数必须不同

C.重载函数参数名称必须不同D.重载函数必须有不同的参数列表

2.关于参数默认值描述正确的是(D)。

A.要设置参数的默认值,就必须全部设置

B.参数设置默认值后,调用函数时不能再对参数赋值

C.参数默认值的设置,可以任意设置

D.参数默认值的设置,只能在函数声明时设置。

3.关于构造函数,下面说法正确的是(A)。

A.构造函数没有返回类型

B.构造函数的名字可以与类名不同

C.构造函数不能重载

D.构造函数只能在类外定义

4.(D)不是构造函数的特征。

A.构造函数的函数名与类名相同

B.构造函数可以重载

C.构造函数可以设置缺省参数

D.构造函数必须指定类型说明

5.关于析构函数,下面说法正确的是(B)。

A.析构函数可以重载

B.析构函数不能指定返回类型

C.析构函数的名字与类名相同

D.析构函数可以定义在私有部分

6.通常的拷贝构造函数的参数是(C)

A.某个对象名

B.某个对象的成员名

C.某个对象的引用名

D.某个对象的指针名

7.关于成员函数特征,下属描述中,(A)是错误的。

A.成员函数一定是内联函数

B.成员函数可以重载

C.成员函数可以设置参数的缺省值

D.成员函数可以是静态的

8.Student是已定义的一个类,那么执行语句“Studentstu1,stu2(3),*stu3,*stu4;”,调用了(B)次构造函数。

A.1B.2C.3D.4

9.“voidpoint(Student&s);”是某类中的一个成员函数声明,Student&s的含义为(B)。

A.将s的地址赋给变量

B.s是类Student的对象引用,用来作为point()的形参

C.指向类Student的指针为s

D.&s是类Student的对象,用来作为point()的形参

四、改错题,请指出下面程序中的错误代码,并说出错误原因和改错方法。

1.

classDate

{

private:

intyear,month,day;

public:

Date(inty,intm,intd);

voidPrint(Timet);

};

classTime

{

private:

inthour,minute,second;

public:

Time(inth,intm,ints);

friendvoidDate:

:

Print(Timet);

};

应在classDate语句前面加入语句classTime;表示向前引用。

因为友元函数Print使用了Time类的对象作为参数,而类Time要在类Date后面才进行声明。

2.

#include

usingnamespacestd;

classBase

{

protected:

intx;

public:

Base(intm){x=m;}

};

voidmian()

{

Basea(10);

cout<

}

cout<

因为数据成员x是受保护数据成员,因此不能被类Base的对象a访问。

改错方法有两种(任选一种):

1)去掉cout<

为public:

3.

#include"stdafx.h"

#include"iostream"

usingnamespacestd;

classClock

{

inthour,minute,second;

public:

voidSetTime(inth=0,intm=0,ints=0);

voidShowTime();

};

intmain()

{

Clockclock;

cout<<"Firsttimesetandoutput:

"<

clock.SetTime();

clock.ShowTime();

cout<<"Secondtimesetandoutput:

"<

clock.SetTime(10,10,10);

clock.ShowTime();

clock.hour=12;

return0;

}

voidClock:

:

SetTime(inth,intm,ints)

{

hour=h;

minute=m;

second=s;

}

voidClock:

:

ShowTime()

{

cout<

"<

"<

}

clock.hour=12;语句有错。

因为数据成员hour是私有成员,所以不能被Clock类的对象clock访问。

改正方法:

去掉语句clock.hour=12;

4.

#include"stdafx.h"

#include

usingnamespacestd;

classCube

{

public:

Cube(int=10,int,int=10);

intvolume();

private:

intheight;

intwidth;

intlength;

};

Cube:

:

Cube(inth,intw,intlen)

{

height=h;

width=w;

length=len;

}

intCube:

:

volume()

{

return(height*width*length);

}

Cube(int=10,int,int=10);语句有错。

因为对一个函数的参数设置默认值时,所有给默认值的参数都必须在不给默认值的参数的右面。

改错方法有两种(任选一种):

1)Cube(int=10,int,int=10);改为Cube(int,int,int=10);;2)Cube(int=10,int,int=10);改为Cube(int=10,int=10,int=10);

五、写出下面程序的运行结果

1.

#include"stdafx.h"

#include"iostream"

usingnamespacestd;

classPoint

{

intx,y;

public:

Point(intxx=0,intyy=0)

{

x=xx;

y=yy;

}

Point(Point&p)

{

x=p.x;

y=p.y;

cout<<"拷贝构造函数被调用"<

}

intGetx()

{

returnx;

}

intGety()

{

returny;

}

};

voidfun1(Pointp)

{

cout<

}

Pointfun2()

{

Pointa(3,4);

returna;

}

intmain()

{

Pointa(7,8);

cout<

Pointb(a);

cout<

fun1(b);

b=fun2();

cout<

return0;

}

运行结果为:

7

拷贝构造函数被调用

7

拷贝构造函数被调用

7

拷贝构造函数被调用

3

2.

#include"stdafx.h"

#include"iostream"

usingnamespacestd;

classPoint

{

intx,y;

public:

Point(inta,intb)

{

x=a;

y=b;

}

voidPrint()

{

cout<<"("<

}

};

intmain()

{

Pointa[3]={Point(1,1),Point(2,2),Point(3,3)};

inti;

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

a[i].Print();

return0;

}

运行结果为:

(1,1)

(2,2)

(3,3)

3.

#include"stdafx.h"

#include"iostream"

usingnamespacestd;

classCexample

{

inti;

public:

Cexample(intn);

Cexample(Cexample&b);

~Cexample();

intGet();

};

intadd(Cexamplea);

intmain()

{

Cexamplex(12);

cout<

cout<

return0;

}

Cexample:

:

Cexample(intn)

{

i=n;

cout<<"Constructing"<

}

Cexample:

:

Cexample(Cexample&b)

{

i=b.i;

cout<<"Copyconstructing"<

}

Cexample:

:

~Cexample()

{

cout<<"Destructing"<

}

intCexample:

:

Get()

{

returni;

}

intadd(Cexamplea)

{

returna.Get()*a.Get();

}

运行结果为:

Constructing

12

Copyconstructing

Destructing

144

Destructing

六、编程题

1.设计一个名为Rectangle的矩形类,其属性为矩形的左上角和右下角两个点的坐标,能计算和输出矩形的周长和面积。

#include"stdafx.h"

#include"iostream"

usingnamespacestd;

structPoint

{

inta;

intb;

};

classRectangle

{

PointtopLeft;

PointbottomRight;

public:

Rectangle(Pointa,Pointb);

intArea();

intSideLength();

};

intmain()

{

Pointm,n;

m.a=3;

m.b=4;

n.a=12;

n.b=10;

Rectanglerect(m,n);

cout<<"矩形的面积为:

"<

cout<<"矩形的周长为:

"<

return0;

}

Rectangle:

:

Rectangle(Pointa,Pointb)

{

topLeft=a;

bottomRight=b;

}

intRectangle:

:

Area()

{

intx=bottomRight.a-topLeft.a;

inty=bottomRight.b-topLeft.b;

returnx*y;

}

intRectangle:

:

SideLength()

{

intx=bottomRight.a-topLeft.a;

inty=bottomRight.b-topLeft.b;

return2*(x+y);

}

2.声明一个datatype类,能处理包含字符型、整型和浮点型三种类型的数据,给出其构造函数。

#include"stdafx.h"

classdatatype

{

charx;

inty;

doublez;

public:

datatype(charx1);

datatype(inty1);

datatype(doublez1);

};

datatype:

:

datatype(charx1)

{

x=x1;

}

datatype:

:

datatype(inty1)

{

y=y1;

}

datatype:

:

datatype(doublez1):

z(z1)

{

}

 

int_tmain(intargc,_TCHAR*argv[])

{

return0;

}

3.一矩形体育场如下图所示,现在需在其周围建一矩形过道,并在四周围安上栅栏。

栅栏价格为50/米,过道造价为240元/平方米。

过道宽为3米,体育场的长宽由键盘输入。

请编写程序计算并输出过道和栅栏的造价。

 

#include"stdafx.h"

#include"iostream"

usingnamespacestd;

classRectangle

{

doublelength;

doublewidth;

public:

Rectangle(doubleLength=10.,doubleWidth=5.);

doubleArea();

doubleSideLength();

};

intmain()

{

inta=50,b=240;

doublex,y;

cout<<"请输入矩形的长和宽:

";

cin>>x>>y;

cout<

Rectanglerect1(x,y),rect2(x+3,y+3);

cout<<"栅栏的长度为:

"<

<<",造价为:

"<

doublearea12;

area12=rect2.Area()-rect1.Area();

cout<<"过道的面积为:

"<

"<

return0;

}

Rectangle:

:

Rectangle(doubleLength,doubleWidth)

{

length=Length;

width=Width;

}

doubleRectangle:

:

Area()

{

returnlength*width;

}

doubleRectangle:

:

SideLength()

{

return2*(length+width);

}

4.定义一个员工类,员工信息由编号、姓名、性别、年龄、职务、职称、岗位、薪酬等组成,要求利用队列实现员工的增加、删除和输出等功能。

#include"stdafx.h"

#include"iomanip"

#include"iostream"

usingnamespacestd;

#defineMAXSIZE5000

structEmployees

{

longno;//编号

charname[10];//姓名

charsex[3];//性别

intage;//年龄

charpositions[20];//职务

charprofessionalTitles[20];//职称

charjobs[20];//岗位

floatremuneration;//薪酬

};

classEmployees_c

{

private:

EmployeesEmployees_struct[MAXSIZE];

inttotal;

public:

Employees_c();

intInsert_seq(inti,Employeesx);//插入第i员工的信息

intDelete_seq(inti);//删除第i个员工信息

voidPrint_seq();//打印所有员工信息

};

voidmenu();

intmain()

{

Employees_cEmployees_Object;

intn;

boolm=true;

while(m)

{

menu();

cin>>n;

switch(n)

{

case1:

{

inti;

Employeesx;

cout<<"请输入插入位置:

";

cin>>i;

cout<<"请输入员工的编号、姓名、性别、年龄、"

<<"职务、职称、岗位和薪酬:

"<

cin>>x.no>>x.name>>x.sex>>x.age>>x.positions>>

x.professionalTitles>>x.jobs>>x.remuneration;

Employees_Object.Insert_seq(i,x);

cout<<"插入后的情况:

"<

Employees_Object.Print_seq();

break;

}

case2:

{

inti;

cout<<"请输入删除位置:

;

cin>>i;

Employees_Object.Delete_seq(i);

cout<<"删除后的情况:

"<

Employees_Object.Print_seq();

break;

}

case0:

m=false;

}

}

return0;

}

voidmenu()

{

cout<

cout<<"1.插入"<

cout<<"2.删除"<

cout<<"0.退出"<

cout<

cout<<"请选择:

";

}

Employees_c:

:

Employees_c()

{

total=0;

}

intEmployees_c:

:

Insert_seq(inti,Employeesx)

{

intj;

if(total==MAXSIZE)

{

cout<<"tableisfull"<

return-1;

}

if(i<1||i>(total+1))

{

cout<<"placeiswrong!

"<

return0;

}

for(j=total-1;j>=i-1;j--)

{

Employees_struct[j+1]=Employees_struct[j];

}

Employees_struct[i-1]=x;

++total;

return1;

}

intEmployees_c:

:

Delete_seq(inti)

{

intj;

if(i<1||i>total)

{

cout<<"thiselementdon'texist!

"<

return-1;

}

for(j=i;j<=total-1;j++)

{

Employees_struct[j-1]=Employees_struct[j];

}

--total;

return1;

}

voidEmployees_c:

:

Print_seq()

{

inti;

for(i=0;i<=total-1;i++)

{

cout<

<

<

<

<

<

}

cout<

}

5.设计一个复数类,要求对其构造函数进行重载。

#include"stdafx.h"

classComplex

{

doublereal,imag;

public:

Complex();

Complex(doublereal1);

Complex(doublereal1,doubleimag1);

};

Complex:

:

Complex()

{

real=0.;

imag=0.;

}

Complex:

:

Complex(doublereal1)

{

real=

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 自然科学 > 物理

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2