Program To Illustrate How To Declare The Entire Class As A Friend Of Another Class Using C++

LEVEL: Beginner

Whats in this post?

  1. Code
  2. Output
  3. Background and Explanation of the code

Code


To copy code: Click View Source “View Source” on the right of this code snippet.


Background and Explanation of the code


The statement, friend class yyy; within the definition of  class xxx informs the compiler that entire class yyy is a friend of class xxx.This means all the member function of a class yyy are the friend function of class xxx.Declaring a class yyy as a friend of a class xxx doesnot mean that class xxx is also a friend of class yyy so member functions of class xxx cant access private members of class yyy.

OUTPUT

x  =  10

y  =  15