+ 2

What affects has the keyword "friend" in my class, if used?

How it works? When is better used? And in what should I pay attention in order to work like charm?

11th Dec 2016, 8:32 AM
Georgios Bariamai
Georgios Bariamai - avatar
5 odpowiedzi
+ 3
friend is used in class when we want an outside function(non memeber function) to access private members of a class too. as- class example{ int data; public: friend void func(example e); }; void func(example e) { cout<<e.data;} this was general syntax. u have to always access using object name and dot operator like e.data not directly as with memeber functions. best application of friend function comes when want -operator overloading -objects of two diff classes to communicate with themselves. -give one class full access of other class
11th Dec 2016, 8:48 AM
manish rawat
manish rawat - avatar
+ 3
boy u have to study more to know this. do u know dunction overloading??do u know classes?? if yes then u can understand this we have + to add two intgers,floats,doubles or their combinations. what if we have a class complex which contains complex no as real and imaginary part?? and we want to add them same way as normal intger and floats . here we can use operator overloading. changing meaning of an operator for different classes
11th Dec 2016, 7:17 PM
manish rawat
manish rawat - avatar
0
What do you mean with "operator overloading"?
11th Dec 2016, 6:58 PM
Georgios Bariamai
Georgios Bariamai - avatar
0
OK thanks for your advice :)
16th Dec 2016, 7:26 PM
Georgios Bariamai
Georgios Bariamai - avatar
0
by using friend we can access private members of class
27th Dec 2016, 5:06 PM
Prathamesh Patil
Prathamesh Patil - avatar