class Increment public Increment (int c=0, inti= 1://default constructor // function addIncrement definition void addIncremento count + increment 1//end function addIncrement void print( const; //prints count and increment private: int count. const int increment,//const data member int &refcount, ://end class Increment O 2015, SEU. All rights reserved. 16
© 2009, SEU. All rights reserved. © 2015, SEU. All rights reserved. 16 class Increment { public: Increment( int c = 0, int i = 1 ); // default constructor // function addIncrement definition void addIncrement() { count += increment; } // end function addIncrement void print() const; // prints count and increment private: int count; const int increment; // const data member int &refCount; }; // end class Increment
//constructor Fig 10.5 Increment: Increment(int c, int i) count(c),// initializer for non-const member increment(i)//required initializer for const member refCount count )// required initializer for ref member cout < Now count= << count < increment ="<< increment and refcount="<< refcount < endla. refcount= 99: Increment value 10, 5 ); value. print(; // print count and increment values void Increment: printO const cout < count="<< count << increment ="<< increment < endla Now count increment and refCount count increment y ZUIE SEU. HUmans reserved. 17
© 2009, SEU. All rights reserved. © 2015, SEU. All rights reserved. 17 // constructor Fig 10.5 Increment::Increment( int c, int i ) : count( c ), // initializer for non-const member increment( i ), // required initializer for const member refCount ( count ) // required initializer for ref member { cout << "Now count = " << count << ", increment = " << increment << " and refCount= " << refCount<< endl; refCount = 99; } // print count and increment values void Increment::print() const { cout << "count = " << count << ", increment = " << increment << endl; } Increment value( 10, 5 ); value.print(); Now count = 10, increment = 5 and refCount = 10 count = 99, increment = 5
思考: °为何 const data member(例外? const static integer)与 reference data member引用类型 的数据成员必须用初始化列表操作? °从 const-与ref的特性出发考虑 o 2015, SEU. All rights reserved. 18
© 2009, SEU. All rights reserved. © 2015, SEU. All rights reserved. 18 思考: 为何const data member (例外?const static integer)与 reference data member 引用类型 的数据成员必须用初始化列表操作? 从const与ref的特性出发考虑
Topics o 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 2015, SEU. All rights reserved. 19
© 2009, SEU. All rights reserved. © 2015, SEU. All rights reserved. 19 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.3 Composition: Objects as Members of classes ° Composition,组合: a class has objects of other class as members Host Object(宿主对象) vs Contained Object(被包含对象) Employees astName: String FirstName: String birthDate: Date hireDate: Date 组合对象的初始化问题? O 2015, SEU. All rights reserved. 20
© 2009, SEU. All rights reserved. © 2015, SEU. All rights reserved. 20 10.3 Composition: Objects as Members of Classes Composition, 组合: A class has objects of other class as members. Host Object(宿主对象) vs Contained Object(被包含对象) Employees - LastName: String - FirstName: String - birthDate: Date - hireDate: Date 组合对象的初始化问题?