0% found this document useful (0 votes)
27 views94 pages

OOP Friend Function

The document provides an overview of Object-Oriented Programming (OOP) concepts, including classes, objects, encapsulation, inheritance, and polymorphism. It emphasizes the importance of data and methods associated with objects, and how they interact while protecting data integrity. The document also contrasts OOP with procedure-oriented programming and outlines basic programming structures in C++.

Uploaded by

safinahmmed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views94 pages

OOP Friend Function

The document provides an overview of Object-Oriented Programming (OOP) concepts, including classes, objects, encapsulation, inheritance, and polymorphism. It emphasizes the importance of data and methods associated with objects, and how they interact while protecting data integrity. The document also contrasts OOP with procedure-oriented programming and outlines basic programming structures in C++.

Uploaded by

safinahmmed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 94

OBJECT-

ORIENTED
PROGRAMMI
NG
REAL WORLD
PROBLEMS
Attributes
:
•Name
•Age
•Height
•…..
Functions
:
•Walking
•Driving
Sheldon
• ……

Aug 10, 2025 2


REAL WORLD
PROBLEMS
Attributes Attributes
: :
•Name •Name
•Age •Fuel
•Height •speed
•….. •…..
Functions Functions
: :
•Walking •setFule
•Driving •setSpeed
Sheldon Sedan
• …… •beDriven
• ……

Aug 10, 2025 3


REAL WORLD
PROBLEMS
Attributes Attributes
: :
•Name •Name
•Age •Fuel
•Height •speed
•….. •…..
Functions Functions
: :
•Walking •setFule
•Driving •setSpeed
Sheldon Sedan
• …… •beDriven
• ……

Sheldon is driving his


Aug 10, 2025 Sedan 4
REAL WORLD
PROBLEMS

A B

Sheldon is driving his Sedan


from A to B

Aug 10, 2025 5


REAL WORLD
PROBLEMS
Penny
Leonard

Amy
Sheldon

Class: person
Aug 10, 2025 6
REAL WORLD
PROBLEMS
Class:
person
Attribute
s:
•Name
•Age
•Height
•…..
Function
s:
•Walking Class: Blueprint of object
•Driving
Aug 10, 2025
• …… 7
REAL WORLD
PROBLEMS

Object: Sheldon

Class: person

Aug 10, 2025


Object: Amy 8
OBJECT-ORIENTED
PROGRAMMING
Object 1 Object 2

communication
Data Data
Function Function

Object 3

Data
Function

Allows decomposition of a problem into a number of entities called object.


Builds data and functions around these objects.
Data of an object can be accessed only by the function associated with the object
and protects it from accidental modification from outside functions.
Function of one object can communication with the function of other object.
Aug 10, 2025 9
C++, Java etc.
CHARACTERISTICS OF
OBJECT-ORIENTED
PROGRAMMING
Emphasis is on data rather than
procedure.
Programs are divided into objects.
Data is hidden and can not be
accessed by external functions.
Object may communicate with each
other through functions.
Follows bottom-up approach.
Aug 10, 2025 10
BOTTOM-UP APPROACH
Lot of small modules will be grouped to
form a single large module.
Specific to general.

Aug 10, 2025 11


HOW PROCEDURE-ORIENTED
PROGRAMMING LOOKS AT A
PROBLEM
Procedures/functions
Procedures act on data
Program

Procedure Data

Procedure

Aug 10, 2025 12


HOW OBJECT-ORIENTED
PROGRAMMING LOOKS AT A
PROBLEM?
Objects M
et

d
Data ho

ho
Methods d

et
M
(functions)
Data
Methods surround

M
d
the data and

e
o

t
protect data from
th

ho
other objects
e

d
M
Aug 10, 2025 OBJECT 13
BASIC CONCEPTS OF
OBJECT-ORIENTED
PROGRAMMING

Aug 10, 2025 14


OBJECTS

Can be concrete and tangible entities.


Sheldon, amy, book, car, laptop etc..
Can be abstract entities and do not have
to be tangible.
Database, email, webpage, song etc..
An object can contain other objects
House = kitchen + bedrooms + …
Laptop = keyboard + display +
processor + …
Aug 10, 2025 15
OBJECTS’ THREE
PROPERTIES
Unique identity – name, serial number,
relationship with another object ..
Set of attributes – location, speed, size,
name, phone number …
Behaviors (action)– walking, driving, take
picture, send email …

Behavior

Attributes
Aug 10, 2025 16
CLASSES
Describe the commonalities of similar
objects.
 Person : sheldon, leonard, amy, panny ..
 Car : sedan, ford, toyota ...
 Classroom : CSE 101, CSE 102 …
 Building: CSE, EEE, CE, ME …
Blueprint of object.
Describe both the attributes and the
behaviors
 Person: name, age .. + sleep, walk ..
User-defined
Aug 10, 2025 data types and behave like the 17
ENCAPSULATION
The wrapping up of data and functions into a single
unit is known as encapsulation.
Data is not accessible to the outside world.
Only those functions which are wrapped in the class
can access it.
This insulation of the data from direct access by the
program is known as ‘data hiding’ or ‘information
hiding’.

Aug 10, 2025 18


ABSTRACTION
Representing essential features without
including the background details or
explanations.

Aug 10, 2025 19


INHERITANCE
A process by which objects of one class acquire the
properties of objects of another class.
Provides the idea of reusability.
Inherited class is called parent/base/super class.
Class that inherits parent class is known as
child/derived/sub class.
vehicle

wheeled vehicle boat

Aug 10, 2025 20


POLYMORPHISM
Poly - many and morph – form(behavior).
An operation may exhibit different behavior in
different instances.

Aug 10, 2025 21


SPECIFYING A CLASS

Class members:
 Functions – member functions.
 Variables – data members.
Visibility labels:
 Private – can be accessed only from within the
class.
 Public – can be accessed from outside of the class
also.
Aug 10, 2025 22
SPECIFYING A CLASS
Only the member functions can have access
to the private data members and private
functions.

Aug 10, 2025 23


SIMPLE CLASS EXAMPLE
AND ITS REPRESENTATION

Aug 10, 2025 Representation of a class 24


CREATING OBJECTS
Once a class has been declared, we can create
variables of that type by using the class name.
item X ; // memory for X is created
It creates a variable of of type item. In c++ the class variables are known as
objects. Therefore X is called an object of type item.
Objects can also be created when a class is defined by placing their names
immediately after the closing brace as we do in the case of structures. For
e.g.-
class item
{ --------
------------ } x,y,z;
But usually we declare the objects close to the place where they are used &
not at the time of class definition.
Aug 10, 2025 25
ACCESSING CLASS
MEMBERS

item x;

lega
l
illegal

illegal

Aug 10, 2025 26


DEFINING MEMBER
FUNCTIONS
Outside the class definition.
Inside the class definition.

Aug 10, 2025 27


OUTSIDE THE CLASS
DEFINITION

Aug 10, 2025 28


OUTSIDE THE CLASS
DEFINITION Membership
label

Aug 10, 2025 29


CHARACTERISTICS OF
MEMBER FUNCTION
Several different classes can use the
same function name. The ‘membership
label’ will resolve their scope.
Member function can access the private
data of the class. A non-member function
(except friend function) can not do so.
A member function can call another
member function directly without using
dot operator.

Aug 10, 2025 30


INSIDE THE CLASS
DEFINITION

When a function is defined inside of a class, it is


treated as an inline function.

Aug 10, 2025 31


A SIMPLE PROGRAM

Input: name and age of a person.


Output: display name and age of that
person.

Aug 10, 2025 32


A SIMPLE PROGRAM

keyword

Attributes
By default “private”

Functions/methods

Aug 10, 2025 33


A SIMPLE PROGRAM
Header file

Scope resolution operator

Insertion or put to operator/ bit-wise left-shif


Extraction or get from operator

Aug 10, 2025 34


A SIMPLE PROGRAM

Aug 10, 2025 35


A SIMPLE PROGRAM

dot operator

Aug 10, 2025 36


C++ PROGRAM WITH
CLASS

Aug 10, 2025 37


C++ PROGRAM WITH
CLASS

Aug 10, 2025 38


C++ PROGRAM WITH
CLASS

Aug 10, 2025 39


C++ PROGRAM WITH
CLASS

Aug 10, 2025 40


NESTING OF MEMBER
FUNCTIONS

Aug 10, 2025 41


NESTING OF MEMBER
FUNCTIONS

Aug 10, 2025 42


NESTING OF MEMBER
FUNCTIONS

Aug 10, 2025 43


NESTING OF MEMBER
FUNCTIONS

Aug 10, 2025 44


NESTING OF MEMBER
FUNCTIONS

Aug 10, 2025 45


NESTING OF MEMBER
FUNCTIONS

Aug 10, 2025 46


NESTING OF MEMBER
FUNCTIONS

Aug 10, 2025 47


NESTING OF MEMBER
FUNCTIONS

Aug 10, 2025 48


ARRAYS OF OBJECTS

Aug 10, 2025 49


ARRAYS OF OBJECTS

Aug 10, 2025 50


ARRAYS OF OBJECTS

Aug 10, 2025 51


OBJECTS AS FUNCTION
ARGUMENTS

Aug 10, 2025 52


OBJECTS AS FUNCTION
ARGUMENTS

Aug 10, 2025 53


OBJECTS AS FUNCTION
ARGUMENTS

Aug 10, 2025 54


OBJECTS AS FUNCTION
ARGUMENTS

Aug 10, 2025 55


OBJECTS AS FUNCTION
ARGUMENTS

Aug 10, 2025 56


OBJECTS AS FUNCTION
ARGUMENTS

Aug 10, 2025 57


OBJECTS AS FUNCTION
ARGUMENTS

Aug 10, 2025 58


OBJECTS AS FUNCTION
ARGUMENTS

Aug 10, 2025 59


OBJECTS AS FUNCTION
ARGUMENTS

Aug 10, 2025 60


FRIENDLY FUNCTIONS

Aug 10, 2025 61


FRIENDLY FUNCTIONS

Aug 10, 2025 62


FRIENDLY FUNCTIONS

Aug 10, 2025 63


FRIENDLY FUNCTIONS

Aug 10, 2025 64


FRIENDLY FUNCTIONS

Aug 10, 2025 65


FRIENDLY FUNCTIONS

Aug 10, 2025 66


A FUNCTION FRIENDLY
TO TWO CLASSES
Forward declaration

Aug 10, 2025 67


A FUNCTION FRIENDLY
TO TWO CLASSES

Aug 10, 2025 68


A FUNCTION FRIENDLY
TO TWO CLASSES

Aug 10, 2025 69


A FUNCTION FRIENDLY
TO TWO CLASSES

Aug 10, 2025 70


A FUNCTION FRIENDLY
TO TWO CLASSES

Aug 10, 2025 71


A FUNCTION FRIENDLY
TO TWO CLASSES

Aug 10, 2025 72


A FUNCTION FRIENDLY
TO TWO CLASSES

Aug 10, 2025 73


A FUNCTION FRIENDLY
TO TWO CLASSES

Aug 10, 2025 74


A FUNCTION FRIENDLY
TO TWO CLASSES

Aug 10, 2025 75


A FUNCTION FRIENDLY
TO TWO CLASSES

Aug 10, 2025 76


RETURNING OBJECTS

Aug 10, 2025 77


RETURNING OBJECTS

Aug 10, 2025 78


RETURNING OBJECTS

Aug 10, 2025 79


RETURNING OBJECTS

Aug 10, 2025 80


FRIENDLY FUNCTIONS

Aug 10, 2025 81


FRIENDLY FUNCTIONS

Aug 10, 2025 82


FRIEND CLASS

Aug 10, 2025 83


FRIEND CLASS

Aug 10, 2025 84


FRIEND CLASS

Aug 10, 2025 85


FRIEND CLASS

Aug 10, 2025 86


FRIEND CLASS

Aug 10, 2025 87


FRIEND CLASS

Aug 10, 2025 88


FRIEND CLASS

Aug 10, 2025 89


FRIEND CLASS

Aug 10, 2025 90


FRIEND CLASS

Aug 10, 2025 91


FRIEND CLASS

Aug 10, 2025 92


FRIEND CLASS

Aug 10, 2025 93


REFERENCES

1. “Object oriented programming with


C++” – E balagurusamy, second
edition.
2. http://www.cplusplus.com/doc/
tutorial/inheritance/
3. Web.

Aug 10, 2025 94

You might also like