This program defines a class XYZ with two private members num and ch, both initialized with specific values. The class also declares a friend function disp which takes an object of class XYZ as its argument.
The disp function is defined outside the class and is able to access the private members of the class XYZ because it has been declared as a friend of the class. In the main function, an object obj of class XYZ is created and passed as an argument to the disp function which then displays the values of the private members of the object.
#include <iostream> using namespace std; class XYZ { private: int num=100; char ch='Z'; public: friend void disp(XYZ obj); }; void disp(XYZ obj) { cout<<obj.num<<endl; cout<<obj.ch<<endl; } int main() { XYZ obj; disp(obj); return 0; }To download raw file Click Here
Enter Student Name: Ram 100 Z
Learn All in Tamil © Designed & Developed By Tutor Joes | Privacy Policy | Terms & Conditions