C++ Constructors & Function Overloading Interview Questions
C++ Constructors
Basic Level (15 Questions)
What is a constructor in C++?
A constructor is a special member function that initializes objects of a class. It has the same name as the class, no return type (not even void), and is automatically called when an object is created.
What are the different types of constructors in C++?
C++ has several constructor types: Default constructor (no parameters), Parameterized constructor (with parameters), Copy constructor (creates object as copy), Move constructor (C++11, transfers resources), and Delegating constructor (C++11, calls another constructor).
What is a default constructor?
A default constructor is a constructor that can be called with no arguments. It either has no parameters or all parameters have default values. If no constructor is defined, compiler provides an implicit default constructor.
What is a copy constructor?
A copy constructor creates a new object as a copy of an existing object. It takes a reference to an object of the same class as its parameter: ClassName(const ClassName &obj). Used when objects are passed by value, returned by value, or explicitly copied.
When is a copy constructor called?
A copy constructor is called in these situations: 1) When an object is passed by value to a function, 2) When an object is returned by value from a function, 3) When an object is initialized using another object of the same class, 4) When compiler creates a temporary object.
What is a destructor in C++?
A destructor is a special member function that cleans up resources when an object is destroyed. It has the same name as the class preceded by a tilde (~), takes no parameters, and has no return type. Called automatically when object goes out of scope.
What is constructor overloading?
Constructor overloading is defining multiple constructors with different parameter lists in the same class. This allows objects to be initialized in different ways. Example: Rectangle(), Rectangle(int w), Rectangle(int w, int h).
What is the difference between initialization list and assignment in constructors?
Initialization list directly initializes member variables: ClassName() : member1(value1), member2(value2) {}. Assignment sets values in constructor body. Initialization list is more efficient, especially for const members and references which must be initialized.
What is a delegating constructor (C++11)?
A delegating constructor is a constructor that calls another constructor of the same class in its initialization list. This avoids code duplication. Syntax: ClassName() : ClassName(defaultValue) {}
What is the Rule of Three/Five in C++?
The Rule of Three: If a class needs a custom destructor, copy constructor, or copy assignment operator, it likely needs all three. The Rule of Five (C++11): Adds move constructor and move assignment operator to the rule.
Review question
Review the tutorial for this topic.
Review question
Review the tutorial for this topic.
Review question
Review the tutorial for this topic.
Review question
Review the tutorial for this topic.
Review question
Review the tutorial for this topic.
Tricky Level (10 Questions)
Review question
Review the tutorial for this topic.
Review question
Review the tutorial for this topic.
Review question
Review the tutorial for this topic.
Review question
Review the tutorial for this topic.
Review question
Review the tutorial for this topic.
What is object slicing?
Assigning derived object to base by value slices off derived part—polymorphism requires pointers or references.
When is virtual destructor required?
When deleting derived objects through base pointers—otherwise derived destructor may not run (UB).
What is dynamic_cast?
Runtime checked downcast for polymorphic types; returns nullptr on failure for pointers, throws std::bad_cast for references.
Can constructors be virtual?
No. Construction order is static; use factory functions or std::make_unique returning base pointers to derived.
What is vtable?
Compiler-generated table of virtual function pointers per class; each object with virtual functions holds a vptr to its vtable.
Note: These questions cover essential concepts of constructors and function overloading in C++. Understanding constructor types, their usage, and overloading principles is crucial for effective object-oriented programming in C++. This page lists 15 basic and 10 tricky questions—use the tutorial and MCQ links above and below.