1.

If two classes have exactly same data members and member function and only they differ by class name. Can copy constructor be used to initialize one class object with another class object?(a) Yes, possible(b) Yes, because the members are same(c) No, not possible(d) No, but possible if constructor is also sameThis question was addressed to me during a job interview.I want to ask this question from Copy Constructor in chapter Constructors and Destructors of Object Oriented Programming

Answer»

The correct answer is (c) No, not possible

Easiest explanation - The restriction for COPY constructor is that it must be USED with the object of same class. Even if the classes are exactly same the constructor won’t be ABLE to access all the MEMBERS of ANOTHER class. Hence we can’t use object of another class for initialization.



Discussion

No Comment Found

Related InterviewSolutions