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

C++ Constructors

A constructor is the setup step that runs when a new object is made, like filling in a name tag for a new student. It gives the object its first values.
Syntax
cpp
class ClassName {
public:
  ClassName() {
    // runs when an object is created
  }
};

What is a Constructor?

A constructor is a special member function that runs automatically every time an object is created. It shares the exact same name as the class, has no return type at all (not even void), and is the natural place to set up an object's initial state.

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

A default constructor takes no arguments.

If you don't define any constructor yourself, C++ silently generates a blank default one for you — though as soon as you write any constructor of your own, that automatic one disappears and you must provide it explicitly if you still need it.

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

A parameterized constructor accepts arguments, letting you initialize an object's member variables with specific values right at the moment of creation instead of setting them one by one afterward through separate assignment statements.

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

A constructor initializer list is the preferred way to initialize member variables, since it initializes them directly before the constructor body even starts running — this is more efficient than assigning values inside the body, and it's required for const or reference members.

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

You can define multiple constructors in the same class with different parameter lists, a technique called constructor overloading.

C++ automatically selects the matching constructor based on the number and types of arguments you supply when creating the object.

Example: 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. #}
Common Mistakes
  1. Giving a constructor a return type such as void Student(), which turns it into an ordinary method.
  2. Defining a parameterized constructor and then writing Student s;, which fails because the compiler no longer generates a default constructor.
  3. Assigning const or reference members in the constructor body instead of the initializer list, which is a 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.