Explore essential concepts of C++ constructors, destructors, and operator overloading with beginner-friendly questions covering syntax, purpose, and key rules. Strengthen your C++ fundamentals and learn how object behavior is controlled in classes.
Which of the following declares a default constructor in a C++ class named Example?
Explanation: The statement Example(); correctly declares a default constructor for the Example class. void Example(); would be interpreted as a regular member function, not a constructor. ~Example(); declares a destructor, not a constructor. Example(1); defines a parameterized constructor, not a default one.
In C++, what is the correct way to define a destructor for the class Animal?
Explanation: ~Animal(); is the proper destructor declaration for the Animal class. Animal(); is a constructor, not a destructor. delete Animal(); attempts to deallocate an object, not declare a destructor. Animal::~(); is not valid C++ syntax.
What is the main purpose of a copy constructor in a C++ class?
Explanation: A copy constructor initializes a new object using the contents of an existing object of the same class. It does not initialize static members, which belong to the class itself. Destructors are responsible for destroying objects, and assigning values after construction is handled by assignment operators, not copy constructors.
When is the destructor called for a local object in C++?
Explanation: The destructor for a local object is automatically called when the scope in which the object was created ends, such as at the end of a function. Including a header file does not trigger the destructor. The constructor, not the destructor, is called during object construction. The destructor is not called at program start.
Which operator is typically overloaded to handle object assignment in C++? For example: obj1 = obj2;
Explanation: operator= specifically handles object assignment by defining how one object's values are assigned to another. operator+ is overloaded for addition, operator() for function call objects, and operator[] for array-like access. Only operator= is correct for assignment.
How is operator overloading typically implemented inside a C++ class?
Explanation: Operator overloading uses a special function with the name operator followed by the symbol being overloaded, like operator+. overload+ is not a recognized function name in C++. Macros and structures are unrelated to operator overloading.
If a C++ class allocates memory using new in its constructor, where should the corresponding delete statement be placed?
Explanation: The destructor should contain the delete statement to ensure memory allocated by new in the constructor is correctly released when the object is destroyed. Placing delete in the constructor would deallocate memory immediately after allocation. The main function or a separate private method may not automatically be called, risking memory leaks.
According to the Rule of Three in C++, which three member functions should usually be defined together in a class managing resources?
Explanation: The Rule of Three suggests defining the destructor, copy constructor, and assignment operator together if your class manages resources to avoid resource-management bugs. The first distractor is the correct set. The other options mix in operators or constructors that are not directly related to resource management.
In inheritance, how is the base class constructor called from the derived class constructor in C++?
Explanation: The base class constructor is called using the member initializer list in the derived class's constructor. Explicitly calling the base constructor inside the derived constructor body is not allowed. The 'super();' syntax does not exist in C++. The base constructor is, in fact, called automatically if not managed otherwise.
Which of the following operators cannot be overloaded in C++?
Explanation: The ternary conditional operator '?:' cannot be overloaded in C++. The addition operator '+', the assignment operator '=', and the subscript operator '[]' can all be overloaded within C++ classes. As such, the distractors are all common operator overloads, but '?:' is a built-in operator that cannot be customized.