当前位置:文档之家› 第三章第四章习题答案

第三章第四章习题答案

第三章习题答案一、填空题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是已定义的一个类,那么执行语句“Student stu1,stu2(3),*stu3,*stu4;”,调用了(B)次构造函数。

A.1B.2C.3D.49.“void point(Student &s);”是某类中的一个成员函数声明,Student &s的含义为(B)。

A.将s的地址赋给变量B.s是类Student的对象引用,用来作为point()的形参C.指向类Student的指针为sD.&s是类Student的对象,用来作为point()的形参四、改错题,请指出下面程序中的错误代码,并说出错误原因和改错方法。

1.class Date{private:int year,month,day;public:Date(int y,int m,int d);void Print(Time t);};class Time{private:int hour,minute,second;public:Time(int h,int m,int s);friend void Date::Print(Time t);};应在class Date语句前面加入语句class Time;表示向前引用。

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

2.#include <iostream>using namespace std;class Base{protected:int x;public:Base(int m) { x=m; }};void mian( ){Base a(10);cout<<a.x<<endl;}cout<<a.x<<endl;语句有错。

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

改错方法有两种(任选一种):1)去掉cout<<a.x<<endl;语句;2)修改语句protected:为public:。

3.#include "stdafx.h"#include "iostream"using namespace std;class Clock{int hour,minute,second;public:void SetTime(int h=0,int m=0,int s=0);void ShowTime();};int main(){Clock clock;cout<<"First time set and output:"<<endl;clock.SetTime ();clock.ShowTime ();cout<<"Second time set and output:"<<endl;clock.SetTime (10,10,10);clock.ShowTime ();clock.hour=12;return 0;}void Clock::SetTime (int h,int m,int s){hour=h;minute=m;second=s;}void Clock::ShowTime (){cout<<hour<<":"<<minute<<":"<<second<<endl;}clock.hour=12;语句有错。

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

改正方法:去掉语句clock.hour=12;4.#include "stdafx.h"#include <iostream>using namespace std;class Cube{public:Cube(int=10,int,int=10);int volume();private:int height;int width;int length;};Cube::Cube(int h,int w,int len){height=h;width=w;length=len;}int Cube::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"using namespace std;class Point{int x,y;public:Point(int xx=0,int yy=0){x=xx;y=yy;}Point(Point &p){x=p.x;y=p.y;cout<<"拷贝构造函数被调用"<<endl;}int Getx(){return x;}int Gety(){return y;}};void fun1(Point p){cout<<p.Getx ()<<endl;}Point fun2(){Point a(3,4);return a;}int main(){Point a(7,8);cout<<a.Getx()<<endl;Point b(a);cout<<b.Getx()<<endl;fun1(b);b=fun2();cout<<b.Getx()<<endl;return 0;}运行结果为:7拷贝构造函数被调用7拷贝构造函数被调用7拷贝构造函数被调用32.#include "stdafx.h"#include "iostream"using namespace std;class Point{int x,y;public:Point(int a,int b){x=a;y=b;}void Print(){cout<<"("<<x<<","<<y<<")"<<endl;}};int main(){Point a[3]={Point(1,1),Point(2,2),Point(3,3)};int i;for(i=0;i<3;i++)a[i].Print();return 0;}运行结果为:(1,1)(2,2)(3,3)3.#include "stdafx.h"#include "iostream"using namespace std;class Cexample{int i;public:Cexample(int n);Cexample(Cexample &b);~Cexample();int Get();};int add(Cexample a);int main(){Cexample x(12);cout<<x.Get ()<<endl;cout<<add(x)<<endl;return 0;}Cexample::Cexample(int n){i=n;cout<<"Constructing"<<endl;}Cexample::Cexample(Cexample &b){i=b.i;cout<<"Copy constructing"<<endl;}Cexample::~Cexample (){cout<<"Destructing"<<endl;}int Cexample::Get (){return i;}int add(Cexample a){return a.Get ()*a.Get ();}运行结果为:Constructing12Copy constructingDestructing144Destructing六、编程题1. 设计一个名为Rectangle的矩形类,其属性为矩形的左上角和右下角两个点的坐标,能计算和输出矩形的周长和面积。

相关主题