C++ Constructors
In this page:
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?
// 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
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
#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
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
// 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
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
// 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
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
// 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
- Giving a constructor a return type such as
void Student(), which turns it into an ordinary method. - Defining a parameterized constructor and then writing
Student s;, which fails because the compiler no longer generates a default constructor. - Assigning const or reference members in the constructor body instead of the initializer list, which is a compile error.
Chapter Quiz — Complete all 14 topics to unlock
0/14 topics done
Complete these topics first: