+ 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?
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
+ 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
0
What do you mean with "operator overloading"?
0
OK thanks for your advice :)
0
by using friend we can access private members of class