Constructors in C++
Written and reviewed by Gagan Bhardwaj · Senior IT Faculty · 15+ years’ experience
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:
#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;
}Unknown 0
Parameterized constructor
Take arguments to initialise an object with specific values:
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
}Toyota 80
Overloading constructors
A class can have several constructors with different parameters — the compiler picks the right one:
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:
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
constmembers in the body instead of the initializer list.
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.
Constructor क्या है?
Constructor एक विशेष function है जो object बनते ही अपने आप चलता है, उसके data को सेट करने के लिए। इसका नाम class जैसा ही होता है और कोई return type नहीं। इसके बिना, नए object के members अप्रत्याशित values से शुरू होते।
Default constructor
Default constructor कोई arguments नहीं लेता और समझदार शुरुआती values देता है:
#include <iostream>
using namespace std;
class Car {
public:
string brand;
int speed;
Car() { // default constructor
brand = "Unknown";
speed = 0;
}
};
int main() {
Car c; // constructor yahan chalta hai
cout << c.brand << " " << c.speed << "\n";
return 0;
}Unknown 0
Parameterized constructor
Object को विशिष्ट values से initialise करने को arguments लें:
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
}Toyota 80
Constructors overload करना
एक class में अलग parameters वाले कई constructors हो सकते हैं — compiler सही चुनता है:
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
Body के अंदर assign करने के बजाय, आप members को colon list से सीधे initialise कर सकते हैं — तेज़, और const members तथा references के लिए ज़रूरी:
class Point {
public:
int x, y;
Point(int a, int b) : x(a), y(b) { } // initializer list
};यहाँ x(a), y(b) members को body चलने से पहले initialise करता है।
आम गलतियाँ
- Constructor को return type देना — इसका कोई नहीं होना चाहिए।
- यह मानना कि दूसरा constructor जोड़ने के बाद भी मुफ़्त default constructor रहता है।
- Constructor का नाम class से अलग रखना।
constmembers को initializer list के बजाय body में assign करना।
एक Student class लिखें जिसमें name और marks के लिए parameterized constructor, साथ में एक default constructor हो। दोनों तरीकों से objects बनाएँ और print करें। एक member initializer list version जोड़ें।
सारांश
- Constructor object बनने पर अपने आप चलता है।
- यह class का नाम साझा करता है और कोई return type नहीं रखता।
- Default कोई arguments नहीं लेता; parameterized values लेता है।
- Constructors अलग parameter सूचियों से overload हो सकते हैं।
- Member initializer lists कुशल हैं और const/reference members के लिए ज़रूरी।
अक्सर पूछे जाने वाले प्रश्न (FAQ)
C++ में constructor क्या है?
Car() Car नामक class का constructor है।Default और parameterized constructor में क्या अंतर है?
Car c("Toyota", 80);।क्या C++ में एक class में एक से ज़्यादा constructors हो सकते हैं?
C++ में member initializer list क्या है?
Constructor() : member(value) { } से। यह body के अंदर assign करने से ज़्यादा कुशल है और const members तथा references के लिए ज़रूरी है।