Chapter 10 Classes: A Deeper Look, part 2 东方 fdong@seu.edu.cn O 2015, SEU. All rights reserved. 1
© 2009, SEU. All rights reserved. © 2015, SEU. All rights reserved. 1 Classes: A Deeper Look, Part 2 东方 fdong@seu.edu.cn Chapter 10
OBJECTIVES o To specify const (constant)objects and const member functions o To create objects composed of other objects o To use friend functions and friend classes o To use the this pointer To create and destroy objects dynamically with operators new and delete, respectively o To use static data members and member functions o 2015, SEU. All rights reserved. 2
© 2009, SEU. All rights reserved. © 2015, SEU. All rights reserved. 2 OBJECTIVES To specify const (constant) objects and const member functions. To create objects composed of other objects. To use friend functions and friend classes. To use the this pointer. To create and destroy objects dynamically with operators new and delete, respectively. To use static data members and member functions
Topics 10.1 Introduction 10.2 const(Constant)Objects and const Member Functions o 10.3 Composition: objects as members of Classes 10. 4 friend Functions and friend classes o 10.5 Using the this Pointer 10.6 Dynamic Memory Management with Operators new and delete o 10.7 static Class Members o 10.8 Data Abstraction and Information Hiding o 2015, SEU. All rights reserved. 3
© 2009, SEU. All rights reserved. © 2015, SEU. All rights reserved. 3 Topics 10.1 Introduction 10.2 const (Constant) Objects and const Member Functions 10.3 Composition: Objects as Members of Classes 10.4 friend Functions and friend Classes 10.5 Using the this Pointer 10.6 Dynamic Memory Management with Operators new and delete 10.7 static Class Members 10.8 Data Abstraction and Information Hiding
10.1 Introduction o const对象 °类的组合 composition:一个类以其他类的对象 作为成员 °友元( friend)函数:非成员函数如何访问类的私有 成员? o this指针:所有非 static函数的隐含实参 °动态内存管理:new和 delete运算符 °静态 static类成员 o 2015, SEU. All rights reserved. 4
© 2009, SEU. All rights reserved. © 2015, SEU. All rights reserved. 4 10.1 Introduction const对象 类的组合(composition): 一个类以其他类的对象 作为成员 友元(friend)函数: 非成员函数如何访问类的私有 成员? this指针: 所有非static函数的隐含实参 动态内存管理: new和delete运算符 静态static类成员
Topics 10.1 Introduction o 10.2 const( Constant)Objects and const Member Functions 10.3 Composition: Objects as Members of Classes 10.4 friend functions and friend classes o 10.5 Using the this pointer 10.6 Dynamic Memory Management with Operators new and delete o 10.7 static Class Members o 10.8 Data Abstraction and Information Hiding o 2015, SEU. All rights reserved. 5
© 2009, SEU. All rights reserved. © 2015, SEU. All rights reserved. 5 Topics 10.1 Introduction 10.2 const (Constant) Objects and const Member Functions 10.3 Composition: Objects as Members of Classes 10.4 friend Functions and friend Classes 10.5 Using the this Pointer 10.6 Dynamic Memory Management with Operators new and delete 10.7 static Class Members 10.8 Data Abstraction and Information Hiding
10.2 const(Constant)Objects and const member functions常量对象 °目的:一些对象要求是可修改的,另一些则 不希望被修改 o const Time noon 12,, 0 ●将变量和对象声明为 const可提高性能 °要求:不允许调用普通成员函数,仅能调用 noon对象的 const member function(常成 员函数) O 2015, SEU. All rights reserved. 6
© 2009, SEU. All rights reserved. © 2015, SEU. All rights reserved. 6 10.2 const (Constant) Objects and const Member Functions—常量对象 目的:一些对象要求是可修改的,另一些则 不希望被修改 const Time noon( 12, 0, 0 ); 将变量和对象声明为const可提高性能 要求:不允许调用普通成员函数,仅能调用 noon对象的const member function (常成 员函数)
10.2 const(Constant)Objects and const Member functions常成员函数 目的:通过把成员函数声明为 const,以表明它们 不修改类的成员变量 常成员函数(两处都要声明) o prototype void printUniversalO const o definition. void Time: printUniversal0 const (. J 要求: °不能修改本对象的数据成员 °不能调用本对象其它non-cons成员函数 O 2015, SEU. All rights reserved. 7
© 2009, SEU. All rights reserved. © 2015, SEU. All rights reserved. 7 10.2 const (Constant) Objects and const Member Functions—常成员函数 目的:通过把成员函数声明为const,以表明它们 不修改类的成员变量 常成员函数(两处都要声明) prototype: void printUniversal() const; definition: void Time::printUniversal() const { … } 要求: 不能修改本对象的数据成员 不能调用本对象其它non-const成员函数
10.2 const(Constant)Objects and const member functions一访问权限 OBJECT Member Function Access const const const non-const non-const const X√√ non-const non-const o 2015, SEU. All rights reserved. 8
© 2009, SEU. All rights reserved. © 2015, SEU. All rights reserved. 8 10.2 const (Constant) Objects and const Member Functions—访问权限 OBJECT Member Function Access const const √ const non-const X non-const const √ non-const non-const √
10.2 const(Constant) Objects and const Member Functions class time public Time (int =0, int =0, int =0 void setTime( int, int, int) void sethour(int ) void setMinute (int ) void setsecond(int int getHour( const; int getMinuteO const; int getSecondO const; void printUniversal0 const; void printstandardo万 P40110.13 private: int hour: int minute, int second, } o 2015, SEU. All rights reserved. 9
© 2009, SEU. All rights reserved. © 2015, SEU. All rights reserved. 9 10.2 const (Constant) Objects and const Member Functions class Time { public: Time( int = 0, int = 0, int = 0 ); void setTime( int, int, int ); void setHour( int ); void setMinute( int ); void setSecond( int ); int getHour() const; int getMinute() const; int getSecond() const; void printUniversal() const; void printStandard(); private: int hour; int minute; int second; };
10.2 const(Constant)Objects and const Member Functions void Time: test(Time &another) const minute 20 /不能修改 object printstandardo万 ∥/不能调用non- const成员函数 another minute= 20. ∥/oK,非同一 object another. sethour 6: ∥/oK 2015, SEU. All rights reserved. 10
© 2009, SEU. All rights reserved. © 2015, SEU. All rights reserved. 10 10.2 const (Constant) Objects and const Member Functions void Time::test(Time &another) const { minute = 20; // 不能修改object printStandard(); // 不能调用non-const成员函数 another.minute = 20; // OK,非同一object another.setHour(6); // OK }