




已阅读5页,还剩9页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
BTH001复习提纲n BTH001期末考试题型:(1)判断分析题(3题,共6分)判断,并简要说明(2)名词解释(3题,共9分)概念简要解释(3)综合编程题(4大题,共85分)每个大题下面细分若干小题包括代码判断分析、代码编写等1、类相关概念类、属性、成员函数、常量成员函数 (class/property/constant member function)n 练习,1.1 理解常量成员函数(constant member function),在下面的Member类中,哪些函数可以定义为常量成员函数,如何定义?1.2 实现下面类中的符号重载operator=1.1 答案:string getName() const;string getAddress() const;string getPhoneNr() const;string toString() const;bool operator=(const Member& member) const;1.2 答案:bool Member:operator =(const Member& member) const return this-name = & this-address = member.address;2、默认构造函数(default constructor)If no constructor is defined in a class it will get an automatically generated default constructor. Furthermore a class will automatically get agenerated- Copy constructor- Assignment operator- DestructorIf a class has member variables that are pointers it is necessary to replace the- Default constructor- Copy constructor- Assignment operator- Destructorn 练习,通过默认构造函数创建对象,见No.6知识点练习。Member memb;3、析构函数(Destructor)If the class has at least one member variable that is a pointer it is necessary to:- Deallocate the memory that has been allocated for the pointers by the object-n 练习,见No.16容器类知识点练习4、复制构造函数(Copy constructor)Each class has a copy constructor. Purpose is to create an identical copy of another object.Automatically generated if not defined.If a member variable is a pointer this will result in shallow copying.ClassName(const ClassName ¶meterName)If the class has at least one member variable that is a pointer it is necessary to:- replace the automatically generated copy construcor by implementing it- Use memberwise (copybyvalue) copying of member variables that are not pointers- Use deep copying for member variables that are pointersn 练习,见No.16容器类知识点练习5、赋值号重载(Assignment operator)Each class has an assignment operator. Purpose is to create an identical copy of another object.Automatically generated if not defined. If a member variable is a pointer this will result in shallow copying.void operator=(const ClassName ¶meterName)If the class has at least one member variable that is a pointer it is necessary to:- replace the automatically generated assignmentoperator by implementing it- If the object is not assigned to itself Deallocate dynamically allocated memory Use memberwise (copybyvalue) copying of member variables that are not pointers Use deep copying for member variables that are pointersn 练习,见No.16容器类知识点练习6、创建对象、new、基类指针利用默认构造函数创建对象(create an object using default constructor)用new创建对象,并赋值给类指针(create an object for the pointer)声明基类指针,并指向派生类对象(create an sub class object for the pointer which is a pointer of the base class type.)n 练习,基于下面的类图,创建对象。Professional prof;Professional *profPtr = new Professional(E,2,E);Runner * runnerPtr = NULL;runnerPtr = new Professional(D,1,D);7、聚合关系Relationship (composition/aggregation, has-a) A house ”has” windows aggregation, windows can exist without a house door aggregation, door can exist without a house walls composition, walls does not exist without a houseA Car “consists of/has” a chassi (composition, 组成关系) four wheels (aggregation, 聚合关系)A Car has 1 Chassi (composition) A Car has 4 Wheels (aggregation)The difference between aggregation and composition is that the Chassi will not exist without the Car but the wheels will.n 练习,聚合关系的判断7.1 A Student “is a” Person is an example of the relationship composition.7.2 A Car “has” four wheels is an example of the relationship composition.答案:7.1 FALSE, it is an example of the relationship inheritance. 7.2 FALSE, it is an example of the relationship aggregation, wheels can exist without a car. n 练习,聚合关系的C+实现方式在Club.h中添加动态数组的声明,该动态数组中保存Member对象或Member*类指针,两个属性capacity 和nrOfMember分别表示数组容量和数组中当前保存的元素个数。上图聚合关系的实现方案一(动态数组保存Member对象):Member * members; int capacity; int nrOfMembers;上图聚合关系的实现方案二(动态数组保存Member*类指针):Member * * members;int capacity; int nrOfMembers;8、继承关系Relationship (Inheritance, is-a)base class(基类), sub classes(派生类,子类)- An Employee is a Person- A Student is a Person- Things (properties and behavior) that are common for both Students and Employees is placed in the Person class- Things (properties and behavior) that are specific for Students are placed in the Student class- Things (properties and behavior) that are specific for Employees are placed in the Employee class- Both Student and Employee inherits from Person9、继承的实现与函数调用继承的C+实现方式:class SubClass : public BaseClass- All members are inherited, though with different access- Public members in the base class are accessible in the subclass- Private members in base class are not accessible in the subclassn 练习,类继承权限概念的判断9.1 Members in a sub class are accessible from the base class.9.2 Private members in a base class are accessible from a sub class.答案: 9.1 FALSE, a base class doesnt know members of the sub class. 9.2 FALSE, -Private members in base class are not accessible in the subclass.n 练习,类继承的实现与方法调用Employee与Person之间的继承关系C+是如何实现的?Employee类中能调用Person类中getName() 吗?Employee类中能调用Person类中的name属性吗?class Employee: public Person.- An Employee object can perform getName() and getSalary()- In the Employee class it is not possible to access the member name it is private in the Person class- In the Employee class it is however possible to access the function getName()n 练习,类继承的实现与方法调用How to implement the relationship between Runner and Professional?在Professional类中的toStringSpec()方法中,能使用Runner中的name或startNr属性吗?class Professional: public Runner.n 练习,派生类指针调用基类public对象Professional *profPtr = new Professional(E,2,E);profPtr - getName();n 练习,基类指针不能调用派生类中的方法Runner * runnerPtr = new Professional(D,1,D);runnerPtr-setClub(); / 错误n 练习,基类指针调用虚函数Runner * runnerPtr = new Professional(D,1,D);runnerPtr-toString(); / 正确,toString()为虚函数n 练习,实现Runner与Professional中的复制构造函数?Runner.hRunner(const Runner& runner);Runner.cppRunner:Runner(const Runner& runner)this-name = ;this-startNr = runner.startNr;Professional.hProfessional(const Professional& prof);Professional.cppProfessional:Professional(const Professional& prof):Runner(prof)this-club = prof.club;10、函数重写(Function overriding)- It is possible to override (redefine) functions in an inheritance hierarchy- If a function is overridden and an object makes a call of the overridden function the definition that is consistent with the declaration of the variable is used. This is static (early) bindingn 练习,理解overriding, static binding概念11、虚函数(virtual Function)- An overridden function that is declared virtual in the base class will accomplish dynamic binding (late binding)- Dynamic (late) binding means that it is the type of the object (not the type of the variable) that decides which definition of the function that will be usedn 练习,理解dynamic binding, virtual function概念12、纯虚函数、抽象类(Pure virtual functions, abstract class)- A pure virtual function is a function that requires a definition in the derived class- If a class contains one or more pure virtual functions the class become abstract.- A virtual function enables dynamic (late) binding instead of static (early) binding- A pure virtual function makes a class abstract- A pure virtual function requires a definition in derived classes, otherwise they will become abstract as well- An abstract class can not be instantiated- Pointer variables can be declared of abstract class type抽象类(Abstract classes)- Base classes can be abstract- Abstract classes can not be instantiated- Abstract classes can be used as a type. Variables can be declared as pointers of abstract class type-BaseClass *variable;n 练习,理解abstract class, pure virtual function概念n 练习,如何将一个类定义为抽象类?- If a class contains one or more pure virtual functions the class become abstract.13、动态内存分配The primary memory is divided into two main parts:- One part for which memory is allocated statically, this is done by the compiler. Memory is reserved for variables and functions. One part that is used for allocation of memory during execution, e.g. used for dynamic memory allocation.Memory allocationMemory deallocationn 练习,动态数组的创建时A dynamically allocated array is created during compile time.答案: FALSE, it is created during runtime.14、深拷贝、浅拷贝(Deep and shallow copying)When we want a pointer variable to point to an identical thing as another pointer variable- the variables will have different addresses- they point to different things but the content of those things will be identical, for exampleCircle *myCircles1 = new Circle5;Circle *newCircles1 = myCircles;/ points to the same array of Circle-objectCircle *myCircle2 = new Circle(10);Circle *newCircle2 = new Circle(myCircle-getRadius(); / points to different Circle-objectn 练习,将下面数组myCircles3通过深拷贝拷贝到一个新的数组中Circle * myCircles3 = new Circle5;myCircles30 = Circle(12);myCircles31 = Circle(15);myCircles32 = Circle(20);答案:Circle * newCircles3 = new Circle5;for(int i =0; i 5; i+)newCircles3i = myCircles3i;n 练习,将下面数组myCircles4通过深拷贝拷贝到一个新的数组中Circle *myCircles4 = new Circle5;myCircles40 = new Circle(12);myCircles41 = new Circle(15);myCircles42 = new Circle(20);答案:Circle *newCircles4 = new Circle5;for(int i =0; igetRadius();15、容器类(Container class)Contains member variables that can hold objects (组合关系的实现)- Statically allocated array of objects- Dynamically allocated array of objects- Statically allocated array of pointers- Dynamically allocated array of pointers n 练习,实现Club类中的成员函数(需要熟练掌握成员函数的编写)Club:Club(int capacity)this-capacity = capacity;this-nrOfMembers =0;this-members= new Member *this-capacity;for (int i =0; icapacity; i+)this-
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025年农药学名词解释试题
- 2025年计算机图形图像处理试题
- 智能车库门远程控制与维护服务合同
- 第3单元 语文园地(含解析答案)统编版(2024)语文一年级下册
- 智能车库租赁与车位共享服务协议
- 七年级下学期5月月考语文试题(pdf版含答案)
- 拆除工程现场安全管理合同范本
- 教育心理学在体育教学中的作用
- 循环利用医疗废物的处理方法
- 车牌租赁与违章处理服务协议
- 2025浙江中考:历史必背知识点
- 卫星遥感图像传输质量评估-全面剖析
- 2025-2030中国跨境支付行业市场发展现状及竞争格局与投资前景研究报告
- 2025年果品购销合同简易模板
- 胰岛素皮下注射团体标准解读 2
- 《眼科手术新技术》课件
- 《SLT631-2025水利水电工程单元工程施工质量验收标准》知识培训
- 2025氮气、氩气供应合同
- 2024年贵州省普通高校招生信息表(普通类本科提前批C段-物理组合)
- 过敏原检测试临床意义
- 2024北京丰台区初一(下)期末英语试题和答案
评论
0/150
提交评论