Oops! It appears that you have disabled your Javascript. In order for you to see this page as it is meant to appear, we ask that you please re-enable your Javascript!

Friend function and friend classes in c++ oop with examples

Friend function and friend classes in c++ oop with examples

In this tutorial, we will learn about the followings;

  1. Friend function and friend classes in c++ oop
  2. Example of  Friend function and friend classes in c++ oop

Friend function and friend classes in c++ oop

Private and protected data of a class can be accessed by making a function as a friend function of the class.

Example of  Friend function and friend classes in c++ oop

#include <iostream>
using namespace std;
class two;
class one {
private:
int n1=1;
public:
friend int add(one, two);
};

class two {
private:
int n2=6;
public:
friend int add(one , two);
};



int main()
{
one object1;
two object2;
cout<<"Sum: "<< add(object1, object2);
return 0;
}

int add(one object1, two object2)
{
return (object1.n1 + object2.n2);
}

 

Fazal Rehman Shamil Click Here to Know More
Instructor, Researcher, Blogger, SEO Expert, Poet and Publisher of International Journal Of Software, Technology & Science ISSN : 2616 - 5325
Dear Professors and Resarchers!You are welome to Cite these tutorials in your research or slides etc. Please don't forget to mention the reference of website. Copy Paste of text is strcitly forbidden. Images can be reuse because images are protected with watermark.

Leave a Reply

Your email address will not be published. Required fields are marked *