classes: a deeper look, part 2

30
CLASSES: A DEEPER LOOK, PART 2 Chapter 10 Part II 1

Upload: dee

Post on 22-Feb-2016

65 views

Category:

Documents


0 download

DESCRIPTION

Classes: A Deeper Look, Part 2. Chapter 10 Part II. Outline . 10.4 friend Functions and friend Classes. A non-member function that can access the private members of a class To make a function a friend function Include its prototype in the class Precede it with the friend keyword - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Classes: A Deeper Look,  Part 2

CLASSES: A DEEPER LOOK, PART 2Chapter 10Part II

1

Page 2: Classes: A Deeper Look,  Part 2

2

Outline

Page 3: Classes: A Deeper Look,  Part 2

3

Page 4: Classes: A Deeper Look,  Part 2

10.4 friend Functions and friend Classes

4

• A non-member function that can access the private members of a class

• To make a function a friend function• Include its prototype in the class• Precede it with the friend keyword• Member access specifiers are not relevant to friend declarations,

so they can be placed anywhere in a class definition• Defined outside class’s scope

• Not a member function.• Access two or more classes with less overhead.• Has the right to access the non-public (and public) members of the

class.

Page 5: Classes: A Deeper Look,  Part 2

5

1 // Fig. 10.15: fig10_15.cpp 2 // Friends can access private members of a class. 3 #include <iostream> 4 using std::cout; 5 using std::endl; 6 7 // Count class definition 8 class Count 9 { 10 friend void setX( Count &, int ); // friend declaration 11 public: 12 // constructor 13 Count() 14 : x( 0 ) // initialize x to 0 15 { 16 // empty body 17 } // end constructor Count 18 19 // output x 20 void print() const 21 { 22 cout << x << endl; 23 } // end function print 24 private: 25 int x; // data member 26 }; // end class Count

friend function declaration (can appear anywhere in the class)

Page 6: Classes: A Deeper Look,  Part 2

27 28 // function setX can modify private data of Count 29 // because setX is declared as a friend of Count (line 10) 30 void setX( Count &c, int val ) 31 { 32 c.x = val; // allowed because setX is a friend of Count 33 } // end function setX 34 35 int main() 36 { 37 Count counter; // create Count object 38 39 cout << "counter.x after instantiation: "; 40 counter.print(); 41 42 setX( counter, 8 ); // set x using a friend function 43 cout << "counter.x after call to setX friend function: "; 44 counter.print(); 45 return 0; 46 } // end main counter.x after instantiation: 0 counter.x after call to setX friend function: 8

6

friend function can modify Count’s private data

Calling a friend function; note that we pass the Count object to the function

Page 7: Classes: A Deeper Look,  Part 2

7

10.4 Friend Classes

• Friend functions may be members of other classes• To declare such friend functions, use scope resolution

friend void otherClass::func(int i);

• We can make all member functions of class ClassTwo friends of class ClassOne

• In ClassOne definition, place declaration of formfriend class ClassTwo;

Page 8: Classes: A Deeper Look,  Part 2

8

8

10.4 Friend Classes (Cont.)

• Friendship is established, not taken• For class B to be a friend of class A, class A must explicitly

declare that class B is its friend

• Friendship relation is not symmetric• If class A is a friend of class B, it does not imply that B is a friend

of A

• Friendship relation is not transitive• If class A is a friend of class B, and class B is a friend of class C,

it does not imply that class A is a friend of class C

Page 9: Classes: A Deeper Look,  Part 2

9

9

10.5  Using the this Pointer• Every object has access to its own address through a pointer

called this (a C++ keyword).• The this pointer is not part of the object itself.

• passed as an implicit.• Used inside a member function; it is a pointer to the invoking objectstring getName() { return name; }or equivalently string getName() { return this->name; }

• Objects use the this pointer implicitly or explicitly to reference their data members and member functions.

• cascaded member-function calls

Page 10: Classes: A Deeper Look,  Part 2

10

10

Page 11: Classes: A Deeper Look,  Part 2

11

11

Page 12: Classes: A Deeper Look,  Part 2

12

Page 13: Classes: A Deeper Look,  Part 2

13

Page 14: Classes: A Deeper Look,  Part 2

14

Page 15: Classes: A Deeper Look,  Part 2

15

Page 16: Classes: A Deeper Look,  Part 2

16

Page 17: Classes: A Deeper Look,  Part 2

17

Page 18: Classes: A Deeper Look,  Part 2

18

Page 19: Classes: A Deeper Look,  Part 2

19

10.6 static Class Members

• A property of a class shared by all instances, not a property of a specific object of the class

• Only one copy of a variable shared by all objects of a class• Specific to a class, NOT object of a class• Exists even if no objects of the class exist

• May seem like global variables, but have class scope• static members can be declared public, private or protected.

• A class’s private and protected static members are normally accessed through the class’s public member functions or friends.

• static const data members of integral or enum types can be initialized where they are declared

• If you want a different initial value, a static data member can be initialized once.

• Initialized to zero if no initialization is present

Page 20: Classes: A Deeper Look,  Part 2

20

static Data Members (Cont.)

• Initialized in the definition file of the class • All other static data members must be defined at global namespace scope and can be initialized only in those definitions.

• If a static data member is an object of a class that provides a default constructor, the static data member need not be initialized because its default constructor will be called.

• public static variables• Can also be accessed using binary scope resolution operator Employee::count

• private static variables• When no class member objects exist

• Can only be accessed via public static member function

Page 21: Classes: A Deeper Look,  Part 2

21

static Member Functions• Is a service of the class, not of a specific object of the class

• A class’s static members exist even when no objects of that class exist.

• Cannot access non-static data or functions

• No this pointer for static functions• static data members and static member functions exist independent of

objects

• Can be accessed using class name and binary scope resolution operator(::)Employee::getCount()

• To access a public static class member when no objects of the class exist, prefix the class name and the binary scope resolution operator (::) to the name of the data member.

• To access a private or protected static class member when no objects of the class exist, provide a public static member function and call the function by prefix-ing its name with the class name and binary scope resolution operator.

Page 22: Classes: A Deeper Look,  Part 2

22

Page 23: Classes: A Deeper Look,  Part 2

23

Page 24: Classes: A Deeper Look,  Part 2

24

Page 25: Classes: A Deeper Look,  Part 2

25

25

Page 26: Classes: A Deeper Look,  Part 2

26

Page 27: Classes: A Deeper Look,  Part 2

27

10.6  static Class Members (cont.)• A member function should be declared static if it does not

access non-static data members or non-static member functions of the class.

• A static member function does not have a this pointer, because static data members and static member functions exist independently of any ob-jects of a class.

Page 28: Classes: A Deeper Look,  Part 2

28

Page 29: Classes: A Deeper Look,  Part 2

10.7  Data Abstraction and Information Hiding

• Classes normally hide the details of their implementation from their clients. This is called information hiding.

• The client cares about what functionality a stack offers, not about how that functionality is implemented. This concept is referred to as data abstraction.

29

Page 30: Classes: A Deeper Look,  Part 2

• This enables a particular class to be replaced with another version without affecting the rest of the system.

• As long as the public services of the class do not the rest of the system is not affected.

30