Sunday, November 28, 2010

Friend of a friend and Parent's friend in C++

If A is a friend class of B and B is a friend of C then can we say A and C are friends?
No. If there will be no friend relation between those classes, if class A is a friend of B then A can access the data members(including private) of class B, and if class B is a friend of class C, similarly class C can have access to class B's data members, but can't access the member of class A which is already a friend of B. i.e can't access friend of a friend's member. It is the same concept that If Alice is bob's friend and if Bob is your friend then it will not be always true that Alice is your friend too(hint:- or even if Alice is your friend too, you will not have access to her private properties, that bob has access to).



class A
{
....
....
};
class B
{
.....
.....
friend class A;
};
class C
{
......
......
friend class B;
};

Friend of Parent

Similarly it is same in the case of inheritance too. If class A is a parent of class B and class F is friend of class A (Parent of B) then class F have access to class A's private attributes members, but not class B's. It is same concept that if your parent has a friend he have some right to access to your parent's properties, it doesn't mean that he has the same right to access your properties too.



class A
{
....
....
friend class F;
};
class B : public/private/protected A
{
};

Experience Note:-
A similar question was asked during NIC(National Informatics Center), India Recruitment Test. The NIC test was purely technical and there was questions from almost all areas of computer science including Data structure, Computer networks, Computer architecture, C Programming, C++ programming, Compiler design and Theory of computation (Automata and computer languages), Database systems, Software Engineering, Cryptography and Network Security, Digital logic and  basic electronics, awareness about current software systems available and few Linux commands based questions for example 

which is not a relational database in the given four choices (choices like Reflex, mysql, fourth dimension, oracle)
which of the following is an object based language
which of the following cant be used to make web pages etc (java services, jsp, asp etc)

No comments:

Post a Comment