Constructors and Destructors in C++ Classes
Constructor Overview
Definition
A constructor is a special member function of the class which has the same name as that of the class. It is automatically invoked when we declare/create new objects of the class.
Rules of Constructors
- A constructor should have the same name as that of the class.
- It should be a public member.
- A constructor does not have any return values.
- A constructor can be overloaded.
Syntax
class class_name {
public:
class_name() {
// Constructor code
}
//... other Variables & Functions
}
Types Of Constructor
- Default Constructor
- Parameterized Constructor
- Copy Constructor
Default Constructor
A default constructor does not have any parameters or if it has parameters, all the parameters have default values.
Syntax
class class_name {
public:
// Default constructor with no arguments
class_name();
// Other Members Declaration
}
Simple Contrcuter Example Programs
- Simple Example Program For Constructor In C++
- Simple Program for Constructor Example 2 Using C++ Programming
Parameterized Constructor
If a Constructor has parameters, it is called a Parameterized Constructor. Parameterized Constructors assist in initializing values when an object is created.
General Syntax of Parameterized Constructor
class class_name {
public:
class_name(parameters...) {
// Constructor code
}
//... other Variables & Functions
}
Parameterized Constructor Example Programs
Copy Constructor
A copy constructor is a like a normal parameterized Constructor, but which parameter is the same class object. copy constructor uses to initialize an object using another object of the same class.
General form of Copy Constructor
class class_name {
public:
class_name(class_name & obj) {
// obj is same class another object
// Copy Constructor code
}
//... other Variables & Functions
}
Declaration in Main
class_name object1(params);
Copy Constructor - Method 1
class_name object2(object1);
Copy Constructor - Method 2
class_name object3 = object1;
Copy Constructor Example Programs
- Simple Example Program For Copy Constructor In C++
- Simple Program for Copy Constructor Example 2 Using C++ Programming
Destructor
Definition
A destructor is a special member function which is called automatically when the object goes out of scope.
It is used for,
- Releasing the memory of objects.
- Closing files and resources.
Rules
- Should start with a tilde(`) and must have the same name as that of the class.
- Destructors do not have parameters and return type.
Syntax
Class class_name {
public:
`class_name() //Destructor
{
}
}
Destructor Example Programs
- Simple Example Program For Destructor In C++
- Simple Destructor Scope Measurement Example Program In C++
More Constructor and Destructor Examples
- Simple Example Program For Constructor In C++
- Simple Program for Constructor Example 2 Using C++ Programming
- Simple Example Program For Parameterized Constructor In C++
- Simple Example Program For Constructor Overloading In C++
- Simple Example Program For Copy Constructor In C++
- Simple Program for Copy Constructor Example 2 Using C++ Programming
- Simple Constructor Example Program For Find Prime Number In C++
- Simple Example Program For Destructor In C++
- Simple Destructor Scope Measurement Example Program In C++
Read More Articles
- Simple Merge Sort Program in C++
- Scope Resolution Operator In C++
- Simple Program for Virtual Functions Using C++ Programming
- Simple Class Example Program For Find Prime Number In C++
- Simple Example Program For Parameterized Constructor In C++
- Define Constructor in Outside Class Example Program In C++
- Simple Example Program For Copy Constructor In C++
- Simple Program for Function Overloading Using C++ Programming
- Simple Program for Single Inheritance Using C++ Programming
- Simple Program for Inline Function without Class Using C++ Programming
- Factorial Using Function Example Program In C++
- Simple Example Program For Constructor Overloading In C++
- Simple Example Program For Constructor In C++
- Simple Addition ( Add Two Integers ) Example Program
- Simple Example Program for Inline Function Using C++ Programming
- Simple Program for Read user Input Using cin
- Factorial Using Loop Example Program In C++
- Simple Stack Program in C++ Programming
- Simple Program for Friend Function Using C++ Programming
- Simple Program for Static Data and Member Function Using C++ Programming
- Simple Program for Unary Operator Overloading Using C++ Programming
- Simple Program for Multiple Inheritance Using C++ Programming
- Do While Loop Example Program In C++
- Simple Copy Constructor Example Program For Find Factorial In C++
- Simple Program for Function Template Using C++ Programming