Friend Function and Friend Classes in C++

21 September, 2018


Related Blogs

Friend function

A non-member function cannot access private member of the class or object. Member function can access private members of “same type or same class or object. There could be a situation where more than one class where a function required to access private members of more than one type. If function is declared as friend of any class, it can able to access private members. Friend function is a non-member function, it is called like normal function. It is not bind with any object (OR) It is not called with object name. Friend function in C++ cannot private members of directly it is having parameters of type class. Friend return type function –name(parameters);  

Example of Friend Function in C++

#include <iostream.h> #include<conio.h> class beta class alpha { int x; public: void read-x(); friend void compare (alpha,bets); }; class beta { int y; public: void read-y(); friend void compare(alpha,beta); }; void alpha::read-x() { cout<<”\n Input x”; cin>>x; } void beta::read-y() { cout<<”\n Input y@; cin>>y; } void compare (alpha a, beta b) { if( cout<<”\n Equal”; else cout <<”\n Not equal”; } void main() { alpha alpha1; beta beta1;;; compare(alpha1,beta1); (friend function) }   Screenshot_21     #include <iostream.h> #include<conio.h> class Marks; class Student { int rno; char name[10]; public: void read-student(); friend void find result(student name); class marks { int sub1,sub2; public: void read-Marks(); friend void find –result (student,Marks); }; void student:: read-student() { cout<<”\n Input rollno”; cin>>rno; cout<<”\n Input name”; cin>>name; } void marks:: read-marks() { cout<<”\n  Input Marks”; cin >>sub1>>sub2; } void find –result(student s,Marks M) { cout<<”\n Roll no”<<S.rno; cout<<”\n Name”<<; if (M.sub1<40 M.sub2<40)   Screenshot_22     O/p: Input name RAM Input roll no 101 Input Marks  

//Friend function which operates on similar type objects

#include<iostream.h> #include<conio.h> class complex { float real,img; public: void read_complex(); void print_complex(); friend complex add-complex(complex,complex); }; void complex::read_complex() { cout<<”\n Input real”); cin>>real; cout<<”\n Input img”; cin>>img; } void complex:: print_complex() { cout<<”\n real=”<<real; cout<<”\n img=”<img; } complex add_complex(complex c1,complex c2) { complex c3; c3.real= c1.real+c2.real; c3.img=c1.img+c2.img; return c3; } void main() { clrscr(); complex comp1,comp2,comp3; comp1.read_complex(); comp2.read_complex(); comp3=add__complex(comp1,comp2); comp1.print__complex(); comp2.print__complex(); comp3.print__complex(); } O/p Input real 1.1 input img


Friend class in C++

A friend of class can be function or class ,having full access rights to use private members

Syntax of Friend Class

friend class-name; Example of Friend Class in C++ # include<iostream.h> class student { int rno; char name[10]; friend Marks; }; class Marks { int sub1,sub2; student stud; public: void read_marks() { cout<<”\n Input rno”; cin>>stud.rno; cout<<”\n Input Name; cin>>; cout <<”\n Input two subjects”; cin>>; cout<<”\n Input two subjects”; cin>>; cout<<”\n Input two subjects”; cin>>sub1>>sub2; } void find-result() { cout<<”\n Roll no”<<stud.rno; cout<<”\n Name”<<; if(sub1<40|| sub2<40) cout<<”\n Result Fail”; else cout<<”\n Result Pass”; }}; void Main() { Marks stud1; stud1.read_Marks(); stud1.find_result(); }   Example # include <iostream,h> class address { char hno[10]; char street[10]; public: void read_address() { cout<<”\n Input hno”; cin>>hno; cout<<”\n Input street”; cin>>street; } friend person; }; class person { char name[10]; public: void read_name() { cout<<”\n Input Name”; cin>>name; } void print_details(address a) { cout<<”\n Name”<<name; cout<<”\n Hno”<<a.hno; cout<<”\n Street”<<a.street; }}; void main() { Person p1; address p1add; p1.read_name(); p1add.read_address(); p1.print_details(p1add); }    

About Author


Author Bio

TekSlate is the best online training provider in delivering world-class IT skills to individuals and corporates from all parts of the globe. We are proven experts in accumulating every need of an IT skills upgrade aspirant and have delivered excellent services. We aim to bring you all the essentials to learn and master new technologies in the market with our articles, blogs, and videos. Build your career success with us, enhancing most in-demand skills .