0
0
C++programming~5 mins

Default constructor in C++ - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
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?
ATo initialize an object without any arguments
BTo copy an object
CTo destroy an object
DTo overload operators
If you define a constructor with parameters but no default constructor, what happens when you try to create an object without arguments?
AIt works fine
BThe compiler creates a default constructor automatically
CObject is initialized with garbage values
DCompiler error: no matching constructor
Which of these is a valid default constructor declaration?
AMyClass(int x = 0);
BMyClass();
CBoth B and C
DMyClass(int x);
What does the compiler do if no constructor is defined in a class?
AIt refuses to compile
BIt creates a default constructor automatically
CIt creates a destructor automatically
DIt creates a copy constructor automatically
Which of the following statements about default constructors is FALSE?
ADefault constructors always initialize members to zero
BDefault constructors can have parameters with default values
CDefault constructors can be user-defined
DIf no constructor is defined, the compiler provides a default constructor
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.