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
A constructor method is defined within a class by using the ClassName
followed by parentheses. It does not have a return type.
class ClassName {
public:
ClassName() {
// Constructor code
}
};
Examples
Basic Constructor
A basic constructor, also known as a default constructor, is a special type of constructor in C++ that takes no parameters.
The compiler will automatically provide a default constructor if there is no constructor explicitly defined for a class.
This constructor is used for initializing an object when no specific values are needed, and it also initializes member variables to default values.
class MyClass {public:// Define a basic constructorMyClass() {std::cout << "The constructor was executed!";}};int main() {MyClass myObject; // This calls the constructorreturn 0;}
The result of the following code:
The constructor was executed!
Parameterized Constructor
A parameterized constructor is one that accepts arguments, allowing initial values to be set for an object’s attributes at the time of creation. This constructor is useful for initializing objects with custom data or values that differ between instances.
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 argumentsCircle myCircle1(5,5,10);Circle myCircle2(0,0,5);// Print out the attribute values set for each objectstd::cout << "X=" << myCircle1.X << ", Y=" << myCircle1.Y << ", radius=" << myCircle1.radius << "\n";// Output: X=5, Y=5, radius=10std::cout << "X=" << myCircle2.X << ", Y=" << myCircle2.Y << ", radius=" << myCircle2.radius << "\n";// Output: X=0, Y=0, radius=5return 0;}
The result of the following code:
X=5, Y=5, radius=10X=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 constructorCircle (int a, int b, int c);};// Define constructor outside the classCircle::Circle(int a, int b, int c) {X = a;Y = b;radius = c;};
Codebyte Example
The following example defines a simple class called Circle
that has a single public attribute radius
, and a default constructor that initializes this attribute.
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn C++ on Codecademy
- Career path
Computer Science
Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!Includes 6 CoursesWith Professional CertificationBeginner Friendly75 hours - Free course
Learn C++
Learn C++ — a versatile programming language that’s important for developing software, games, databases, and more.Beginner Friendly11 hours