0% found this document useful (0 votes)
3 views

Friend Classes and Functions

Uploaded by

shariqqayyum612
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Friend Classes and Functions

Uploaded by

shariqqayyum612
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 13

Friend Function and

classes
Introduction to Friendship in
OOP
 What is Friendship?
 A concept that allows a function or class to access private and protected
members of another class.
 Used to enable special relationships between classes or functions.
 Why Use Friendship?
 When external functions or classes require tightly coupled interactions with
the internals of another class.
Friend Functions - Definition

 Definition: A friend function is a non-member function that has access


to the private and protected members of a class.
 Key Points:
 Declared using the keyword friend inside the class.
 Defined outside the class but has special access rights.
 Not a member of the class it is "friends" with.
class ClassName {
private:
int privateMember;
public:
// Friend Function Declaration
friend void friendFunction(ClassName obj);
};

// Friend Function Definition


void friendFunction(ClassName obj) {
cout << "Private Member: " << obj.privateMember << endl;
}
Important Points
Friend Keyword:
 Use friend to declare a friend function inside a class.
Access Private/Protected Members:
 Friend functions can directly access private and protected members of
the class.
Not a Class Member:
 A friend function is not a member of the class and cannot be called using
an object (e.g., obj.functionName()).
Declaration Inside Class:
 The friend function must be declared within the class whose members it
accesses.
Definition Outside Class:
 Define the friend function like a regular global function outside the class.
Important Points

Friendship is Limited:
 Friendship is not mutual (not bidirectional) and not inherited by derived classes.
Encapsulation Violation:
 Use sparingly to avoid breaking the encapsulation principle; prefer public methods
like getters/setters.
Explicit Friendship:
 Friendship must be explicitly granted by the class, not assumed by the function.
Global Nature:
 Friend functions are global, behaving like regular functions but with special access.
Common Use Cases:
 Operator overloading.
 Utility functions requiring access to private data (e.g., comparing or combining
objects).
Friend Class

 Definition: A class declared as a friend of another class can access


private and protected members of that class.
 Key Points:
 Allows two classes to have close collaboration.
 Declared using friend class in the class whose members will be accessed.
class ClassA {
private:
int privateMember;

// Declare ClassB as a friend


friend class ClassB;

public:
ClassA() : privateMember(100) {}
};

class ClassB {
public:
void accessClassA(ClassA obj) {
cout << "Accessing private member of ClassA: " << obj.privateMember << :endl;
}
};
Scenario 1

 Two classes, ClassA and ClassB, need to access each other's private
data. ClassB is declared as a friend of ClassA, so it can access its
private members.
Scenario 2

 You are designing a time management system where you need to add
two time durations. Each time object stores hours, minutes, and
seconds as private data members. However, you don’t want to
expose these details directly through public getters. A friend
function is used to add two time objects by directly accessing their
private data members.
Scenario 3

 You are designing a time management system where you need to add
two time durations. Each time object stores hours, minutes, and
seconds as private data members. However, you don’t want to
expose these details directly through public getters. A friend
function is used to add two time objects by directly accessing their
private data members.
Scenario 4

 You are building a school management system where a Course class


needs to display details of students enrolled in a particular course.
Since student data such as name and grade is stored privately within
the Student class, the Course class is declared as a friend class to
access that private data directly.
Feature Friend Class Friend Function
A friend class has A friend function has
access to all private access to the private
Access Level and protected and protected
members of another members of a specific
class. class.
The entire class has
A single function (non-
access to the private
Scope member) is granted
members of the other
access.
class.
friend void
friend class ClassB;
functionName(ClassA
Declaration inside the class being
a); inside the class
accessed.
being accessed.
Useful when two Useful when a single
classes need to non-member function
Usage closely interact with needs to access
each other's private private data of a
data. class.

You might also like