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

下载本文档

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

文档简介

1、第4章 类与对象一、选择题1.标志着C+从面相过程变成面相对象的主要措施是(D)。 A.增加了新的运算符 B.允许函数重载,设置默认参数C.规定函数声明必须用原型 D.引用了类和对象的概念2.有关类的说法错误的是(D)。 A.类是一种用户自定义的数据类型B.只有类中的成员函数才能存取类中的私有数据C.在类中如果不作特殊说明,所指的数据均为私有类型D. 在类中如果不作特殊说明,所指的成员函数均为公有类型3.有关类和对象的说法错误的是(C)。A.对象是类的一个实例B.任何一个对象只能属于一个具体的类C.一个类只能有一个对象D.类与对象的关系和数据类型与变量的关系相似4.下列关于构造函数的描述中,错

2、误的是(D)。A.构造函数的函数名与类名相同 B.构造函数可以重载C.构造函数可以设置默认参数 D.构造函数必须指定类型说明5.对任意一个类,析构函数的个数最多为(B)个。A.0 .B.1 C.2 D.46.通常拷贝构造函数的参数是(D)。 A.某个对象名 B.某个对象的成员名 C.某个对象的指针名 D.某个对象的引用名7.已知A是一个类,则执行语句A a;时,将自动调用该类的(B)。A.有参构造函数 B.无参构造函数C.拷贝构造函数 D.赋值构造函数二、简答题1.比较C+中的结构(struct)和类(class)的概念的相同和不同之处。答:C+中的结构体与类的概念相同点:结构体与类都属于用户

3、自定义类型。C+中的结构体与类的概念不同点:结构体定义没有函数部分,而类定义包含数据部分和函数部分。结构体定义中的数据都是公有权限,而类定义限制了成员的访问权限。2.类中的公有(public)成员和私有(private)成员有什么区别?答:类中的共有成员既可以被类的成员函数访问,也可以在类外的程序中通过对象被访问;私有成员只能被该类的成员函数和友元函数来访问。3.构造函数和析构函数的作用是什么?答:构造函数的作用是创建对象时为数据成员分配存储空间并赋初值。析构函数的作用是在撤销对象时清除并释放内存空间。4.构造函数是否可以重载?为什么?析构函数呢?答:构造函数可以重载,因为对于不同的参数输入需

4、要有相应的构造函数与之匹配;而析构函数不可以重载。5.拷贝构造函数的作用是什么?何时调用拷贝构造函数?答:拷贝构造函数的作用是对象之间的复制。拷贝构造函数在以下三种情况下会被复制:(1)用已经存在的对象去初始化创建同类的一个新对象。(2)对象作为函数的参数(将实参对象拷贝给形参对象)。(3)函数的返回值为一个对象(将返回对象拷贝给一个临时对象)。6.分析下面的程序,写出运行结果。程序():#include <iostream.h>class exapint x,y; public:exap(int a,int b) x=a;y=b; exap(exap &P)x=P.x;

5、y=P.y;int set(int x1,int y1) x=x1; y=y1;int geta() return a; int getb() return b; ; void main() exap A(1,2); exap B=A; cout<<"A="<<A.geta()<<","<<A.getb()<<endl;cout<<"B="<<B.geta()<<","<<B.getb()<<en

6、dl;B.set(10,20);cout<<"B="<<B.geta()<<","<<B.getb()<<endl;运行结果:1,21,210,20程序(2):#include <iostream.h>#include <string.h>class example1public:example1()cout<<"constructing example1."<<endl; example1()cout<<"

7、;destructing example1."<<endl;class example2public:example2()cout<<"constructing example2 ."<<endl; example2()cout<<"destructing example2."<<endl;void main()example1 stu1; example2 tea1; cout<<"end in main"<<endl;运行结果:const

8、ructing example1.constructing example2.destructing example2.destructing example1.end in main 7.设类A的定义如下: class A int a; 若用类A定义了两个对象x1,x2,它们各自的数据成员a是同一个变量吗?取值是否可以不同?答:它们各自的数据成员a不是同一个变量,在内存中占有各自不同的存储空间,所以数值上是可以相同的。8.下面是一个产品类Product的定义。完成成员函数的定义,并用数据测试这个类。 class Product char *name; / 产品名称 int price; /

9、产品单价 int quantity; / 剩余产品数量 public: Product(char *n, int p, int q); Product(); void buy( int money); / 购买产品 void get () const; / 返回剩余产品数量;答:Product类成员函数及主函数定义如下:Product: Product(char *n, int p, int q) name=new charstrlen(n)+1; strcpy(name,n); price=p; quantity=q; cout<<”constructing Product”&l

10、t;<name<<endl;Product: Product cout<<”destructing Product”<<name<<endl; delete name;void Product:buy(int money) if(quantity<=0) cout<<”sorry,the quantity is empty.”<<endl; return;int number=money/price;if (number>quantity) cout<<”the quantity”<&l

11、t;”that using”<<money<<”buys is not enough!”<<endl; return;else cout<<”the”<<money<<”buys”<<number<<” ”<<name<<endl; quantity-=number;void Product:get() constcout<<”the quantity is ”<<quantity<<endl;void main() Product cup

12、(“cup”,3,100);cup.buy(120);cup.get();三、编程题1.设计一个长方体类,有长、宽、高三个属性,用成员函数计算;计算并输出长方体的体积和表面积。#include <iostream.h>#include <string.h>class cuboid int length; int width; int high; public: cuboid(int l=0,int w=0,int h=0) length=l; width=w; high=h; int getv() return length*width*high;int gets()

13、return 2*(length*width+width*high+length*high);void print() cout<<”the cuboid is”<<length<<”*”<<width<<”*”<<high<<endl; cout<<”the volume is ”<<getv()<<endl;cout<<”the surface area is”<<gets()<<endl;void main() cuboid A(4,

14、5,6); A.print();2.设计一个点类point,有x,y两个整型的数据成员,用成员函数返回x,y的值。使用测试程序验证此类。#include <iostream.h>class point int x; int y;public: point(int px,int py) x=px  y=py  int getx() return x int gety()return y  void main() point A(3,5); cout<<”the point is (”<<A.getx()<

15、<”,”<<A.gety()<<”)”<<endl;3.编写一个程序,设计一个满足以下要求的data类,其数据成员为year,month和day;类中有两个公有函数setdate( )和display( ),前一个用来设置日期,后一个用来显示日期。在主程序中定义该类的两个对象,设置两个具体日期,然后输出这两个日期。#include <iostream.h>class data int year; int month; int day; public: data(int y=1900,int m=1,int d=1) year=y; mont

16、h=m; day=d;void print() cout<<”the data is:”<<year<<”/”<<month<<”/”<<day<<endl;void addoneday() int a13=0,31,28,31,30,31,30,31,31,30,31,30,31; if(year%4=0&&year%100!=0)|(year%400=0) a2=29; if(day=amonth) if(month=12) year+; month=1; day=1;else month+

17、; day=1;else day+; cout<<”the day add one.”<<endl;void setdata(int y,int m,int d) year=y; month=m; day=d; cout<<”the data is reset.”<<endl;void main() data d1(1965,2,28); d1.print(); d1.addoneday(); d1.print();4.设计一个学生类,包括学号、姓名、两门课成绩,要求输出学生数据,计算平均分。若在测试程序中定义了一个对象数组,对类的构造函数有什么

18、要求?#include <iostream.h>#include <string.h>class student int num;char *name;int score1,score2;public: student(int n,char *na,int s1,int s2) num=n; name=new charstrlen(na)+1; strcpy(name,na);score1=s1;score2=s2;student() num=0; name=0; score1=0; score2=0;void init(int n,char *na,int s1,in

19、t s2) num=n; name=new charstrlen(na)+1; strcpy(name,na);score1=s1;score2=s2;student() delete name;int getavg() return (score1+score2)/2;void print() cout<<”学生学号:”<<num<<” 姓名:”<<name<<”平均成绩:”<<getavg()<<endl;void main() student x(1001,”王明”,79,82); x.print();5.在上题程序的基础上,定义一个拷贝构造函数,进行学生类对象的复制,并在主函数中测试拷贝构造函数。#include <iostream.h>#include <string.h>class student int num;char *name;int sco

温馨提示

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

评论

0/150

提交评论