This is a C++ program that demonstrates the use of friend classes to access private data members of another class. The program defines two classes, A and B. Class A has a private integer data member x initialized to 5, and class B is declared as a friend of class A. The program also defines a member function display in class B, which takes an object of class A as its argument and prints the value of its private data member x.
In the main function, the program creates an object of classA and an object of class B. The display function of class B is then called, passing the object of class A as an argument. The display function then accesses the private data member x of the object of class A using the friend declaration, and prints its value in the console.
#include<iostream> using namespace std; class A { int x =5; friend class B; }; class B { public: void display(A &a) { cout<<"value of x is : "<<a.x; } }; int main() { A a; B b; b.display(a); return 0; }To download raw file Click Here
value of x is : 5
Learn All in Tamil © Designed & Developed By Tutor Joes | Privacy Policy | Terms & Conditions