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

C++ Constructor Overloading

A class can define multiple constructors with different parameter lists, called constructor overloading, letting objects be created in several different ways depending on which arguments are supplied.
Syntax
cpp
class ClassName {
public:
  ClassName() { }                    // default
  ClassName(type param) { }          // one parameter
  ClassName(type p1, type p2) { }    // two parameters
};

What is Constructor Overloading?

Constructor overloading means a class defines several constructors, each with a different number or type of parameters, and C++ automatically calls the one matching the arguments used when an object is created.

Example: 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;
}

The Default Constructor

A default constructor takes no parameters and is used when an object is created without any arguments; if a class defines any other constructor, the compiler stops providing this one automatically, and it must be written explicitly if still needed.

Example: 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;
}

Overloading by Parameter Count

Constructors can be overloaded simply by taking a different number of parameters, letting objects be initialized with as much or as little information as is available at creation time.

Example: 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;
}

Overloading by Parameter Type

Constructors can also be overloaded by accepting different parameter types, letting an object be constructed from, for example, either an int or a string.

Example: 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

Since C++11, one constructor can delegate to another constructor of the same class using an initializer-list-style call, avoiding duplicated initialization logic across overloads.

Example: 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. #}
Common Mistakes
  1. Writing Student s(); to create an object with the default constructor, which actually declares a function instead.
  2. Creating ambiguous overloads such as Point(int x = 0) alongside Point(), so Point p; is ambiguous.
  3. Copying the same initialization code into every constructor instead of sharing it.

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.