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

C++ में Constructor Overloading

एक class अलग parameter lists के साथ कई constructors define कर सकती है, जिसे constructor overloading कहते हैं, objects को अलग-अलग तरीकों से बनने देते हुए यह depend करते हुए कि कौन से arguments supplied हैं।
Syntax
cpp
class ClassName {
public:
  ClassName() { }                    // default
  ClassName(type param) { }          // one parameter
  ClassName(type p1, type p2) { }    // two parameters
};

Constructor Overloading क्या है?

Constructor overloading का मतलब है एक class कई constructors define करती है, हर एक की parameters की अलग संख्या या type, और C++ automatically उस एक को call करता है जो object बनते समय इस्तेमाल किए arguments से match करे।

उदाहरण: What is Constructor Overloading?

cpp
// Include std::cout and std::cin
#include <iostream>

// Define the type Box
class Box {
// Members below can be accessed from outside the class
public:
	int size;
	Box() { size = 1; }
	Box(int s) { size = s; }
};

// Program execution starts in main()
int main() {
	Box a;
	Box b(10);
	// Print to the console with cout
	std::cout << a.size << " " << b.size << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}

Default Constructor

एक default constructor कोई parameters नहीं लेता और इस्तेमाल होता है जब एक object बिना किसी arguments के बनाया जाए; अगर एक class कोई भी दूसरा constructor define करे, compiler automatically इसे provide करना बंद कर देता है, और अभी भी ज़रूरत होने पर इसे explicitly लिखना होगा।

उदाहरण: The Default Constructor

cpp
#include <iostream>

class Box {
public:
	int size;
	Box() { size = 1; } // used when Box is created with no arguments
};

int main() {
	Box b;
	std::cout << b.size << std::endl;
	return 0;
}

Parameter Count से Overloading

Constructors को बस एक अलग संख्या में parameters लेकर overload किया जा सकता है, objects को creation time पर जितनी information available हो उतनी ज़्यादा या कम से initialize होने देते हुए।

उदाहरण: Overloading by Parameter Count

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) { x = px; y = 0; }
	Point(int px, int py) { x = px; y = py; }
};

// Program execution starts in main()
int main() {
	Point p1(5);
	Point p2(5, 10);
	// Print to the console with cout
	std::cout << p1.x << "," << p1.y << " " << p2.x << "," << p2.y << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}

Parameter Type से Overloading

Constructors को अलग parameter types accept करके भी overload किया जा सकता है, एक object को उदाहरण के लिए एक int या एक string दोनों से construct होने देते हुए।

उदाहरण: Overloading by Parameter Type

cpp
// Include std::cout and std::cin
#include <iostream>
// Include std::string
#include <string>

// Define the type Label
class Label {
// Members below can be accessed from outside the class
public:
	std::string text;
	Label(int number) { text = std::to_string(number); }
	Label(std::string s) { text = s; }
};

// Program execution starts in main()
int main() {
	Label a(5);
	Label b("Hi");
	// Print to the console with cout
	std::cout << a.text << " " << b.text << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}

Delegating Constructors

C++11 से, एक constructor same class के दूसरे constructor को एक initializer-list-style call इस्तेमाल करके delegate कर सकता है, overloads में duplicated initialization logic avoid करते हुए।

उदाहरण: Delegating Constructors

cpp
#include <iostream>

class Box {
public:
	int size;
	Box() : Box(1) {}         // delegates to the constructor below
	Box(int s) { size = s; }
};

int main() {
	Box a;
	std::cout << a.size << std::endl;
	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. default constructor से एक object बनाने के लिए Student s(); लिखना, जो actually इसकी बजाय एक function declare करता है।
  2. Point() के साथ Point(int x = 0) जैसे ambiguous overloads बनाना, ताकि Point p; ambiguous हो जाए।
  3. share करने की बजाय हर constructor में same initialization code copy करना।

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.