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

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

Embed Size (px)

Citation preview

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

CS212: Object Oriented Analysis and Design

Lecture 5: Classes and Objects - II

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

Books and References

• C++ complete reference: Herbert Schildt

• Object Oriented Programming with C++: E balagurusamy

• C++ How to Program, Deitel and Dietel

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

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

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

Today’s objective

• Scope of class members

• Nesting member function

• Class members and arrays

• Static class members

• Friendly classes and function

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

Data hiding in classes

CLASS

Data

Function

Data

Function

Private Area

Public Area

Entry allowed to public area

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

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

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

Scope resolution

Global variables and local variables

Class members

Member variable

Member functions

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

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

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

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

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

Memory Allocation for ObjectsCommon for all objects

Member function 1

Member function 2

Object 1 Object 2 Object 3

Member variable 1 Member variable 1 Member variable 1

Member variable 2 Member variable 2 Member variable 2

Memory created when functions defined

Memory created when objects defined

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

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

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

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

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

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

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

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

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

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.

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

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

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

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

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

Logistic

Group No. Roll Numbers Day of Week Time

1B14CS001 – 043, B14SS002 – 017, B14BS011 – 015

THURSDAY 1 P.M.

2UG20131001 – 043, UG2013003 – 031, UG20134006 – 08,

FRIDAY 1 P.M.

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

Thank youNext Lecture: Constructor and destructors