Introduction to C++ | Constructor
C++ Constructor
A Constructor in C++ is a special member function that is automatically called when an object of a class is created. It is used to initialize the object’s data members and allocate resources when the object is created. The constructor has the same name as the class and does not have a return type.
What is a Constructor?
A Constructor is a function that gets executed when an object of a class is created. Its primary job is to initialize the object. A constructor can either have parameters or be parameterless. There are two types of constructors:
- Default Constructor: A constructor that takes no arguments.
- Parameterized Constructor: A constructor that takes arguments to initialize an object with specific values.
Syntax of Constructor
The basic syntax of a constructor is as follows:
Syntax of Constructor
class ClassName {
public:
// Constructor with no arguments
ClassName() {
// initialization code
}
// Constructor with parameters
ClassName(DataType param) {
// initialization code using param
}
};
Example of a Default Constructor
A default constructor is one that does not take any parameters. It initializes the object with default values:
Code Example: Default Constructor
#include <iostream>
using namespace std;
class Car {
public:
string brand;
int year;
// Default constructor
Car() {
brand = "Toyota";
year = 2020;
}
// Member function to display car details
void displayDetails() {
cout << "Brand: " << brand << ", Year: " << year << endl;
}
};
int main() {
// Creating an object of class Car using the default constructor
Car myCar;
// Displaying the details of the car
myCar.displayDetails(); // Output: Brand: Toyota, Year: 2020
return 0;
}
Output:
Example of a Parameterized Constructor
A parameterized constructor takes arguments and initializes the object with specific values:
Code Example: Parameterized Constructor
#include <iostream>
using namespace std;
class Car {
public:
string brand;
int year;
// Parameterized constructor
Car(string b, int y) {
brand = b;
year = y;
}
// Member function to display car details
void displayDetails() {
cout << "Brand: " << brand << ", Year: " << year << endl;
}
};
int main() {
// Creating an object of class Car using the parameterized constructor
Car myCar("Honda", 2022);
// Displaying the details of the car
myCar.displayDetails(); // Output: Brand: Honda, Year: 2022
return 0;
}
Output:
Constructor Overloading
C++ allows multiple constructors in a class with different parameter lists. This is called constructor overloading. It allows you to create objects with different ways of initializing their data members:
Code Example: Constructor Overloading
#include <iostream>
using namespace std;
class Car {
public:
string brand;
int year;
// Default constructor
Car() {
brand = "Unknown";
year = 0;
}
// Parameterized constructor
Car(string b, int y) {
brand = b;
year = y;
}
// Member function to display car details
void displayDetails() {
cout << "Brand: " << brand << ", Year: " << year << endl;
}
};
int main() {
// Creating an object of class Car using the default constructor
Car myCar1;
myCar1.displayDetails(); // Output: Brand: Unknown, Year: 0
// Creating an object of class Car using the parameterized constructor
Car myCar2("Ford", 2021);
myCar2.displayDetails(); // Output: Brand: Ford, Year: 2021
return 0;
}
Output:
Brand: Ford, Year: 2021
Pro Tip:
💡 Pro Tip
Constructors are an essential part of object creation and initialization. When you create a class, always define constructors that make it easy to instantiate objects with appropriate values. Constructor overloading can help when you need multiple ways to create an object with different initialization options.