书面作业_4.doc_第1页
书面作业_4.doc_第2页
书面作业_4.doc_第3页
书面作业_4.doc_第4页
已阅读5页,还剩2页未读 继续免费阅读

下载本文档

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

文档简介

1、本文格式为word版,下载可任意编辑书面作业_4.doc 书面作业_4 1 、 简答题: (1) 构造函数的作用是什么? 它有哪些特点? 作用是对对象进行初始化 函数名必需与其类名相同,一个类可以有多个构造函数(即构造函数重载),也可以没有构造函数,构造函数可以有参数,也可以没有参数;在创建对象时,系统会自动调用构造函数 (2) 析构函数的功能是什么? 它有哪些特点? 在对象消亡时,自动完成清除工作。 函数名必需与其类名相同,但该函数前面加。没有参数,也没有返回值,而且不能重载,在一个类中只能有一个析构函数。当撤销对象时,编译器会自动调用析构函数。 (3) 拷贝构造函数有什么作用? 在哪些状况

2、下会调用拷贝构造函数? 用一个已有的对象来初始化一个被创建的同类对象,是一种特别的成员函数。 用类的一个对象去初始化另一个对象;用对象作为函数实参传递给形参时,调用拷贝构造函数;假如函数的返回值是类的对象,函数调用返回时,调用拷贝构造函数。 (4) 类型转换构造函数有什么作用? 它有什么特点? 实现类型的自动转换; 只有一个参数 不是复制构造函数 编译系统会自动调用类型转换构造函数,建立一个临时对象 / 临时变量 (5) 在用 new、delete 运算符创建、删除对象时,是否会调用构造函数和析构函数? 用 new 运算符动态创建对象时也会自动调用构造函数; 用 delete 运算符删除对象时

3、也会自动调用析构函数 。 2 、 教材 p206 第 第 6 题 #includeiostream using namespace std; class csample int x; public: csample() cout c1 endl; csample(int n) x = n; cout c2,x= n endl; ; int main() csample array12; csample array22 = 6,8 ; csample array32 = 12 ; csample *array4 = new csample3; return 0; 3 、 教材 p206 第 第

4、7 题 #include iostream using namespace std; class sample public: int v; sample() ; sample(int n) :v(n) ; sample(sample x) v = 2 + x.v; ; sample printanddouble(sample o) cout o.v; o.v = 2 * o.v; return o; int main() sample a(5); sample b = a; sample c = printanddouble(b); cout endl; cout c.v endl; sam

5、ple d; d = a; cout d.v; 4 、请按下列要求编写程序: (1)声明一个老师类 teacher,该类的私有数据成员有:工号(num)、姓名(char *pname)、年龄(age)。请定义相应的成员函数来设置、读取这些私有成员,并为该类定义构造函数、拷贝构造函数、类型转换构造函数(只包含工号一个参数)、析构函数; (2)在 main 函数里先定义 teacher 类的一个对象 wang(201,'王伟',35),然后以它为基础复制一个对象 li,再将 li 的工号修改为 202,姓名修改为"李华',最终把这两个对象的信息输出到屏幕上。 (提

6、示:姓名是字符指针类型,需要动态安排、释放内存空间,分别在构造函数、析构函数中实现,还要在拷贝构造函数中安排新内存空间,以达到深拷贝目的。请参考 程序代码 4.doc (5) 改进版本) #includecassert #includeiostream using namespace std; class teacher; class teacher public: teacher(int num, const char* name, int age) assert(num = 0); assert(name); assert(age = 0 age = 200); this-num = nu

7、m; int len = this-str_len(name); this-name = new charlen + 1; str_copy(this-name, name); this-age = age; teacher(const teacher teacher) this-num = teacher.num; assert(); int len = this-str_len(); this-name = new charlen + 1; this-str_copy(this-name, ); this-age =

8、teacher.age; virtualteacher() if (this-name) delete this-name; this-name = null; teacher operator=(const teacher teacher) if (this != teacher) this-num = teacher.num; assert(); if (this-name) delete this-name; int len = this-str_len(); this-name = new charlen + 1; this-str_co

9、py(this-name, ); this-age = teacher.age; return *this; public: void setnum(int num) assert(num = 0); this-num = num; void setage(int age) assert(age = 0 age = 200); this-age = age; void setname(const char* name) assert(name); if (this-name) delete this-name; int len = this-str_len(name);

10、 this-name = new charlen + 1; this-str_copy(this-name, name); int getage()const return this-age; int getnum()const return this-num; const char* getname()const return this-name; void showteacherinfo()const cout 工号: this-num endl; cout 姓名: this-name endl; cout 龄: this-age endl; private: int str_len(const char* src) assert(src); int len = 0; while (*src+len); return len; void str_copy(char* dest, const char* src) assert(src); while (*dest+ = *src+); private: int num; char* name; int age; ; void main() teac

温馨提示

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

评论

0/150

提交评论