Constructors
Constructors in C++
Constructors are special member functions in C++ that are automatically invoked when an object of a class is created. They are primarily used to initialize* the data members of the class.
1. Introduction to Constructors
Purpose:
-
Initialize objects with valid state.
-
Allocate resources (memory, files, etc.).
-
Enforce invariants.
-
Provide default values.
- *Key Characteristics: * *
-
Same name as the class.
-
No return type (not even
void). -
Can be overloaded.
-
Automatically called upon object creation.
-
Can be defined inside or outside the class.
- *Types of Constructors: * *
-
Default Constructor
-
Parameterized Constructor
-
Copy Constructor
-
Move Constructor (C++11)
-
Conversion Constructor