0% found this document useful (0 votes)
24 views26 pages

Friend Function

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)
24 views26 pages

Friend Function

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/ 26

Amity School of Engineering & Technology

Object Oriented Programming Using

[ES203]
Dr. Subhash Chand Gupta
1
Amity School of Engineering & Technology

Friend Function & Class

2
Amity School of Engineering & Technology

Friend functions
• A friend function is a function that can access the private
members of a class as though it were a member of that
class.

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


function prototype in the class definition with the keyword
friend.

• To make a function as a friend of a class, it is declared


prototype inside the class either in private or in public
section with keyword friend before its
Amity School of Engineering & Technology

Declaration of friend function in C++

• We can define the friend function as a normal


function to access the data of the class.

4
Amity School of Engineering & Technology

Declaration of friend function in C++

No friend keyword is used in the definition.


5
Amity School of Engineering & Technology

Example of Declaration of Friend Function:

6
Amity School of Engineering & Technology

There are some points to remember while implementing friend


functions in our program:
• A friend function can be declared in the private or public
section of the class.
• It can be called like 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 scope of the class.
• 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.

7
Amity School of Engineering & Technology

class sample{
int length, width;
public:
sample(int len, int bre){
length=len, width =bre; }
friend void calcArea(sample s); //friend function declaration
};
//friend function definition
void calcArea(sample s){
cout<<"Area = "<<s.length * s. width;
}
int main()
{ sample s1(10,15);
calcArea(s1);
return 0;
8
}
Amity School of Engineering & Technology

class Box {
double width;
public:
void setWidth( double wid );
friend void printWidth( Box box ); };

void Box::setWidth( double wid ) { width = wid; }


// Note: printWidth() is not a member function of any class.
void printWidth( Box box )
{
/* Because printWidth() is a friend of Box, it can directly access any
member of this class */
cout << "Width of box : " << box.width <<endl;
}

9
Amity School of Engineering & Technology

int main( )
{
Box box;
// set box width without member function
box.setWidth(10.0);
// Use friend function to print the wdith.
printWidth( box );
return 0; }
10
Other example
Amity School of Engineering & Technology
#include <iostream>
using namespace std;
class XYZ
{
private: int num=100; char ch='Z';
public: friend void disp(XYZ obj);
};
Void disp(XYZ obj)
{
cout<<obj.num<<endl; cout<<obj.ch<<endl;
}
int main()
{
XYZ obj; disp(obj); return 0;
} 11
Amity School of Engineering & Technology

Friend Class
• Friend class can access private and
protected members of the class to which it
is a friend.

12
Amity School of Engineering & Technology

class A{
……
friend class B;
};
class B
{ ……..
};
B is a friend of class A. So class B can access the
private and protected members of class A.
But this does not mean that class A can access private
and protected members of the class B. Note that the
friendship is not mutual unless we make it so.
13
Amity School of Engineering & Technology

#include <iostream> class ABC


using namespace std; {
class XYZ public: void disp(XYZ obj)
{ { cout<<obj.ch<<endl;
private: char ch='A'; cout<<obj.num<<endl;
int num = 11; }
public: };
Int a; int main()
friend class ABC; {
}; ABC obj; XYZ obj2;
obj.disp(obj2);
return 0; } 14
Friend Class Amity School of Engineering & Technology

using namespace std; class printClass{


class Area{ public:
int length , breadth , area; void printArea(Area a){
public: cout<<"Area = "<<a.area;
Area(int len, int bre) }
{ };
length=len;breadth=bre; int main(){
} Area a(10,15);
void calcArea(){ a.calcArea();
area = length * breadth; printClass p;
} p.printArea(a);
friend class printClass;
}; return 0;
}

15
Amity School of Engineering & Technology

Addition of private members of two different classes using friend Function

#include <iostream> class B {


using namespace std; private:
int numB;
class B;
public:
class A { B(): numB(1) { }
private: friend int add(A , B);
int numA; };
public: int add(A objectA1, B objectB1)
{
A(){ numA=12;}
return (objectA1.numA +
friend int add(A, B); objectB1.numB);
}; }

16
Amity School of Engineering & Technology

int main()
{
A objectA;
B objectB;
cout<<"Sum: "<< add(objectA, objectB);
return 0;
}

17
Properties of Friend Function
Amity School of Engineering & Technology

• 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, such a friend is known as GLOBAL
FRIEND.
• 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.
• Friends are non-members hence do not get “this” pointer.
• Friends, can be friend of more than one class, hence they can
be used for message passing between the classes.
• Friend can be declared anywhere (in public, protected or
private section) in the class.

18
Array of Objects
Amity School of Engineering & Technology

• Like array of other user-defined data types, an


array of type class can also be created.
• The array of type class contains the objects of
the class as its individual elements.
• Thus, an array of a class type is also known as
an array of objects.
• An array of objects is declared in the same
way as an array of any built-in data type.
Syntax:
class_name array_name [size] ;
Amity School of Engineering & Technology

Array of Objects
#include void main()
<iostream> {
class MyClass MyClass obs[4];
{ int i;
int x; for(i=0; i < 4; i++)
public: {obs[i].setX(i); } Output
void setX(int i) { x for(i=0; i < 4; i++) obs[0].getX(): 0
= i; } { obs[1].getX(): 1
int getX() { return cout << "obs[" << i << "].getX(): " << obs[2].getX(): 2
x; } obs[i].getX() << "\n"; obs[3].getX(): 3
}; }
getch(); }
Amity School of Engineering & Technology

#include<iostream.h>
#include<conio.h>
class Employee
{ int Id; char Name[25];
int Age; long Salary;
public: void GetData() //Statement 1 : Defining GetData()
{
cout<<"\n\tEnter Employee Id : ";
cin>>Id; cout<<"\n\tEnter Employee Name : ";
cin>>Name; cout<<"\n\tEnter Employee Age : ";
cin>>Age;
cout<<"\n\tEnter Employee Salary : ";
cin>>Salary;
}
void PutData() //Statement 2 : Defining PutData()
{
cout<<"\n"<<Id<<"\t"<<Name<<"\t"<<Age<<"\t"<<Salary;
} }; 21
Amity School of Engineering & Technology

void main() { int i;


Employee E[3];
//Statement 3 : Creating Array of 3 Employees
for(i=0;i<3;i++)
{ cout<<"\nEnter details of "<<i+1<<"
Employee";
E[i].GetData();
}
cout<<"\nDetails of Employees";
for(i=0;i<3;i++) E[i].PutData();
}

22
Amity School of Engineering & Technology

Constant Member functions and Objects


• const objects require const functions
– Member functions declared const cannot modify their object
– const must be specified in function prototype and definition
– Prototype:
ReturnType FunctionName(param1,param2…) const;
– Definition:
ReturnType FunctionName(param1,param2…) const { …}
– Example:
int A::getValue() const { return
private DataMember };
• Returns the value of a data member but doesn’t modify anything so is declared
const
• Constructors / Destructors cannot be const
– They need to initialize variables, therefore modifying them
Constant Member functions
Amity School of Engineering & Technology

 The const member functions are the functions which


are declared as constant in the program.
 The object called by these functions cannot be
modified.
 It is recommended to use const keyword so that
accidental changes to object are avoided.
 A const member function can be called by any type of
object. Non-const functions can be called by non-const
objects only. syntax
datatype function_name const ();
Amity School of Engineering & Technology

Constant Member functions

Output
The value using object d : 28
The value using object d1 : 8
Amity School of Engineering & Technology

Thank You

26

You might also like