← Back to C++ Course | Chapter 8: OOP Core | Lesson 5 of 14

C++ के Constructors

एक constructor वह setup step है जो एक नया object बनने पर चलता है, एक नए student के लिए एक name tag भरने जैसा। यह object को इसकी पहली values देता है।
Syntax
cpp
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?

cpp
// 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;
}

Default Constructor

एक default constructor कोई arguments नहीं लेता।

अगर आप खुद कोई constructor define नहीं करते, C++ चुपचाप आपके लिए एक blank default constructor generate करता है — हालांकि जैसे ही आप अपना कोई constructor लिखें, वह automatic वाला गायब हो जाता है और अगर आपको अभी भी इसकी ज़रूरत हो तो इसे explicitly provide करना होगा।

उदाहरण: Default Constructor

cpp
#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;
}

Parameterized Constructor

एक parameterized constructor arguments accept करता है, आपको creation के moment पर ही एक object की member variables को specific values से initialize करने देते हुए बाद में separate assignment statements से एक-एक करके set करने की बजाय।

उदाहरण: Parameterized Constructor

cpp
// 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;
}

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

cpp
// 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;
}

Constructor Overloading

आप same class में अलग parameter lists के साथ कई constructors define कर सकते हैं, एक technique जिसे constructor overloading कहते हैं।

C++ object बनाते समय आपके दिए arguments की संख्या और types के आधार पर automatically matching constructor select करता है।

उदाहरण: Constructor Overloading

cpp
// 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;
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. एक constructor को void Student() जैसा एक return type देना, जो इसे एक ordinary method में बदल देता है।
  2. एक parameterized constructor define करना और फिर Student s; लिखना, जो fail होता है क्योंकि compiler अब एक default constructor generate नहीं करता।
  3. constructor body में const या reference members assign करना initializer list की बजाय, जो एक compile error है।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.