960 likes | 1.17k Views
Programming Techniques Classes II. Important Class Features Spring 2009. References. C++ How to Program, Deitel, Prentice Hall, 2003 by Pearson Education, Fourth Edition Object Oriented Programming in C++, Robert Lafore, Waite Group Press, 1995, Second Edition. Classes II. Agenda:
E N D
Programming Techniques Classes II Important Class Features Spring 2009
References • C++ How to Program, Deitel, Prentice Hall, 2003 by Pearson Education, Fourth Edition • Object Oriented Programming in C++, Robert Lafore, Waite Group Press, 1995, Second Edition
Classes II • Agenda: • const objects and const member functions • friend functions and friend classes • Composition: Objects as Members of Classes • Dynamic Memory Management with Operators new and delete • this pointer • static data members and static functions • The Container class • The iterator class
1. const objects and const member functions • Keyword const • Specify object not modifiable • Compiler error if attempt to modify const object • Example const Time noon( 12, 0, 0 ); • Declares const object noon of class Time • Initializes to 12
1. const objects and const member functions • const member functions • Member functions for const objects must also be const • Cannot modify object • Specify const in both prototype and definition • in Prototype: • After parameter list • in Definition • Before beginning left brace • Constructors and destructors Cannot be const as • they Must be able to modify objects
1. const objects and const member functions • Example 1
1. const objects and const member functions • Example 1
1. const objects and const member functions const functions do not modify objects. • Example 1
Declare noon a const object. Note that non-const constructor can initialize const object. 1. const objects and const member functions • Example 1
1. const objects and const member functions • Example 1 Attempting to invoke non-const member function on const object results in compiler error. Attempting to invoke non-const member function on const object results in compiler error even if function does not modify object.
1. const objects and const member functions • Const member functions • Can be used for: • All class objects • Must be used for: • Const class objects
1. const objects and const member functions • Member initializer syntax • Can be used for: • All data members • Must be used for: • const data members • Data members that are references
1. const objects and const member functions • Example 2
1. const objects and const member functions • Example 2
1. const objects and const member functions • Example 2
Classes II • Agenda: • const objects and const member functions • friend functions and friend classes • Composition: Objects as Members of Classes • Dynamic Memory Management with Operators new and delete • this pointer • static data members and static functions • The Container class • The iterator class
2. friend functions and friend classes • friend functions: • are defined outside class’s scope • they have the right to access non-public members
2. friend functions and friend classes • Declaring friends • A function F ( ) : by preceding function prototype with keyword friend inside class • A class“classTwo” as friend of another class “class One”: by placing declaration of form: • Example: • friend F ( ); • friend class ClassTwo; inside ClassOne definition
2. friend functions and friend classes • Properties of friendship • Friendship granted, not taken i.e. • Class B friend of class A • Class A must explicitly declare class B friend • Not symmetric • Class B friend of class A • Class A not necessarily friend of class B
2. friend functions and friend classes • Properties of friendship (cont.) • Not transitive • Class A friend of class B • Class B friend of class C • Class A not necessarily friend of Class C friend
2. friend functions and friend classes • Example 1:
2. friend functions and friend classes • Example 1: NOT a member function!
2. friend functions and friend classes • Example 1:
2. friend functions and friend classes • Example 2: A global function as friend class Distance // English Distance class { friend Distance Add_Dist (Distance , Distance); private: int feet; float inches; public: Distance ( ) { } // constructor (no args) Distance (int ft, float in); ..........................
2. friend functions and friend classes • Example 2: function implementation Distance Add_Dist(Distance d2, Distance d3) { float inches = d2.inches + d3.inches; int feet = 0; if (inches >= 12.0) { inches -= 12.0; feet++; } feet += d2.feet + d3.feet; // add the feet return Distance (feet, inches); } Non-member function! Creating a member and returning its value!
2. friend functions and friend classes • Example 2: main ( ) { Distance d1 (7, 5), d2 (3, 4); Distance d3; d3 = Add_Dist ( d1, d2); } How was it in the previous example of Add_dist? Compare!
2. friend functions and friend classes • Example 3: class as a friend of another class class X { int a, b; friend class F; public: X(int i=1, int j =2 ) : a(i), b(j) { } };
2. friend functions and friend classes • Example 3: class as a friend of another class class F { public: void print( X& x ) { cout << "a is " << x . a << endl; cout << "b is " << x . b << endl; } };
2. friend functions and friend classes • Example 3: class as a friend of another class int main( ) { X xobj; F fobj; fobj . Print ( xobj ); } Another alternative was to make only F::Print ( ) a friend to class X
Classes II • Agenda: • const objects and const member functions • friend functions and friend classes • Composition: Objects as Members of Classes • Dynamic Memory Management with Operators new and delete • this pointer • static data members and static functions • The Container class • The iterator class
3. Composition: Objects as Members of Classes • Composition: an object inside another one ==> very strong automatic relationship • Aggregation: an object inside another one, also. If implementation is done using pointers, then the outer object (may or may not be) is responsible for const and destruc. of inner objects. • Association: an object with pointer to another object(s). Outer object is not resp. for const or destruc of the other object.
3. Composition: Objects as Members of Classes • Composition versus Aggregation: • In both we talk about objects of one class declared inside another class. • Aggregation differs from ordinary composition in that it pointers may be used, whilein composition they are automatic. • In both, when the owning object is destroyed, so are the contained objects.
3. Composition: Objects as Members of Classes • Composition versus Aggregation Example: • A university owns various departments (e.g., chemistry) • Composition: If departments are implemented as static array • Aggregation: If departments are implemented as pointers
3. Composition: Objects as Members of Classes • What about Association ? • An object with pointer to another object(s). Outer object is NOT resp. for const or destruc of the other object. Example: • Association: Each department has a number of professors. • When university is deleted, professors are not deleted ( Refer to website notes)
3. Composition: Objects as Members of Classes • Construction of member objects • Member objects are constructed in order of declaration • Member objects are constructed before the enclosing outer class objects (host objects)
Create Date objects to pass to Employee constructor. 3. Composition: Objects as Members of Classes • Example:
3. Composition: Objects as Members of Classes • Aggregation: As opposed to composition • class Sales_info { float amount; Date ddmmyy; ………… };
3. Composition: Objects as Members of Classes • Aggregation: As opposed to composition class Sales_Person { Sales_info * sales; ………… public: Sales_Person (int num_sales = 10); ~Sales_Person ( ); };
3. Composition: Objects as Members of Classes • Aggregation: As opposed to composition Sales_Person :: SalesPerson ( int c = 10) { sales = new sales_info [ c ]; } Sales_Person :: ~Sales_Person ( ) { delete [ ] Sales; }
3. Composition: Objects as Members of Classes • Association: As opposed to composition and aggregation class Prof; // forward declaration class Course { char course_id [ 8 ]; ….. Prof * prof; public: Course ( char* id, ….., Prof* p); ~ Course ( ); }
3. Composition: Objects as Members of Classes • Association: As opposed to composition and aggregation Course :: Course ( char* id, ….., Prof* p) { ………. prof = p; } ~ Course ( ) { // No deletion of prof pointer!! // He is still allowed to exist! ! }