There are some threads debating about friend class , is violating encapsulation (one of the important oops concept). there are some genuine case where we need friend ..
Before that we will discuss about the friend class .. not much briefly :)
well, A very good name given to this concept by the inventor. In general , we are sharing our personal (private) data to our friends only . Similarly A Class can share its private data to its friend class .
lets look at this code
class nanban;
class dude
{
int data ;
friend class nanban;
} ;
now , nanban class has the privilege to access dude class's private data ..
Coming to the point , where it can be used
Unit testing
To accomplish this , we need to write some additional piece of code to test the main code . There we can't write additional function for this purpose .
lets take the above example
dude class only contains data (by default it is private) , I want to test the value of data from the testing framework , Instead of writing a public function (introducing function increases .exe size) which will return the value of data , I am making nanban class as friend of dude .So that I can get the value directly.
Well , thats it . any quires !!!
Before that we will discuss about the friend class .. not much briefly :)
well, A very good name given to this concept by the inventor. In general , we are sharing our personal (private) data to our friends only . Similarly A Class can share its private data to its friend class .
lets look at this code
class nanban;
class dude
{
int data ;
friend class nanban;
} ;
now , nanban class has the privilege to access dude class's private data ..
Coming to the point , where it can be used
Unit testing
To accomplish this , we need to write some additional piece of code to test the main code . There we can't write additional function for this purpose .
lets take the above example
dude class only contains data (by default it is private) , I want to test the value of data from the testing framework , Instead of writing a public function (introducing function increases .exe size) which will return the value of data , I am making nanban class as friend of dude .So that I can get the value directly.
Well , thats it . any quires !!!
No comments:
Post a Comment