C++ self-assignment -If object assigned to itself what happens

Answer: Self assignment of object in C++ is OK only if object declaration and initialization has been already done before. Otherwise, we may get compiler error for object declaration or run time crash, stating object has not been initialized. In below complete example, in class A, we have declared an object int Id and has … Read more C++ self-assignment -If object assigned to itself what happens

Implement an objects counter mechanism for a class in C++

Interview question: Can you implement an object counter program in C++ for a class that count the number of objects created for that class. Answer: Actually, counting object in C++ implementation is simple. Solution is, to create a static variable, say “objCount” and a static method like “GetObjectCount()” that returns number of objects created for … Read more Implement an objects counter mechanism for a class in C++

We have 50 classes and out of these we want to make 10 classes noncopyable, means, we cannot copy or assign objects of same class type. How would you achieve this? Note that we cannot change the body of classes.

Answer: One of the solutions is, create a separate base class, say “NonCopyable” and make copy constructor and assignment operator declaration private leaving constructor and destructor public or having default constructor. When we create object of existing class it will first call the constructor of base(NonCopyalbe) class then existing class. Same will happen for copy … Read more We have 50 classes and out of these we want to make 10 classes noncopyable, means, we cannot copy or assign objects of same class type. How would you achieve this? Note that we cannot change the body of classes.