0% found this document useful (0 votes)
26 views19 pages

Chapter 5 Friend Function

Chapter 5 of the document discusses friend functions and operator overloading in C++. Friend functions can access private and protected members of a class, while operator overloading allows operators to work with user-defined data types. The chapter includes examples to illustrate these concepts.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views19 pages

Chapter 5 Friend Function

Chapter 5 of the document discusses friend functions and operator overloading in C++. Friend functions can access private and protected members of a class, while operator overloading allows operators to work with user-defined data types. The chapter includes examples to illustrate these concepts.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

HEWAD UNIVERSITY

Object Oriented Programming ‫هیــــواد پوهنــتون‬

OOP in cpp
Chapter 5
Friend Function

Ahmad Jalal Zalal

Department
HEWAD University of Computing & Technology ‫کمپیوتر ساینس پوهنځی‬ ‫هیــــواد پوهنــتون‬
HEWAD University
Department
of Computing & Technology ‫کمپیوتر ساینس پوهنځی‬ ‫هیــــواد پوهنــتون‬

In the end of this Chapter

• Friend function.

• Operator overloading.

• Program examples.
HEWAD University
Department
of Computing & Technology ‫کمپیوتر ساینس پوهنځی‬ ‫هیــــواد پوهنــتون‬

Friend Function

• 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.
HEWAD University
Department
of Computing & Technology ‫کمپیوتر ساینس پوهنځی‬ ‫هیــــواد پوهنــتون‬

Friend Function

• A friend can be a function, function template of member


function, a class or class template, in which case the entire
class and all of its members are friends.

• To declare a function as a friend of class, precede the function


prototype in the class definition with keyword friend.
HEWAD University
Department
of Computing & Technology ‫کمپیوتر ساینس پوهنځی‬ ‫هیــــواد پوهنــتون‬

Friend Function
class Sample int main()
{ Declaration of friend function {
private: inside class Sample. Sample obj1;
int num; show(obj1);
public: return 0;
friend void show(Sample obj1); }
};

void show(Sample obj1) • This is the friend function of class Sample which
{ has access to all member data + functions of class
[Link]=10; Sample. num in this function is unknown until we
declared it inside the class Sample.
cout << "The value of num: "
<< [Link] << endl; • Sample is the class_name inside parenthesis of
} friend function and obj1 is the object.
HEWAD University
Department
of Computing & Technology ‫کمپیوتر ساینس پوهنځی‬ ‫هیــــواد پوهنــتون‬

Friend Function
class B int main()
class A { {
{ public: A obj1;
private: void input(A obj1) B obj2;
int a; { [Link](obj1);
protected: cout << "Enter 1st num: ";
int b; cin >> obj1.a; return 0;
public: cout << "Enter 2nd num: "; }
friend class B; cin >> obj1.b;
}; sum(obj1); Sum function is
} called inside
void sum(A obj1) input function.
Class B is the friend of class • 2 objects of both classes are
{
A. Means class B can access made.
all data members of class A.
int res; • Input function of obj2 is
res=obj1.a+obj1.b; called.
cout << "Sum: " << res << endl; • The data is passed in obj1 of
} class A (Data members).
};
HEWAD University
Department
of Computing & Technology ‫کمپیوتر ساینس پوهنځی‬ ‫هیــــواد پوهنــتون‬

Friend Function

• Imagine that you want a function to operate on objects of two


different classes.

• Perhaps the function will take objects of the two classes as


arguments, and operate on their private data.

• In this situation there’s nothing like a friend function.


HEWAD University
Department
of Computing & Technology ‫کمپیوتر ساینس پوهنځی‬ ‫هیــــواد پوهنــتون‬

Friend function which can access both A and


B classes and can access both objects Obj1
Friend Function and Obj2 of main function.

#include <iostream> class B void Add(A obj1, B obj2)


using namespace std; { {
private: cout << "Result: " << obj1.num1 + obj2.num2;
class B; int num2; }
class A public:
{ void get_value_B(); int main()
private: friend void Add(A,B); {
int num1; }; A obj1;
public: B obj2;
void get_value_A(); void B::get_value_B()
friend void Add(A,B); { obj1.get_value_A();
}; cout << "Enter 2nd number: "; obj2.get_value_B();
void A::get_value_A() cin >> num2;
{ } Add (obj1, obj2);
cout << "Enter 1st number: ";
cin >> num1; return 0;
} }
HEWAD University
Department
of Computing & Technology ‫کمپیوتر ساینس پوهنځی‬ ‫هیــــواد پوهنــتون‬

Operator Overloading

• In C++ the meaning of operator is already defined and fixed for


basic data types like int, float, double etc.

• If we want to add two integers then + operator is used. i.e.


• Int a=2, b=4;
• Cout << a+b;
• The compiler adds a and b and return the result.
• But what if I add 2 objects like (obj1+obj2) it will give error. So how can
we define it? Through operator overloading.
HEWAD University
Department
of Computing & Technology ‫کمپیوتر ساینس پوهنځی‬ ‫هیــــواد پوهنــتون‬

Operator Overloading

• Now consider we have 1 calss and 2 objects and want to add


values of both objects:
• Test obj1, obj2;
• Obj1=“hello”;
• Obj2=“world”;
• Cout << obj1+obj2;

• This will give error while compilation time. So here we use


operator overloading.
HEWAD University
Department
of Computing & Technology ‫کمپیوتر ساینس پوهنځی‬ ‫هیــــواد پوهنــتون‬

Operator Overloading

• Operator overloading enables an operator to perform different


operations depending on the type of operands.

• It also enables the operators to process the user-defined data


types.
HEWAD University
Department
of Computing & Technology ‫کمپیوتر ساینس پوهنځی‬ ‫هیــــواد پوهنــتون‬

Overload-able Operators
HEWAD University
Department
of Computing & Technology ‫کمپیوتر ساینس پوهنځی‬ ‫هیــــواد پوهنــتون‬

Non-overload-able Operators
HEWAD University
Department
of Computing & Technology ‫کمپیوتر ساینس پوهنځی‬ ‫هیــــواد پوهنــتون‬

Syntax of O.O

Return-type operator-sign(parameter-list)
{
Body of function
}
HEWAD University
Department
of Computing & Technology ‫کمپیوتر ساینس پوهنځی‬ ‫هیــــواد پوهنــتون‬

Example

void operator ++()


{
Body of function
}
HEWAD University
Department
of Computing & Technology ‫کمپیوتر ساینس پوهنځی‬ ‫هیــــواد پوهنــتون‬

Program Example
class Test
{ int main()
Getter Function (Gets value
public: from user).
{
void get_value() Test obj1, obj2;
{ [Link]. Takes
cout << "Enter value: "; the values from 2 obj cout << "Enter data of obj1: " << endl;
cin >> n; and add them. obj1.get_value();
} If this function is cout << "Enter data of obj2: " << endl;
void operator + (Test obj) removed obj1+obj2 in obj2.get_value();
{ main will no longer
Test t; work. obj1+obj2;
t.n=n+obj.n;
cout << "The result is: " << t.n << endl; return 0;
} }
private:
The plus sign here specifies if it Adds both values given in
int n; O.O.F.
}; must be added or subtracted.
HEWAD University
Department
of Computing & Technology ‫کمپیوتر ساینس پوهنځی‬ ‫هیــــواد پوهنــتون‬

Program Example

void operator + (Test obj) • Object of Test is declared in parenthesis to


overload.
{
Test t; • Object t of class Test is declared.

• Takes value of n from object t.


t.n=n+obj.n;
• Adds the n with n value of obj.

cout << "The result is: “ • Gives output.


<< t.n << endl;
}
HEWAD University
Department
of Computing & Technology ‫کمپیوتر ساینس پوهنځی‬ ‫هیــــواد پوهنــتون‬

In this Chapter

• Friend function.

• Operator overloading.

• Program examples.
HEWAD University
Department
of Computing & Technology ‫کمپیوتر ساینس پوهنځی‬ ‫هیــــواد پوهنــتون‬

End of Chapter 5

Any Questions???

You might also like