Constructors and Destructors in C++ Classes
On this page (21sections)
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++
Related Pages
Continue learning with these related tutorials and programs:
- C++ Tutorials — Browse all C++ Tutorials.
- Introduction of Classes (OOP) — Tutorial — classes and object-oriented programming.
- Constructor Example in C++ — Program — constructors and destructors.
- OOPS Overview and Concepts — More in classes in c.