Recall & Review
beginner
What is a default constructor in C++?
A default constructor is a special constructor that can be called with no arguments. It initializes objects with default values.
Click to reveal answer
beginner
How does the compiler behave if you don't define any constructor in a class?
The compiler automatically provides a default constructor that initializes members with default values if no constructor is defined.
Click to reveal answer
beginner
Write a simple class with a default constructor that prints a message when called.#include <iostream>
class MyClass {
public:
MyClass() { std::cout << "Default constructor called\n"; }
};Click to reveal answer
intermediate
Can a default constructor have parameters?
Yes, but all parameters must have default values so that the constructor can be called without arguments.
Click to reveal answer
intermediate
What happens if you declare a constructor with parameters but no default constructor?
The compiler does not generate a default constructor automatically, so you cannot create objects without arguments unless you define one explicitly.
Click to reveal answer
What is the main purpose of a default constructor?
✗ Incorrect
A default constructor initializes an object when no arguments are provided.
If you define a constructor with parameters but no default constructor, what happens when you try to create an object without arguments?
✗ Incorrect
Without a default constructor, creating an object without arguments causes a compiler error.
Which of these is a valid default constructor declaration?
✗ Incorrect
A constructor with no parameters or with all parameters having default values can act as a default constructor.
What does the compiler do if no constructor is defined in a class?
✗ Incorrect
The compiler provides a default constructor if none is defined.
Which of the following statements about default constructors is FALSE?
✗ Incorrect
Default constructors do not always initialize members to zero; uninitialized members may have garbage values unless explicitly initialized.
Explain what a default constructor is and when the compiler provides one automatically.
Think about how objects are created without giving any values.
You got /4 concepts.
Describe what happens if you define a constructor with parameters but do not define a default constructor.
Consider what happens when you try to create an object with no arguments.
You got /4 concepts.