A default constructor helps create an object with default values automatically.
Default constructor in C++
class ClassName { public: ClassName() { // code to set default values } };
The default constructor has no parameters.
If you don't write one, C++ creates a default constructor automatically if no other constructors exist.
class Car { public: Car() { speed = 0; color = "red"; } private: int speed; std::string color; };
class Box { public: Box() = default; };
This program creates a Person object using the default constructor. It sets name to "Unknown" and age to 0, then prints these values.
#include <iostream> #include <string> class Person { public: Person() { name = "Unknown"; age = 0; } void display() { std::cout << "Name: " << name << ", Age: " << age << std::endl; } private: std::string name; int age; }; int main() { Person p; p.display(); return 0; }
If you define any constructor with parameters, C++ will not create a default constructor automatically.
You can use = default; to ask the compiler to create a default constructor for you.
A default constructor creates objects with default values.
It has no parameters and can be written by you or generated by the compiler.
Use it to make object creation simple and consistent.
Practice
default constructor in C++?Solution
Step 1: Understand constructor basics
A constructor is a special function to create objects.Step 2: Identify default constructor meaning
A default constructor has no parameters and sets default values.Final Answer:
A constructor with no parameters that initializes objects with default values. -> Option BQuick Check:
Default constructor = no parameters, default values [OK]
- Thinking default constructor requires arguments
- Confusing destructor with constructor
- Assuming default constructor runs at program end
Solution
Step 1: Check constructor syntax
Constructor has the same name as the class and no return type.Step 2: Identify default constructor syntax
Default constructor has empty parentheses and no parameters.Final Answer:
class MyClass { MyClass() {} }; -> Option AQuick Check:
Constructor syntax = class name + () + no return type [OK]
- Adding void return type to constructor
- Using parameters in default constructor
- Incorrect placement of braces
class Box {
public:
int length;
Box() { length = 5; }
};
int main() {
Box b;
std::cout << b.length;
return 0;
}Solution
Step 1: Understand the default constructor
The constructor sets length to 5 when object b is created.Step 2: Check output statement
Printing b.length outputs the value set by constructor, which is 5.Final Answer:
5 -> Option CQuick Check:
Constructor sets length=5, output = 5 [OK]
- Assuming uninitialized int defaults to 0
- Expecting compilation error due to missing initialization
- Thinking output is random without constructor
class Sample {
public:
int x;
Sample() {
x = 10
}
};Solution
Step 1: Check constructor body syntax
Each statement inside constructor must end with a semicolon.Step 2: Locate missing semicolon
The line 'x = 10' lacks a semicolon at the end.Final Answer:
Missing semicolon after x = 10 -> Option DQuick Check:
Statements need semicolons [OK]
- Adding return type to constructor
- Ignoring missing semicolon errors
- Confusing constructor name with return type
class Point {
public:
int x, y;
Point() { x = 0; y = 0; }
Point(int a, int b) { x = a; y = b; }
};
// In main:
Point p1;
Point p2(3, 4);
std::cout << p1.x << "," << p1.y << " " << p2.x << "," << p2.y;What is the output and why?
Solution
Step 1: Understand constructor calls
p1 is created with no arguments, so default constructor sets x=0, y=0.Step 2: Understand parameterized constructor
p2 is created with (3,4), so x=3, y=4 from parameterized constructor.Step 3: Check output statement
Output prints p1's coordinates then p2's coordinates separated by space.Final Answer:
0,0 3,4 because p1 uses default constructor, p2 uses parameterized constructor. -> Option AQuick Check:
Default and parameterized constructors work together [OK]
- Assuming default constructor overrides parameterized
- Expecting compilation error with multiple constructors
- Mixing up which constructor is called
Ready to prove it?
Pass the test and this pattern moves unknown
