Constructor in C++
1. Introduction
A constructor is a special member function of a class that is automatically called when an object is created.
The main purpose of a constructor is to initialize the data members of an object.
Example
#include <iostream> using namespace std; class Student { public: string name; Student() { name = "Anu"; } }; int main() { Student s1; cout << s1.name; return 0; }
Output
Anu
Here, Student() is the constructor. It is automatically called when s1 is created.
2. Characteristics of a Constructor
A constructor has the following characteristics:
- Its name must be the same as the class name.
- It does not have a return type.
- It is automatically called when an object is created.
- It is mainly used to initialize objects.
- A class can have multiple constructors using constructor overloading.
- Constructors can have parameters.
- Constructors can have default arguments.
- A constructor cannot be inherited in the usual sense as a member function.
- Constructors are called once for each object when that object is created.
3. Syntax of a Constructor
class ClassName { public: ClassName() { // initialization } };
Example
class Student { public: Student() { cout << "Constructor called"; } };
4. Simple Constructor Example
#include <iostream> using namespace std; class Student { public: Student() { cout << "Constructor is called"; } }; int main() { Student s1; return 0; }
Output
Constructor is called
The constructor is called automatically when s1 is created.
5. Types of Constructors
Common types of constructors in C++ are:
- Default Constructor
- Parameterized Constructor
- Copy Constructor
6. Default Constructor
A constructor that takes no arguments is called a default constructor.
Example
#include <iostream> using namespace std; class Student { public: Student() { cout << "Default constructor called"; } }; int main() { Student s1; return 0; }
Output
Default constructor called
7. Default Constructor with Initialization
A default constructor can initialize data members.
#include <iostream> using namespace std; class Student { public: string name; int age; Student() { name = "Anu"; age = 22; } }; int main() { Student s1; cout << "Name: " << s1.name << endl; cout << "Age: " << s1.age << endl; return 0; }
Output
Name: Anu Age: 22
8. Parameterized Constructor
A constructor that accepts one or more arguments is called a parameterized constructor.
Example
#include <iostream> using namespace std; class Student { public: string name; int age; Student(string n, int a) { name = n; age = a; } }; int main() { Student s1("Anu", 22); cout << "Name: " << s1.name << endl; cout << "Age: " << s1.age << endl; return 0; }
Output
Name: Anu Age: 22
Here:
Student s1("Anu", 22);
passes "Anu" and 22 to the constructor.
9. Multiple Objects with Parameterized Constructor
Different objects can have different values.
#include <iostream> using namespace std; class Student { public: string name; int age; Student(string n, int a) { name = n; age = a; } void display() { cout << "Name: " << name << endl; cout << "Age: " << age << endl; } }; int main() { Student s1("Anu", 22); Student s2("Ram", 21); s1.display(); cout << endl; s2.display(); return 0; }
Output
Name: Anu Age: 22 Name: Ram Age: 21
10. Copy Constructor
A copy constructor creates a new object by copying the data of an existing object.
Syntax
ClassName(const ClassName &object) { // copy data }
Example
#include <iostream> using namespace std; class Student { public: string name; int age; Student(string n, int a) { name = n; age = a; } Student(const Student &s) { name = s.name; age = s.age; } }; int main() { Student s1("Anu", 22); Student s2 = s1; cout << "Name: " << s2.name << endl; cout << "Age: " << s2.age << endl; return 0; }
Output
Name: Anu Age: 22
Here, s2 is created by copying s1.
11. Constructor Overloading
When a class has multiple constructors with different parameter lists, it is called constructor overloading.
Example
#include <iostream> using namespace std; class Student { public: string name; int age; Student() { name = "Unknown"; age = 0; } Student(string n) { name = n; age = 0; } Student(string n, int a) { name = n; age = a; } void display() { cout << "Name: " << name << endl; cout << "Age: " << age << endl; } }; int main() { Student s1; Student s2("Anu"); Student s3("Ram", 21); s1.display(); cout << endl; s2.display(); cout << endl; s3.display(); return 0; }
Output
Name: Unknown Age: 0 Name: Anu Age: 0 Name: Ram Age: 21
12. Constructor with this Pointer
The this pointer refers to the current object.
It is useful when constructor parameter names are the same as data member names.
Example
#include <iostream> using namespace std; class Student { private: string name; int age; public: Student(string name, int age) { this->name = name; this->age = age; } void display() { cout << "Name: " << name << endl; cout << "Age: " << age << endl; } }; int main() { Student s1("Anu", 22); s1.display(); return 0; }
Output
Name: Anu Age: 22
13. Constructor with Default Arguments
A constructor can have default values for its parameters.
#include <iostream> using namespace std; class Student { public: string name; int age; Student(string n = "Unknown", int a = 0) { name = n; age = a; } void display() { cout << "Name: " << name << endl; cout << "Age: " << age << endl; } }; int main() { Student s1; Student s2("Anu", 22); s1.display(); cout << endl; s2.display(); return 0; }
Output
Name: Unknown Age: 0 Name: Anu Age: 22
14. Constructor with Multiple Data Members
A constructor can initialize multiple data members.
#include <iostream> using namespace std; class Employee { public: string name; int id; float salary; Employee(string n, int i, float s) { name = n; id = i; salary = s; } void display() { cout << "Name: " << name << endl; cout << "ID: " << id << endl; cout << "Salary: " << salary << endl; } }; int main() { Employee e1("Anu", 101, 35000); e1.display(); return 0; }
Output
Name: Anu ID: 101 Salary: 35000
15. Constructor Initialization List
C++ provides an initializer list for initializing data members.
Syntax
ClassName(parameters) : member1(value1), member2(value2) { }
Example
#include <iostream> using namespace std; class Student { private: string name; int age; public: Student(string n, int a) : name(n), age(a) { } void display() { cout << "Name: " << name << endl; cout << "Age: " << age << endl; } }; int main() { Student s1("Anu", 22); s1.display(); return 0; }
Output
Name: Anu Age: 22
Initializer lists are especially important for initializing const members, reference members, and base classes.
16. Constructor and Object Creation
Consider:
Student s1("Anu", 22);
The following happens:
Object is created ↓ Constructor is automatically called ↓ "Anu" and 22 are passed ↓ Data members are initialized
17. Constructor vs Normal Function
| Constructor | Normal Function |
|---|---|
| Same name as class | Can have any valid name |
| No return type | May have a return type |
| Automatically called during object construction | Usually called explicitly |
| Used mainly for initialization | Used to perform operations |
| Called when an object is created | Called when invoked |
| Can be overloaded | Can also be overloaded |
18. Constructor vs Destructor
| Constructor | Destructor |
|---|---|
| Initializes an object | Performs cleanup when an object is destroyed |
| Name is same as class | Name is class name preceded by ~ |
| Called when object is created | Called when object is destroyed |
| Can have parameters | Cannot have parameters |
| Can be overloaded | Cannot be overloaded |
Example: Student() | Example: ~Student() |
19. Important Rules of Constructors
Rule 1: Same name as class
class Student { public: Student() { } };
Rule 2: No return type
Correct:
Student() { }
Incorrect:
void Student() { }
The second one is a normal function, not a constructor.
Rule 3: Automatically called
Student s1;
The constructor is automatically called.
Rule 4: Can be overloaded
Student(); Student(string name); Student(string name, int age);
20. Real-Life Example
Consider a bank account.
When an account object is created, we may want to initialize:
- Account holder name
- Account number
- Balance
A constructor can do this automatically.
#include <iostream> using namespace std; class BankAccount { public: string name; int accountNumber; double balance; BankAccount(string n, int a, double b) { name = n; accountNumber = a; balance = b; } void display() { cout << "Account Holder: " << name << endl; cout << "Account Number: " << accountNumber << endl; cout << "Balance: " << balance << endl; } }; int main() { BankAccount account( "Anu", 1001, 50000 ); account.display(); return 0; }
Output
Account Holder: Anu Account Number: 1001 Balance: 50000
21. Complete Constructor Example
#include <iostream> using namespace std; class Student { private: string name; int age; float marks; public: // Default constructor Student() { name = "Unknown"; age = 0; marks = 0; } // Parameterized constructor Student(string n, int a, float m) { name = n; age = a; marks = m; } void display() { cout << "Name: " << name << endl; cout << "Age: " << age << endl; cout << "Marks: " << marks << endl; } }; int main() { Student s1; Student s2("Anu", 22, 85.5); cout << "Student 1:" << endl; s1.display(); cout << "\nStudent 2:" << endl; s2.display(); return 0; }
Output
Student 1: Name: Unknown Age: 0 Marks: 0 Student 2: Name: Anu Age: 22 Marks: 85.5
22. Advantages of Constructors
- Automatically initializes objects.
- Reduces repetitive initialization code.
- Makes programs easier to understand.
- Helps ensure objects start in a valid state.
- Supports constructor overloading.
- Can initialize data members using initializer lists.
- Makes object creation convenient.
23. Key Points to Remember
-
A constructor is a special member function.
-
It has the same name as the class.
-
It has no return type.
-
It is automatically called when an object is created.
-
It is mainly used for initialization.
-
Common types include:
- Default constructor
- Parameterized constructor
- Copy constructor
-
Constructors can be overloaded.
-
Constructors can use initializer lists.
-
A constructor is not called like an ordinary member function.
Basic Syntax
class Student { public: Student() { // initialization } };
Parameterized Constructor
class Student { public: Student(string name, int age) { // initialization } };
Creating Object
Student s1;
or:
Student s1("Anu", 22);