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

OOP in C++ (Friend Function)

B

Uploaded by

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

OOP in C++ (Friend Function)

B

Uploaded by

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

OOP in C++

(Friend Function and Friend Class)

Prof. Dr. Md. Abdulla Al Mamun


CSE, HSTU
1
Friend Function
 Concept: class MyClass {
private:
•Data hiding is a fundamental int member1;
concept of object-oriented }
programming. int main() {
MyClass obj;
•It restricts the access of
private and protected // Error! Cannot access private
members from outside of the members from here.
obj.member1 = 5;
class. }
2
Friend Function
 Concept:
•There is a feature in C++ called friend functions that break
this rule and allow us to access member functions from
outside the class.
•If a function is defined as a friend function in C++, then the
protected and private data of a class can be accessed using
the function.

3
Friend Function
 Concept:
•A class’s friend function is defined outside that class’s scope,
but it has the right to access all private and protected
members of the class.
•For accessing the data, the declaration of a friend function
should be done inside the body of a class.
•Even though the prototypes for friend functions appear in
the class definition, friends are not member functions.
4
Friend Function
 Concept:
•A friend function in C++ is a function that is preceded by the
keyword “friend.”
•By using the keyword friend compiler knows the given
function is a friend function.

5
Friend Function
 Syntax:
class className {
... .. ...
friend returnType functionName(arguments);
... .. ...
}
• The function can be defined anywhere in the program like a
normal C++ function. The function definition does not use
either the keyword friend or scope resolution operator.
6
Friend Function
 Example: Working of friend Function
class Distance { // friend function definition
private: int addFive(Distance d) {
int meter;
//accessing private members from the friend function
// friend function d.meter += 5;
friend int addFive(Distance); return d.meter;
}
public:
Distance() : meter(0) {} int main() {
Distance D;
}; cout << "Distance: " << addFive(D);
return 0;
Output }
Distance: 5
7
Friend Function
 Characteristics of friend function:
•A friend function can be declared in the private or public
section of the class.
•It can be called a normal function without using the object.
•A friend function is not in the scope of the class, of which it
is a friend.
•A friend function is not invoked using the class object as it is
not in the class’s scope. 8
Friend Function
 Characteristics of friend function:
•A friend function cannot access the private and protected
data members of the class directly. It needs to make use of a
class object and then access the members using the dot
operator.
•A friend function can be a global function or a member of
another class.

9
Friend Function
 Example: Add Members of Two Different Classes
// forward declaration
class ClassB;

class ClassA {

public:
// constructor to initialize numA to 12
ClassA() : numA(12) {}

private:
int numA;

// friend function declaration


friend int add(ClassA, ClassB);
};
10
Friend Function
 Example: Add Members of Two Different Classes
class ClassB {
public: int main() {
// constructor to initialize numB to 1 ClassA objectA;
ClassB() : numB(1) {} ClassB objectB;
cout << "Sum: " << add(objectA, objectB);
private: return 0;
int numB; }

// friend function declaration


friend int add(ClassA, ClassB);
};
Output
// access members of both classes Sum: 13
int add(ClassA objectA, ClassB objectB) {
return (objectA.numA + objectB.numB);
} 11
Friend Function
 Operator Overloading using friend function: The
Example to overload the binary operator +
class Complex { void display() {
private: cout << real << " + " << imag << "i" << endl;
float real, imag; }
};
public:
Complex(float r = 0, float i = 0) : real(r), imag(i) {} int main() {
Complex c1(2.5, 3.5), c2(1.5, 2.0);
// Friend function to overload the + operator Complex c3 = c1 + c2; // Using the overloaded + operator
friend Complex operator+(const Complex& lhs, const c3.display(); // Output: 4 + 5.5i
Complex& rhs) {
return Complex(lhs.real + rhs.real, lhs.imag + rhs.imag); return 0;
} }
Output
4 + 5.5i 12
Friend Class
 Concept:
•We can also use a friend Class in C++ using the friend
keyword.
•When a class is declared a friend class, all the member
functions of the friend class become friend functions.
•So, a friend class can access both private and protected
members of the class in which it has been declared as
friend.
13
Friend Class
 Concept:
class ClassB;
•Since ClassB is a friend class,
we can access all members of class ClassA {
// ClassB is a friend class of ClassA
ClassA from inside ClassB. friend class ClassB;
... .. ...
•However, we cannot access }
members of ClassB from
inside ClassA. It is because class ClassB {
... .. ...
friend relation in C++ is only }
granted, not taken. 14
class ClassB {
Friend Class private:
int numB;

 An Example: public:
// constructor to initialize numB to 1
ClassB() : numB(1) {}

class ClassB; // forward declaration // member function to add numA from ClassA and numB from ClassB
int add() {
class ClassA { ClassA objectA;
private: return objectA.numA + numB;
int numA; }
};
friend class ClassB; // friend class declaration
int main() {
public: ClassB objectB;
// constructor to initialize numA to 12 cout << "Sum: " << objectB.add();
ClassA() : numA(12) {} return 0;
}; }
Output
Sum: 13 15

You might also like