C++ Interview Questions and Answers


Answer. There is nothing like Virtual Constructor. The Constructor can’t be virtual as the constructor is a code which is responsible for creating an instance of a class and it can’t be delegated to any other object by virtual keyword means

Answer. Constructor creates an object and initializes it. It also creates vtable for virtual functions. It is different from other methods in a class.

Answer. Yes there is a Virtual Destructor. A destructor can be virtual as it is possible as at runtime depending on the type of object caller is calling to, proper destructor will be called.

Answer. A copy constructor constructs a new object by using the content of the argument object. An overloaded assignment operator assigns the contents of an existing object to another existing object of the same class.

Answer. Can a constructor throws an exception? How to handle the error when the constructor fails?

Answer. Constructor with no arguments or all the arguments has default values

Answer. Constructor which initializes the it's object member variables ( by shallow copying) with another object of the same class. If you don't implement one in your class then compiler implements one for you. for example:

  • Boo Obj1(10); // calling Boo constructor
  • Boo Obj2(Obj1); // calling boo copy constructor
  • Boo Obj2 = Obj1;// calling boo copy constructor

Answer. throw an exception. Constructors don't have a return type, so it's not possible to use return codes. The best way to signal constructor failure is therefore to throw an exception.

Answer. When derived class overrides the base class method by redefining the same function, then if client wants to access redefined the method from derived class through a pointer from base class object, then you must define this function in base class as virtual function.

Answer. The abstract class whose pure virtual method has to be implemented by all the classes which derive on these. Otherwise it would result in a compilation error. This construct should be used when one wants to ensure that all the derived classes implement the method defined as pure virtual in base class.

Answer. Virtual functions are implemented using a table of function pointers, called the vtable. There is one entry in the table per virtual function in the class. This table is created by the constructor of the class. When a derived class is constructed, its base class is constructed _rst which creates the vtable. If the derived class overrides any of the base classes virtual functions, those entries in the vtable are overwritten by the derived class constructor. This is why you should never call virtual functions from a constructor: because the vtable entries for the object may not have been set up by the derived class constructor yet, so you might end up calling base class implementations of those virtual functions

Answer. When you de_ne only function prototype in a base class without implementation and do the complete implementation in derived class. This base class is called abstract class and client won't able to instantiate an object using this base class. You can make a pure virtual function or abstract class this way.

class Boo 
{
    void foo() = 0;

}
Boo MyBoo; // compilation error

Answer. Inheritance allows one class to reuse the state and behavior of another class. The derived class inherits the properties and method implementations of the base class and extends it by overriding methods and adding additional properties and methods.

Answer. There are three acceptable answers:- "Never,""Rarely," and "When the problem domain cannot be accurately modeled any other way." Consider an Asset class, Building class, Vehicle class, and CompanyCar class. All company cars are vehicles. Some company cars are assets because the organizations own them. Others might be leased. Not all assets are vehicles. Money accounts are assets. Real estate holdings are assets. Some real estate holdings are buildings. Not all buildings are assets. Ad infinitum. When you diagram these relationships, it becomes apparent that multiple inheritance is a likely and intuitive way to model this common problem domain. The applicant should understand, however, that multiple inheritance, like a chainsaw, is a useful tool that has its perils, needs respect, and is best avoided except when nothing else will do.

Answer. Multiple Inheritance is the process whereby a child can be derived from more than one parent class. The advantage of multiple inheritance is that it allows a class to inherit the functionality of more than one base class thus allowing for modeling of complex relationships. The disadvantage of multiple inheritance is that it can lead to a lot of confusion(ambiguity) when two base classes implement a method with the same name

Answer. Polymorphism allows a client to treat di_erent objects in the same way even if they were created from di_erent classes and exhibit di_erent behaviors. You can use implementation inheritance to achieve polymorphism in languages such as C++ and Java. Base class object's pointer can invoke methods in derived class objects. You can also achieve polymorphism in C++ by function overloading and operator overloading.

Answer. The run time type identification comes at a cost of performance penalty. Compiler maintains the class.

Answer. A class is an expanded concept of a data structure: instead of holding only data, it can hold both data and functions.

Answer. The default member and base class access specifies are different. This is one of the commonly misunderstood aspects of C++. Believe it or not, many programmers think that a C++ struct is just like a C struct, while a C++ class has inheritance, access specifes, member functions, overloaded operators, and so on. Actually, the C++ struct has all the features of the class. The only differences are that a struct defaults to public member access and public base class inheritance, and a class defaults to the private access specified and private base-class inheritance.

Answer. If your class has at least one virtual function, you should make a destructor for this class virtual. This will allow you to delete a dynamic object through a caller to a base class object. If the destructor is non-virtual, then wrong destructor will be invoked during deletion of the dynamic object.

Answer. Containing and hiding Information about an object, such as internal data structures and code. Encapsulation isolates the internal complexity of an object's operation from the rest of the application. For example, a client component asking for net revenue from a business object need not know the data's origin.

Answer. The this pointer is a pointer accessible only within the member functions of a class, struct, or union type. It points to the object for which the member function is called. Static member functions do not have a this pointer. When a nonstatic member function is called for an object, the address of the object is passed as a hidden argument to the function. For example, the following function call myDate.setMonth( 3 ); can be interpreted this way: setMonth( &myDate, 3 ); The object's address is available from within the member function as the this pointer. It is legal, though unnecessary, to use the this pointer when referring to members of the class.

Answer. A container class is a class that is used to hold objects in memory or external storage. A container class acts as a generic holder. A container class has a predefined behavior and a wellknown interface. A container class is a supporting class whose purpose is to hide the topology used for maintaining the list of objects in memory. When a container class contains a group of mixed objects, the container is called a heterogeneous container; when the container is holding a group of objects that are all the same, the container is called a homogeneous container.

Answer. To override a method, a subclass of the class that originally declared the method must declare a method with the same name, return type (or a subclass of that return type), and same parameter list. The definition of the method overriding is:

  • · Must have same method name.
  • · Must have same data type.
  • · Must have same argument list.
  • Overriding a method means that replacing a method functionality in child class.
  • To imply overriding functionality we need parent and child classes. In the child class you define the same method signature as one defined in the parent class.

Answer.Space is allocated (on the stack or the heap) for the full object (that is, enough space to store the data members inherited from the base class plus the data members defined in the derived class itself) .

  • The base class's constructor is called to initialize the data members inherited from the base class
  • The derived class's constructor is then called to initialize the data members added in the derived class
  • The derived-class object is then usable When the object is destroyed (goes out of scope or is deleted) the derived class's destructor is called on the object first
  • Then the base class's destructor is called on the object Finally the allocated space for the full object is reclaimed

Answer. Malloc/free do not know about constructors and destructors. New and delete create and destroy objects, while malloc and free allocate and deallocate memory.

Answer.Both malloc and new functions are used for dynamic memory allocations and the basic difference is: malloc requires a special "typecasting" when it allocates memory for eg. if the pointer used is the char pointer then after the processor allocates memory then this allocated memory needs to be typecasted to char pointer i.e (char*).but new does not requires any typecasting. Also, free is the keyword used to free the memory while using malloc and delete the keyword to free memory while using new, otherwise this will lead the memory leak

Answer. Whenever you allocate memory with new[], you have to free the memory using delete[]. When you allocate memory with 'new', then use 'delete' without the brackets. You use new[] to allocate an array of values (always starting at the index 0).

Answer. The term alignment primarily means the tendency of an address pointer value to be a multiple of some power of two. So a pointer with two byte alignment has a zero in the least signi_cant bit. And a pointer with four byte alignment has a zero in both the two least signi_cant bits. And so on. More alignment means a longer sequence of zero bits in the lowest bits of a pointer.

Answer. No,When realloc() has to copy the allocation, it uses a bitwise copy operation, which will tear many C++ objects to shreds. C++ objects should be allowed to copy themselves. They use their own copy constructor or assignment operator.
Besides all that, the heap that new uses may not be the same as the heap that malloc() and realloc() use!