1 / 30

Classes: A Deeper Look, Part 2

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

dee
Download Presentation

Classes: A Deeper Look, Part 2

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Classes: A Deeper Look, Part 2 Chapter 10 Part II

  2. Outline

  3. 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 • 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.

  4. friend function declaration (can appear anywhere in the class)

  5. friend function can modify Count’s private data Calling a friend function; note that we pass the Count object to the function

  6. 10.4 Friend Classes • Friend functions may be members of other classes • To declare such friend functions, use scope resolution friendvoidotherClass::func(inti); • We can make all member functions of class ClassTwo friends of class ClassOne • In ClassOne definition, place declaration of form friend classClassTwo;

  7. 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

  8. 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 • object • string 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

  9. 10.6 staticClass 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

  10. 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. • publicstatic 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

  11. 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.

  12. 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.

  13. 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.

  14. 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.

More Related