More about Classes and Object Oriented Programming More

More about Classes and Object Oriented Programming

More C++ • The this pointer • Constant member functions • Static member variables • Static member functions • Friend functions

11. 1 The this Pointer and Constant Member Functions • this pointer: - Implicit parameter passed to every member function - it points to the object calling the function • const member function: - does not modify its calling object 1 -3

Using the this Pointer Can be used to access members that may be hidden by parameters with the same name: class Some. Class { private: int num; public: void set. Num(int num) { this->num = num; } }; 1 -4

Review from Chapter 7 – Passing Objects to Functions • Passing an object by value makes a copy of the object. This means the copy constructor is called and all the values of the data members are copied into the new object. void show. Values(Inventory. Item item) { … • This can slow down a program’s execution time, especially if it is large or has many members. • Any mutator functions called in the function will change only the copied object, not the original object. 11 -5

Review from Chapter 7 – Constant Reference Parameters • Passing an object by reference is faster because it doesn’t have to make a copy of all the object’s members. • Passing an object by reference is preferable. • To help guard against changing the private data members of the object, the reference parameter should be passed as a constant reference: void show. Values(const Inventory. Item &item) { … • This means that a reference to the object is passed in to the function but it cannot call any of the mutator functions. • It also cannot call any accessor functions unless the word const is also place after the function name: double get. Price() const { … 11 -6

Constant Member Functions • Declared with keyword const • When const appears in the parameter list, int set. Num (const int num) the function is prevented from modifying the parameter. The parameter is read-only. • When const follows the parameter list, int get. X()const the function is prevented from modifying the object. 11 -7

11. 2 Static Members • Static member variable: – One instance of variable for the entire class – Shared by all objects of the class • Static member function: – Can be used to access static member variables – Can be called before any class objects are created 11 -8

Static Member Variables 1) Must be declared in class with keyword static: class Int. Val { public: int. Val(int val = 0) { value = val; val. Count++; } int get. Val(); void set. Val(int); }; private: int value; static int val. Count; 11 -9

Static Member Variables 2) Must be defined outside of the class: class Int. Val { public: int. Val(int val = 0) { value = val; val. Count++; } int get. Val(); void set. Val(int); }; private: int value; static int val. Count; //Definition outside of class int Int. Val: : val. Count = 0; 11 -10

Static Member Variables 3) Can be accessed or modified by any object of the class: Modifications by one object are visible to all objects of the class: Int. Val val 1, val 2; val. Count val 1 11 -11 2 val 2

Static Member Functions 1)Declared with static before return type: class Int. Val { public: static int get. Val. Count() { return val. Count; } private: int value; static int val. Count; }; 11 -12

Static Member Functions 2) Can be called independently of class objects, through the class name: cout << Int. Val: : get. Val. Count(); 3) Because of item 2 above, the this pointer cannot be used 4) Can be called before any objects of the class have been created 5) Used primarily to manipulate static member variables of the class 11 -13

11. 3 Friends of Classes • Friend function: a function that is not a member of a class, but has access to private members of the class • A friend function can be a stand-alone function or a member function of another class • It is declared a friend of a class with the friend keyword in the function prototype 11 -14

Friend Function Declarations 1) Friend function may be a stand-alone function: class a. Class { private: int x; friend void f. Set(a. Class &c, int a); }; void f. Set(a. Class &c, int a) { c. x = a; } 11 -15

Friend Function Declarations 2) Friend function may be a member of another class: class a. Class { private: int x; friend void Other. Class: : f. Set(a. Class &c, int a); }; class Other. Class { public: void f. Set(a. Class &c, int a) { c. x = a; } }; 11 -16

Friend Class Declaration 3) An entire class can be declared a friend of a class: class a. Class { private: int x; friend class fr. Class; }; class fr. Class { public: void f. Set(a. Class &c, int a) {c. x = a; } int f. Get(a. Class c) {return c. x; } }; 11 -17

Friend Class Declaration • If fr. Class is a friend of a. Class, then all member functions of fr. Class have unrestricted access to all members of a. Class, including the private members. • In general, restrict the property of Friendship to only those functions that must have access to the private members of a class. 11 -18
- Slides: 18