Constructors

A constructor is a specially defined method in a C++ class that is automatically called when an instance of that class is created. It is typically used for tasks such as initializing class attributes in a new object. Like with functions, a constructor can take arguments that can aid in the initialization. The arguments are passed to the constructor method when a new object is created.

Syntax and Examples

A constructor method is defined within a class by using the class name followed by parenthesis:

class MyClass {
public:
MyClass() { // This defines a constructor that outputs a message
std::cout << "The constructor was executed!";
}
};
int main() {
MyClass myObj; // This calls the constructor
return 0;
}

Using arguments in a constructor:

class Circle {
public:
int X;
int Y;
int radius;
Circle (int a, int b, int c) {
X = a;
Y = b;
radius = c;
}
};
int main() {
// Call the constructor using arguments
Circle myCircle1(5,5,10);
Circle myCircle2(0,0,5);
// Print out the attribute values set for each object
std::cout << "X=" << myCircle1.X << ", Y=" << myCircle1.Y << ", radius=" << myCircle1.radius << "\n";
std::cout << "X=" << myCircle2.X << ", Y=" << myCircle2.Y << ", radius=" << myCircle2.radius << "\n";
return 0;
}
// Output:
// X=5, Y=5, radius=10
// X=0, Y=0, radius=5

Like member functions, once declared in the class, the constructor can be defined outside the class:

class Circle {
public:
int X;
int Y;
int radius;
// Declare the constructor
Circle (int a, int b, int c);
};
// Define constructor outside the class
Circle::Circle (int a, int b, int c) {
X = a;
Y = b;
radius = c;
};

Contributors

Interested in helping build Docs? Read the Contribution Guide or share your thoughts in this feedback form.

Learn C++ on Codecademy