第三章第四章习题答案_第1页
第三章第四章习题答案_第2页
第三章第四章习题答案_第3页
第三章第四章习题答案_第4页
第三章第四章习题答案_第5页
已阅读5页,还剩17页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、第三章习题答案一、 填空题1类的成员包括两类成员,一类是代表对象属性的_数据成员_,另一类是实现对象行为的_成员函数_。2. C+对类的成员提供了_ public(公有类型) 、private(私有类型)和protected(保护类型)三种不同的访问权限。3. C+中,_main_是主函数名,一个项目中_一个_名为main的函数,它表示程序执行的_开始点_。4. 在C+中,构造函数的名字必须与_类名_相同,它可以有任意类型的_参数_,但没有_返回值类型_,也不能指定为_void_类型。定义对象时,系统会_自动_调用构造函数。5. 在C+中,析构函数的名字必须由_和_类名_组成,它没有_参数_,

2、也没有_返回值_,也不能_被重载_。6. 在C+中,函数的参数传递有_三种 _方式,即_值传递_、_指针传递_和_引用传递_。7. 对象数组是指每一数组元素都是_对象 _的数组。对象数组的元素不仅具有_数据成员_,而且具有_成员函数_。二、判断正误1. 若没有明确的声明,则类中的成员的访问权限为protected。(X)2. 类中的任何成员函数都可以被定义为内联函数。 (X)3. 构造函数必须定义,不能默认。 (X)4. 在类中定义的函数默认为内联函数。()5. 声明为protected的类成员,只能被它所在类及从该类派生的子类的成员函数及友元函数访问。 ()6在声明类的同时,不能直接定义对象

3、。(X)7对象数组的元素是对象,但只能有数据成员。(X)8C+语言中,函数的参数和返回值的传递方式只有值传递和引用传递两种。(X)9拷贝构造函数的形参只能是本类对象的引用。()三、选择题1下面关于重载函数说法中正确的是( D )。A.重载函数必须具有不同的返回类型 B.重载函数的参数个数必须不同C.重载函数参数名称必须不同 D.重载函数必须有不同的参数列表2关于参数默认值描述正确的是( D )。 A.要设置参数的默认值,就必须全部设置 B.参数设置默认值后,调用函数时不能再对参数赋值 C.参数默认值的设置,可以任意设置 D.参数默认值的设置,只能在函数声明时设置。3关于构造函数,下面说法正确的

4、是( 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 )是错误的

5、。 A.成员函数一定是内联函数 B.成员函数可以重载 C.成员函数可以设置参数的缺省值 D.成员函数可以是静态的8Student是已定义的一个类,那么执行语句“Student stu1,stu2(3),*stu3,*stu4;”,调用了( B )次构造函数。 A.1 B.2 C.3 D.49 “void point(Student &s);”是某类中的一个成员函数声明,Student &s的含义为( B )。 A.将s的地址赋给变量 B.s是类Student的对象引用,用来作为point()的形参 C.指向类Student的指针为s D.&s是类Student的对象,用来作为point()的形

6、参四、改错题,请指出下面程序中的错误代码,并说出错误原因和改错方法。1class Date private: int year,month,day;public: Date(int y,int m,int d); void Print(Time t);class Timeprivate: int hour,minute,second;public: Time(int h,int m,int s); friend void Date:Print(Time t);应在class Date语句前面加入语句class Time;表示向前引用。因为友元函数Print使用了Time类的对象作为参数,而类T

7、ime要在类Date后面才进行声明。2#include using namespace std;class Base protected:int x; public:Base(int m) x=m; ;void mian( ) Base a(10); couta.xendl;couta.xendl;语句有错。因为数据成员x是受保护数据成员,因此不能被类Base的对象a访问。改错方法有两种(任选一种):1)去掉couta.xendl;语句;2)修改语句protected:为public:。3#include stdafx.h#include iostreamusing namespace std

8、;class Clockint hour,minute,second;public: void SetTime(int h=0,int m=0,int s=0); void ShowTime();int main() Clock clock; coutFirst time set and output:endl; clock.SetTime (); clock.ShowTime (); coutSecond time set and output:endl; clock.SetTime (10,10,10); clock.ShowTime (); clock.hour=12; return 0

9、;void Clock:SetTime (int h,int m,int s) hour=h; minute=m; second=s;void Clock:ShowTime () couthour:minute:secondendl;clock.hour=12;语句有错。因为数据成员hour是私有成员,所以不能被Clock类的对象clock访问。改正方法:去掉语句clock.hour=12;4. #include stdafx.h#include using namespace std;class Cube public: Cube(int=10,int,int=10); int volume

10、(); 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)

11、Cube(int=10,int,int=10);改为Cube(int=10,int=10,int=10);五、写出下面程序的运行结果1. #include stdafx.h#include iostreamusing namespace std;class Pointint 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

12、p.Getx ()endl;Point fun2() Point a(3,4); return a;int main() Point a(7,8); couta.Getx()endl; Point b(a); coutb.Getx()endl; fun1(b); b=fun2(); coutb.Getx()endl; return 0;运行结果为: 7 拷贝构造函数被调用 7拷贝构造函数被调用7拷贝构造函数被调用32. #include stdafx.h#include iostreamusing namespace std;class Pointint x,y;public: Point(i

13、nt a,int b) x=a; y=b; void Print() cout(x,y)endl; ;int main() Point a3=Point(1,1),Point(2,2),Point(3,3); int i; for(i=0;i3;i+)ai.Print(); return 0;运行结果为: (1,1) (2,2) (3,3)3.#include stdafx.h#include iostreamusing namespace std;class Cexampleint i;public: Cexample(int n); Cexample(Cexample &b); Cexam

14、ple(); int Get();int add(Cexample a);int main() Cexample x(12); coutx.Get ()endl; coutadd(x)endl; return 0;Cexample:Cexample(int n)i=n; coutConstructingendl;Cexample:Cexample(Cexample &b) i=b.i; coutCopy constructingendl;Cexample:Cexample () coutDestructingendl;int Cexample:Get () return i;int add(C

15、example a) return a.Get ()*a.Get ();运行结果为: Constructing 12 Copy constructing Destructing 144Destructing六、编程题1. 设计一个名为Rectangle的矩形类,其属性为矩形的左上角和右下角两个点的坐标,能计算和输出矩形的周长和面积。#include stdafx.h#include iostreamusing namespace std;struct Pointint a;int b;class Rectangle Point topLeft; Point bottomRight;public

16、: Rectangle(Point a,Point b); int Area(); int SideLength();int main()Point m,n; m.a=3;m.b=4;n.a=12; n.b =10; Rectangle rect(m,n); cout矩形的面积为:rect.Area()endl; cout矩形的周长为:rect.SideLength()endl; return 0;Rectangle:Rectangle(Point a,Point b) topLeft=a; bottomRight=b;int Rectangle:Area() int x=bottomRigh

17、t.a-topLeft.a;int y=bottomRight.b-topLeft.b; return x*y ;int Rectangle:SideLength() int x=bottomRight.a-topLeft.a;int y=bottomRight.b-topLeft.b; return 2*(x+y) ;2. 声明一个datatype类,能处理包含字符型、整型和浮点型三种类型的数据,给出其构造函数。#include stdafx.hclass datatypechar x;int y;double z;public: datatype(char x1);datatype(int

18、 y1);datatype(double z1);datatype:datatype (char x1)x=x1;datatype:datatype(int y1)y=y1;datatype:datatype (double z1):z(z1)int _tmain(int argc, _TCHAR* argv) return 0;3. 一矩形体育场如下图所示,现在需在其周围建一矩形过道,并在四周围安上栅栏。栅栏价格为50/米,过道造价为240元/平方米。过道宽为3米,体育场的长宽由键盘输入。请编写程序计算并输出过道和栅栏的造价。#include stdafx.h#include iostrea

19、musing namespace std;class Rectangledouble length;double width;public:Rectangle(double Length=10.,double Width=5.);double Area();double SideLength();int main()int a=50,b=240;double x,y;coutxy;coutendl;Rectangle rect1(x,y),rect2(x+3,y+3);cout栅栏的长度为:rect2.SideLength() ,造价为:rect2.SideLength()*aendl; do

20、uble area12;area12=rect2.Area ()-rect1.Area ();cout过道的面积为:area12 ,造价为:area12*bn;switch(n) case 1: int i;Employees x;couti;cout请输入员工的编号、姓名、性别、年龄、职务、职称、岗位和薪酬:x.no x.sex x.age x.positions fessionalTitles x.remuneration ;Employees_Object.Insert_seq(i,x);cout插入后的情况:endl;Employees_Objec

21、t.Print_seq();break; case 2: int i;couti;Employees_Object.Delete_seq(i);cout删除后的情况:endl;Employees_Object.Print_seq();break; case 0:m=false; return 0;void menu() coutendl; cout1.插入endl; cout2.删除endl; cout0.退出endl; coutendl; cout请选择:;Employees_c:Employees_c ()total=0;int Employees_c:Insert_seq (int i,

22、Employees x) int j; if(total=MAXSIZE) couttable is fullendl;return -1; if(i(total+1) coutplace is wrong!=i-1;j-) Employees_structj+1=Employees_structj; Employees_structi-1=x; +total; return 1;int Employees_c:Delete_seq (int i) int j; if(itotal) coutthis element dont exist!endl;return -1; for(j=i;j=t

23、otal-1;j+) Employees_structj-1=Employees_structj; -total; return 1;void Employees_c:Print_seq () int i; for (i=0;i=total-1;i+) coutEmployees_structi.nosetw(10)Employees_ setw(10)Employees_structi.sexsetw(10)Employees_structi.age setw(10)Employees_structi.positionssetw(10)Employees_struct

24、fessionalTitles setw(10)Employees_ setw(10)Employees_structi.remuneration endl; coutendl;5. 设计一个复数类,要求对其构造函数进行重载。#include stdafx.hclass Complexdouble real,imag;public:Complex();Complex(double real1);Complex(double real1,double imag1);Complex:Complex ()real=0.;imag=0.;Complex:Complex

25、(double real1)real=real1; imag=0.;Complex:Complex(double real1,double imag1)real=real1;imag=imag1;第四章习题答案一、 填空题1. 静态数据成员是一种_特殊的_数据成员类型,它的定义以关键字_static_开头。2. 静态数据成员不能在类的_构造函数_中初始化,也不可在类的_体内_进行赋初值,它的初始化工作只能在_类外_进行,并且在_对象生成_之前进行。3. 静态成员函数是_类_的一部分,而不是_对象_的一部分。如果要在类外调用公用的静态成员函数,要使用_类名 和 域运算符”:”_。4. 类的友元是

26、一种定义在_该类_外部的或者普通函数或者另一个类的成员函数或者另一个类,但需要在该_类体内_进行说明,在说明时前面需加关键字_friend_。5. 类的友元虽不是该类的成员函数,但是可以访问该类的_私有_成员。当友元是一个函数时,我们称该函数为_友元函数_;当友元是一个类时,我们称该类为_友元类_。6. 按生存期的不同,对象可分为_局部对象_、_静态对象_、_全局对象_和_动态对象_四种。7. 常量对象的特点是它的数据成员的_值_在对象的整个生存期内都不能被_修改_。8. const与_指针_的配合使用有两种方式:一种是用const修饰指针指向的变量,称为_指向常量的指针_;另一种是用cons

27、t修饰指针,称为_常量指针_。二、判断正误1. 类中的静态数据成员可以采用构造函数进行初始化。(X)2. 如果某个对象在其生命周期内不能被修改,那么将这个对象定义为const对象。()3. 静态数据成员初始化在类体外进行,而且前面必须加static。(X)4静态成员函数可以在类内定义,也可以在类外定义。在类外定义时,和普通成员函数不同的是要使用static前缀。(X)5按生存期的不同,对象只可分为局部对象、静态对象和全局对象三种。(X)6常量数据成员的初始化只能通过构造函数的初始化列表进行。()三、选择题1下述静态数据成员的特性中,( D )是错误的。A.说明静态数据成员时,前面要加修饰符st

28、aticB.静态数据成员要在类体外进行初始化C.引用静态数据成员时,要在静态数据成员名前加和作用域运算符D.静态数据成员不是所有对象所共用的2友元的作用是( A )。 A.提高程序的运行效率 B.加强类的封装性 C.实现数据的隐藏性 D.增加成员函数的种类3下面关于友元函数的描述中,正确的说法是( A )。 A.友元函数是独立于当前类的外部函数 B.一个友元函数不能同时定义为多个类的友元函数 C.友元函数必须在类的外部定义 D.在外部定义友元函数时,必须加关键字friend4关于静态数据成员,下面叙述错误的是( A )。 A.静态数据成员在对象调用析构函数后,从内存中撤销 B.即使没有实例化类

29、,静态数据成员也可以通过类名进行访问 C.类的静态数据成员为该类的全部对象所共享 D.类的静态数据成员需要初始化5关于静态成员,下面叙述错误的是( B )。 A.类的外部可以直接调用类的静态数据成员和成员函数 B.与一般成员一样,只有通过对象才能访问类的静态成员 C.类的静态数据成员不能在构造函数中初始化 D.类的一般成员函数可以调用类的静态成员6静态成员为该类的所有( B )共享。 A.成员 B.对象 C.this指针 D.友元7下面的定义中,( B )是非法的。 A. int I; B. const int I; C. const int *p; D. int *const p=&I四、改

30、错题,请指出下面程序中的错误代码,并说出错误原因和改错方法。 1class Basic int a; static b; public: static void Output() coutaendl; coutbendl; ;static b;错误,原因是没有指定b的数据类型。应改为static int b;2.#include stdafx.h#include using namespace std;class Ppublic:P(int a,int b) x=a; y=b;static void f(P m);private:int x;static int y;void P:f(P m)

31、coutx=m.xendl; couty=yendl;int P:y=0; int p:x=1;int p:x=1;错误。原因是x不是静态数据成员,不能在类外初始化。改错方法:去掉int p:x=1;3. #include stdafx.h#include iostreamusing namespace std;class Rectangle int w,h; public: int getValue1() const; int getValue(); Rectangle()Rectangle(int x,int y);void main() Rectangle const a(3,4); c

32、outa.getValue()endl; couta.getValue1 ()endl;int Rectangle:getValue1() const return w*h; int Rectangle:getValue() return w+h;Rectangle:Rectangle(int x,int y) w=x; h=y;couta.getValue()endl;语句错。原因是常量对象不能调用非常量成员函数。改错方法:去掉couta.getValue()endl;五、写出下面程序的运行结果1#include stdafx.h#include iostreamusing namespac

33、e std;class Countstatic int count;public: Count() count+; static int Get() return count; Count() count-; ;int Count:count=1000;int main()Count d1,d2,d3,d4; coutCount:Get()endl; return 0;10042#include stdafx.h#include iostreamusing namespace std;class Set int elems100; int Pc;public: Set() Pc=0; Set(

34、Set &b); void Empty() Pc=0; int IsEmpty() return Pc=0; int IsMemberOf(int n); int Add(int n); void Print(); friend void reverse(Set *m);int Set:IsMemberOf (int n)for(int i=0;i=100)return 0; else elemsPc+=n; return 1; Set:Set (Set &b) Pc=b.Pc; for(int i=0;iPc;i+)elemsi=b.elems i;void Set:Print()cout(

35、; for(int i=0;iPc-1;i+)coutelemsi0)coutelemsPc-1; cout)Pc /2; for(int i=0;ielemsi;m-elemsi=m-elemsm-Pc-i-1;m-elemsm-Pc-i-1=temp; int main() Set A; coutA.IsEmpty ()endl; A.Print(); Set B; for(int i=1;i=8;i+)B.Add (i); B.Print(); coutB.IsMemberOf (5)endl; B.Empty (); for(int j=11;j20;j+) B.Add (j); Se

36、t C(B); C.Print(); reverse(&C); C.Print(); return 0;1()(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)六、编程题1(1)建立一个类,该类具有const和非const成员函数。(2)创建这个类的const和非const对象,并用不同类型的对象调用不同类型的成员函数。#include stdafx.h#include using namespace std;class A public: void point();void output() c

37、onst;int _tmain(int argc, _TCHAR* argv) A const a1;A a2;a1.output();a2.output ();a2.point ();return 0;void A:point ()coutpointendl;void A:output () constcoutoutputendl;2编写一个类,统计目前存在多少个该类的对象。#include stdafx.h#include using namespace std;class Astatic int total;public:A() total+; cout当前类的对象总数为:totalen

38、dl; A() total-; cout当前类的对象总数为totalendl; ;void f()A aa,bb,cc;int A:total=0;int _tmain(int argc, _TCHAR* argv)A a1,a2,a3;f();A b1,b2,b3;return 0;3编写一个学生类,学生信息包括姓名、学号、年龄、性别和成绩;统计学生的总人数及总成绩,并输出。#include stdafx.h#include using namespace std;class Studentint no;char name10;char sex3; int age;double score;

39、static int totalNumber;static double totalScore;public:Student(int no_,char *name_,char *sex_,int age_,double score_);static void Output();void StudentInformation();int Student:totalNumber=0;double Student:totalScore =0;int _tmain(int argc, _TCHAR* argv)Student stu1(1001,张三,男,18,97.5);stu1.StudentIn

40、formation ();Student stu2(1002,李四,女,19,83.);stu2.StudentInformation ();Student stu3(1003,王五,男,17,93.);stu3.StudentInformation ();Student stu4(1004,郭六,女,20,62.5);stu4.StudentInformation ();Student stu5(1005,任七,男,18,77.); stu5.StudentInformation ();Student:Output ();return 0;Student:Student (int no_,c

41、har *name_,char *sex_,int age_,double score_)no=no_;strcpy(name,name_);strcpy(sex,sex_);age=age_;score=score_;totalNumber+;totalScore+=score;void Student:StudentInformation ()cout学号:no 姓名:name 性别:sex 年龄:age 成绩:scoreendl;void Student:Output ()cout学生总数:totalNumber 总成绩:totalScore 平均成绩:totalScore/totalN

42、umberendl;4编写一个学生类,(1)输出每个学生的姓名、学号、成绩;(2)统计并输出学生的总人数、总成绩、平均成绩、最高成绩、最低成绩。#include stdafx.h#include using namespace std;class Studentint no;char name10; double score;static int totalNumber;static double totalScore;static double lowestScore;static double highestScore;public:Student(int no_,char *name_,

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论