A class can have data members that are private. Normally these are not accessible outside the class. However we can declare that a function, class or a class method is a friend and then that function or class methods can access our private members.
#include <iostream>using namespace std ;class point { private: int x1 ; int y1 ; //Does not matter under what access we place the declaration under friend void printPoint ( point p1 ) ; friend class B ;};void printPoint ( point p1 ){ cout << p1.x1 << " " << p1.y1 << endl ;}class B { void anotherPrintFunction ( point p1 ) { cout << p1.x1 << endl ; }};int main(){ return 0 ;}
Exercise:
Correct the compiler error in the below code by making class A1 a friend of the Point class.
#include <iostream>using namespace std ;class point { private: int x1 ; int y1 ;};class A1 { static void printPoint ( point p1 ) { cout << p1.x1 << " " << p1.y1 << endl ; }};int main(){ return 0 ;}