4.3) Constructors in C++
Constructors are special member functions in object-oriented programming that are used to initialize objects of a class. They are automatically called when an object is created and are responsible for setting initial values to the attributes (data members) of the object.
Constructors have the same name as the class and do not have a return type, not even void. They play a crucial role in object initialization and ensuring that objects are in a valid and usable state upon creation.
Here are the key points to understand about constructors:
Table of Contents
1. Default Constructor
A default constructor is a constructor that is called when an object is created without any arguments. If no constructors are defined in the class, the compiler automatically generates a default constructor that initializes all attributes to default values.
class MyClass {
public:
MyClass() {
// Default constructor
}
};
2. Parameterized Constructor
A parameterized constructor is used to initialize object attributes with specific values provided as arguments during object creation. It allows you to customize the initialization process based on the arguments passed.
class Person {
public:
Person(std::string n, int a) {
name = n;
age = a;
}
private:
std::string name;
int age;
};
3. Constructor Overloading
You can define multiple constructors with different parameter lists. This is known as constructor overloading. It allows you to create objects in various ways, providing flexibility in object initialization.
class Circle {
public:
Circle() {
radius = 1.0;
}
Circle(double r) {
radius = r;
}
private:
double radius;
};
4. Initialization List
Constructors often use an initialization list to set attributes directly during object creation. Initialization lists are more efficient and recommended for initializing attributes, especially for non-primitive types and constant attributes.
class Student {
public:
Student(std::string n, int r) : name(n), rollNumber(r) {
// Attributes are initialized in the initialization list
}
private:
std::string name;
const int rollNumber;
};
5. Copy Constructor
A copy constructor is a special constructor that initializes a new object with the attributes of an existing object. It is called when an object is created as a copy of another object. If you don’t define a copy constructor, the compiler generates a default one.
class Point {
public:
Point(int xVal, int yVal) : x(xVal), y(yVal) {}
// Copy constructor
Point(const Point &other) : x(other.x), y(other.y) {}
private:
int x;
int y;
};
6. Delegating Constructors (C++11)
Delegating constructors allow one constructor of a class to call another constructor of the same class. This helps avoid code duplication when multiple constructors share common initialization logic.
class MyClass {
public:
MyClass() : MyClass(0) {
// Delegating constructor
}
MyClass(int x) : value(x) {
// Actual constructor
}
private:
int value;
};
Constructors play a vital role in the object creation process. They ensure that objects are properly initialized and in a valid state. By defining constructors that match your design requirements, you make your classes more usable, maintainable, and reliable.