C++ के Constructors
In this page:
class ClassName {
public:
ClassName() {
// runs when an object is created
}
};
Constructor क्या है?
एक constructor एक special member function है जो हर बार एक object बनने पर automatically चलता है। यह class के exact same नाम को share करता है, इसका बिल्कुल कोई return type नहीं (void भी नहीं), और यह एक object की initial state setup करने की natural जगह है।
उदाहरण: What is a Constructor?
// Include std::cout and std::cin
#include <iostream>
// Define the type Car
class Car {
// Members below can be accessed from outside the class
public:
Car() { std::cout << "Car created" << std::endl; }
};
// Program execution starts in main()
int main() {
Car myCar;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Default Constructor
एक default constructor कोई arguments नहीं लेता।
अगर आप खुद कोई constructor define नहीं करते, C++ चुपचाप आपके लिए एक blank default constructor generate करता है — हालांकि जैसे ही आप अपना कोई constructor लिखें, वह automatic वाला गायब हो जाता है और अगर आपको अभी भी इसकी ज़रूरत हो तो इसे explicitly provide करना होगा।
उदाहरण: Default Constructor
#include <iostream>
class Box {
public:
int size = 10; // no constructor written: compiler generates a blank default one
};
int main() {
Box b;
std::cout << b.size << std::endl;
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Parameterized Constructor
एक parameterized constructor arguments accept करता है, आपको creation के moment पर ही एक object की member variables को specific values से initialize करने देते हुए बाद में separate assignment statements से एक-एक करके set करने की बजाय।
उदाहरण: Parameterized Constructor
// Include std::cout and std::cin
#include <iostream>
// Define the type Point
class Point {
// Members below can be accessed from outside the class
public:
int x, y;
Point(int px, int py) {
x = px;
y = py;
}
};
// Program execution starts in main()
int main() {
Point p(3, 4);
// Print to the console with cout
std::cout << p.x << "," << p.y << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Constructor Initializer List
एक constructor initializer list member variables initialize करने का preferred तरीका है, क्योंकि यह उन्हें constructor body के actually चलना शुरू करने से पहले सीधे initialize करता है — यह body के अंदर values assign करने से ज़्यादा efficient है, और const या reference members के लिए required है।
उदाहरण: Constructor Initializer List
// Include std::cout and std::cin
#include <iostream>
// Define the type Point
class Point {
// Members below can be accessed from outside the class
public:
int x, y;
Point(int px, int py) : x(px), y(py) {}
};
// Program execution starts in main()
int main() {
Point p(3, 4);
// Print to the console with cout
std::cout << p.x << "," << p.y << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Constructor Overloading
आप same class में अलग parameter lists के साथ कई constructors define कर सकते हैं, एक technique जिसे constructor overloading कहते हैं।
C++ object बनाते समय आपके दिए arguments की संख्या और types के आधार पर automatically matching constructor select करता है।
उदाहरण: Constructor Overloading
// Include std::cout and std::cin
#include <iostream>
// Define the type Point
class Point {
// Members below can be accessed from outside the class
public:
int x, y;
Point() : x(0), y(0) {}
Point(int px, int py) : x(px), y(py) {}
};
// Program execution starts in main()
int main() {
Point p1;
Point p2(3, 4);
// Print to the console with cout
std::cout << p1.x << " " << p2.x << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
- एक constructor को
void Student()जैसा एक return type देना, जो इसे एक ordinary method में बदल देता है। - एक parameterized constructor define करना और फिर
Student s;लिखना, जो fail होता है क्योंकि compiler अब एक default constructor generate नहीं करता। - constructor body में const या reference members assign करना initializer list की बजाय, जो एक compile error है।
Chapter Quiz — Complete all 14 topics to unlock
0/14 topics done
Complete these topics first: