Table of Contents
Back Button
Back to Chapter
C++ Setting Up
C++ Introduction
No items found.
C++ Basics
No items found.
C++ Control Statements
No items found.
C++ Logical Operators
No items found.
C++ Procedural Programming
No items found.
C++ Structural Programming
No items found.
C++ Implementation of OOPS
No items found.
C++ Arrays and Vectors
No items found.
C++ Error Handling
No items found.
C++ File Handling
No items found.

C++ friend class and functions

Friend Class

A friend class can access private and protected members of other classes in which it is declared as friend. It is sometimes useful to allow a particular class to access private members of other classes.

Consider a scenario in which you forget your hall ticket to your examination in such a situation having a friend who has access to your private drawer can be really helpful similarly a friend class is very useful.

Syntax for friend Class

class ClassNmae { private: int key; Node* next; /* Other members of Node Class */ // Now class LinkedList can // access private members of Node friend class FriendClassName; };

Like friend class, a friend function can be given a special grant to access private and protected members.

A friend function can either be a global function or a member function of a different class.

Syntax for friend function

// When Friend Function is a member of a class class className1 { private: int key; Node* next; /* Other members of Node Class */ friend int className2::FreindFuncName(); // Only FreindFuncName() of className2 can access the private members // can access internal members };
//When the friend function is a global function class A { int a; public: A() { a = 0; } // global friend function friend void showA(A&);// friend function showA() is global. };

Points to remember

  • The use of friends should be limited. If we declare too many friends of a class, it lessens the value of encapsulation of separate classes in object-oriented programming.
  • Friendship is not mutual. If class A is a friend of B, then B doesn’t become a friend of A.
  • Friendship is not inherited.
  • The concept of friends is not there in Java.

Friendship and Inheritance

In C++, when we derive a new class from a base class, friendship is not inherited. If a base class has a friend function, then the function doesn’t become a friend of the derived class.

Demonstration of how a friend of base class is not friend of a derived class

#include using namespace std; class X { protected: int a; public: X() { a = 0;} friend void show(); }; class Y: public X { public: Y() : b (0) {} private: int c; }; void show() { Y b; cout << "The default value of A::x = " << b.a; // Can't access private member "c" declared in class 'B' cout << "The default value of B::y = " << b.c; } int main() { show(); getchar(); return 0; }

We will get an error message because show() which is a friend of base class X tries to access private data of derived class Y.

A simple C++ program to demonstrate friend function of another class

#include class Y; class X{ public: void showB(B&); }; class Y { private: int b; public: Y() { y= 0; } friend void A::showB(B& x); // Friend function }; void X::showY(Y& x) { // Since showB() is friend of B, it can // access private members of B std::cout << "Y::y = " << x.y; } int main() { X a; Y x; a.showY(x); return 0; }
Y::y = 0
Ask queries
Contact Us on Whatsapp
Hi, How Can We Help You?