Free tutorials & notes in Hindi & English · Clean code examples 🧰 Free PDF & Image Tools →
🟣 OOP  ·  Lesson 39

Constructors in C++

In this lesson: Default, parameterized and copy constructors in C++ plus initializer lists — with examples and output. In Hindi & English.

What is a constructor?

A constructor is a special function that runs automatically the moment an object is created, to set up its data. It has the same name as the class and no return type. Without one, a new object's members would start with unpredictable values.

Default constructor

A default constructor takes no arguments and gives sensible starting values:

C++
#include <iostream>
using namespace std;
class Car {
public:
    string brand;
    int speed;
    Car() {                 // default constructor
        brand = "Unknown";
        speed = 0;
    }
};
int main() {
    Car c;                  // constructor runs here
    cout << c.brand << " " << c.speed << "\n";
    return 0;
}
Output:
Unknown 0

Parameterized constructor

Take arguments to initialise an object with specific values:

C++
class Car {
public:
    string brand;
    int speed;
    Car(string b, int s) {    // parameterized
        brand = b;
        speed = s;
    }
};
int main() {
    Car c("Toyota", 80);
    cout << c.brand << " " << c.speed;   // Toyota 80
}
Output:
Toyota 80

Overloading constructors

A class can have several constructors with different parameters — the compiler picks the right one:

C++
class Box {
public:
    int w, h;
    Box() { w = h = 1; }              // default
    Box(int side) { w = h = side; }   // square
    Box(int a, int b) { w = a; h = b; } // rectangle
};

Member initializer list

Instead of assigning inside the body, you can initialise members directly with a colon list — faster, and required for const members and references:

C++
class Point {
public:
    int x, y;
    Point(int a, int b) : x(a), y(b) { }   // initializer list
};

Here x(a), y(b) initialises the members before the body runs.

Common mistakes

  • Giving the constructor a return type — it must have none.
  • Assuming the free default constructor still exists after you add another constructor.
  • Naming the constructor differently from the class.
  • Assigning const members in the body instead of the initializer list.
🏋️ Practice

Write a Student class with a parameterized constructor for name and marks, plus a default constructor. Create objects both ways and print them. Add a member initializer list version.

Summary

  • A constructor runs automatically when an object is created.
  • It shares the class name and has no return type.
  • Default takes no arguments; parameterized takes values.
  • Constructors can be overloaded with different parameter lists.
  • Member initializer lists are efficient and needed for const/reference members.

Frequently Asked Questions

What is a constructor in C++?
A constructor is a special member function that runs automatically when an object is created, used to initialise the object's data. It has the same name as the class and no return type, so Car() is the constructor of a class called Car.
What is the difference between a default and a parameterized constructor?
A default constructor takes no arguments and sets up an object with default values. A parameterized constructor takes arguments so you can initialise an object with specific values at creation, such as Car c("Toyota", 80);.
Can a class have more than one constructor in C++?
Yes. A class can have several constructors as long as their parameter lists differ — this is constructor overloading. The compiler picks the matching one based on the arguments you pass when creating the object, just like overloading ordinary functions.
What is a member initializer list in C++?
A member initializer list initialises data members directly, before the constructor body runs, using the syntax Constructor() : member(value) { }. It is more efficient than assigning inside the body and is required for const members and references.
What happens if you do not define a constructor in C++?
If you write no constructor, the compiler provides an implicit default constructor that does no special initialisation. But as soon as you define any constructor yourself, that automatic default is no longer generated, so you must add one explicitly if you still need it.
← Back to C++ Tutorial
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

💻 Live Code Editor

This page's programs are ready here — run them, edit them, and learn. No installation needed.
Powered by OneCompiler. The code loads into the editor automatically — press Run to see the output. If the editor does not open, open it in a new tab.
Next topicDestructors in C++ Your progress: 39 / 81 (48%) · Lesson 40 / 81 Continue →

How was this lesson?

Your feedback helps us make the content better.

Write a review or suggestion (optional)

After approval, your name and city may appear publicly with your review. Please do not write your email or phone number.

▶ CodingEasily on YouTube

Learn with video lessons in easy Hindi

Featured video: RDBMS Part 3 – Class XI Commerce IT

▶ Subscribe – it’s free