
bth001复习提纲.doc
14页1BTH001 复习提纲 BTH001 期末考试题型:(1)判断分析题(3 题,共 6 分)判断,并简要说明(2)名词解释(3 题,共 9 分)概念简要解释(3)综合编程题(4 大题,共 85 分)每个大题下面细分若干小题包括代码判断分析、代码编写等1、类相关概念类、属性、成员函数、常量成员函数 (class/property/constant member function) 练习,1.1 理解常量成员函数(constant member function) ,在下面的 Member类中,哪些函数可以定义为常量成员函数,如何定义?1.2 实现下面类中的符号重载 operator==1.1 答案:string getName() const;2string getAddress() const;string getPhoneNr() const;string toString() const;bool operator==(const Member1.2 答案:bool Member::operator ==(const Member& member) const {return this->name == member.name }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- Destructor 练习,通过默认构造函数创建对象,见 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- 练习,见 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.3ClassName(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 (copy‐by‐value) copying of member variables that are not pointers- Use deep copying for member variables that are pointers 练习,见 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 (copy‐by‐value) copying of member variables that are not pointers Use deep copying for member variables that are pointers 练习,见 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.) 练习,基于下面的类图,创建对象。
4Professional 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.5 练习,聚合关系的判断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. 练习,聚合关系的 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 class6- 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 subclass 练习,类继承权限概念的判断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 doesn’t know members of the sub class. 9.2 FALSE, -Private members in base class are not accessible in the subclass. 练习,类继承的实现与方法调用Employee 与 Person 之间的继承关系 C++是如何实现的?E。












