Sunday, 24 February 2019

Write a Program using Friend class in c++

  Program to implement the concept of Friend class in c++


1.Open your turbo c++ compiler and start a new program.
2. Copy the given code in your program





#include<iostream.h>
#include<conio.h>
class A
{
 private: void show()
 {
cout<<"\n This is a private function of class A";

 }
friend class B;//Making class B as friend of A

};
class B
{
 public: void display()
 {
 A obj1; //Making object of class A

 obj1.show();  //Accessing private member function
     // of class A in Friend class B
 }

};

void main()
{clrscr(); //Main Program
 B obj;
 obj.display();    //calling display of class B
 // which is calling show of A
   getch();

}











3. Now run the program output will be like this:-


No comments:

Post a Comment

👇 🌐 Top 10 Hosting for Developers in 2025: Fast, Reliable & Affordable

 Whether you’re a beginner building your first portfolio site or a professional developer deploying client projects, choosing the right host...