Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS212: Object Oriented Analysis and Design Lecture 5: Classes and Objects - II.

Similar presentations


Presentation on theme: "CS212: Object Oriented Analysis and Design Lecture 5: Classes and Objects - II."— Presentation transcript:

1 CS212: Object Oriented Analysis and Design Lecture 5: Classes and Objects - II

2 Books and References C++ complete reference: Herbert Schildt Object Oriented Programming with C++: E balagurusamy C++ How to Program, Deitel and Dietel

3 Recap of Lecture 3 Class identification and CRC chart Defining classes in C++ Execution of blue print Access specifiers – Public, private, protected Getter and setter function Separating interface from implementation

4 Today’s objective Scope of class members Nesting member function Class members and arrays Static class members Friendly classes and function

5 Data hiding in classes CLASS Data Function Data Function Private Area Public Area Entry allowed to public area

6 Scope of class members Scope is an enclosing context where values and expressions are associated Scope resolution operator helps to identify and specify the context to which an identifier refers Scope resolution operator is written as " :: ". It is used to qualify hidden names so that you can still use them It is a unary scope operator

7 Scope resolution Global variables and local variables Class members Member variable Member functions

8 Nesting of member functions Member functions can be called by another member function of the same class The private data member remains safely encapsulated Get and set function helps the client to interact with the object Demonstration

9 Private member function Usually member data are made private while functions (or methods) are made public. You may not want the user to directly access these functions Private functions can only be called from within public member functions. These functions are also called ‘helper functions’ Demonstration

10 Memory Allocation for Objects Common for all objects Member function 1 Member function 2 Object 1Object 2Object 3 Member variable 1 Member variable 2 Memory created when functions defined Memory created when objects defined

11 Class members as arrays Arrays can be used as member variables of a class Individual array is created in the memory for each objects Caution: Segmentation fault Dynamic memory allocation Copying data Demonstration

12 Static Member Variable A data member of a class can be static A static member has following characteristics It is initialized when first object is created Only one copy of that member is created The member is shared by all the objects of the class Visible only within the class Lifetime is entire program A global definition must be provided

13 Static member function Member functions can also be static Restrictions: Only static member variables are accessible (apart from global ones) Does not have a this pointer Static and non-static of same function is not allowed May not be virtual, constant Demonstration

14 Friend function A friend function of a class is defined outside that class' scope but it has the right to access all private and protected members of the class. Even though the prototypes for friend functions appear in the class definition, friends are not member functions. A friend can be a function, function template, or member function, or a class or class template

15 Properties of friend functions Friend of the class can be member of some other class. Friend of one class can be friend of another class or all the classes in one program: GLOBAL FRIEND. Can access the private or protected members of the class in which they are declared to be friend, but they can use the members for a specific object Do not get “this” pointer. Can be friend of more than one class, hence they can be used for message passing between the classes. Can be declared anywhere (in public, protected or private section) in the class.

16 When to use friend function Three different circumstances where friend functions are useful Operator overloading - for certain types of operators Creation of I/O operations Multiple classes share common functionality

17 Friend Class Friendship may allow a class to be better encapsulated by granting per-class access to parts of its API that would otherwise have to be public. This increased encapsulation comes at the cost of tighter coupling between classes Friendships are not symmetric Friendships are not transitive Friendships are not inherited Access due to friendship is inherited

18 Logistic Group No. Roll NumbersDay of WeekTime 1 B14CS001 – 043, B14SS002 – 017, B14BS011 – 015 THURSDAY1 P.M. 2 UG20131001 – 043, UG2013003 – 031, UG20134006 – 08, FRIDAY1 P.M.

19 Thank you Next Lecture: Constructor and destructors


Download ppt "CS212: Object Oriented Analysis and Design Lecture 5: Classes and Objects - II."

Similar presentations


Ads by Google