0% found this document useful (0 votes)
18 views6 pages

W4L6

The document discusses friend functions in C++. A friend function can access private and protected members of a class but is defined outside the class. The document provides examples of declaring a single function and an entire class as friends and demonstrates how to define and use friend functions.

Uploaded by

avantisng2203
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)
18 views6 pages

W4L6

The document discusses friend functions in C++. A friend function can access private and protected members of a class but is defined outside the class. The document provides examples of declaring a single function and an entire class as friends and demonstrates how to define and use friend functions.

Uploaded by

avantisng2203
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
You are on page 1/ 6

Object-Oriented Programming Using C++

Friend Function

Indranil Saha

Department of Computer Science and Engineering


Indian Institute of Technology Kanpur

CS253: Software Development and Operations Object-Oriented Programming Using C++ 1/6
Friend Function

A 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

A friend can be a class, in which case the entire class and all of its
members are friends

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


prototype in the class definition with keyword friend

CS253: Software Development and Operations Object-Oriented Programming Using C++ 2/6
Friend Function

Friend frunction for the Box class


class Box {
double width;

public:
double length;
friend void printWidth( Box box );
void setWidth( double wid );
};

Friend an entire class


friend class ClassTwo;

CS253: Software Development and Operations Object-Oriented Programming Using C++ 3/6
Example: Friend Function
Friend function for the Box class
class Box {
double width;

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

// Member function definition


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;
}

// Main function for the program


int main() {
Box box;
// set box width without member function
box.setWidth(10.0);
// Use friend function to print the width
printWidth( box );
return 0;
}

CS253: Software Development and Operations Object-Oriented Programming Using C++ 4/6
Example: Friend Class

Friend class LinkedList of the Node class


class Node {
private:
int key;
Node* next;
/* Other members of Node Class */

// Now class LinkedList can access private members of Node


friend class LinkedList;
};

Friend function of the Node class from the LinkedList class


class Node {
private:
int key;
Node* next;
/* Other members of Node Class */

// Only search() of linkedList can access internal members


friend int LinkedList::search();
};

CS253: Software Development and Operations Object-Oriented Programming Using C++ 5/6
Object-Oriented Programming Using C++
Friend Function

Indranil Saha

Department of Computer Science and Engineering


Indian Institute of Technology Kanpur

CS253: Software Development and Operations Object-Oriented Programming Using C++ 6/6

You might also like