0% found this document useful (0 votes)
7 views5 pages

Understanding C++ Constructors and Destructors

The document explains the concepts of constructors and destructors in C++. A constructor initializes objects upon creation, while a destructor is responsible for destroying those objects when they are no longer needed. It details the types of constructors, including default, parameterized, and copy constructors, and provides examples of their usage in C++ programming.

Uploaded by

jojiba2617
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views5 pages

Understanding C++ Constructors and Destructors

The document explains the concepts of constructors and destructors in C++. A constructor initializes objects upon creation, while a destructor is responsible for destroying those objects when they are no longer needed. It details the types of constructors, including default, parameterized, and copy constructors, and provides examples of their usage in C++ programming.

Uploaded by

jojiba2617
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

What Is Destructor in C++?

As the name implies, the destructor destroys objects that the Constructor has created within a C++ program. The name of the
destructor is the same as the name of the class, except that it has a tilde ( ~ ) before its name.

C++ provides a particular member function called the Constructor, which enables an object to
initialize itself at the time of its creation. It is known as the automatic initialization of objects.
This concept of C++ also provides another member function called destructor, which destroys
the objects when they are no longer required. In this tutorial, you will learn how constructors and
destructors work, the types of constructors and how they can be implemented within the C++
program.
Table of Contents

 What Is Constructor in C++?


 Types of Constructor
o Do Nothing Constructor
o Default Constructor
o Parameterized Constructor
o Copy Constructor
 What Is Destructor in C++?

What Is Constructor in C++?


The process of creating and deleting objects is a vital task in C++. Every time an instance of a class is created, the constructor method is called. A constructor is a
class member function used to initialize the objects of the class. It is treated as a special member function because it has the same name as the class name.

The Constructor is called whenever an object of the respective class is created. It is named "constructor" because it constructs the value of the data members of a
class. Initial values ​can be passed as arguments to the constructor function when the object is declared.

This can be done in two ways:

 By calling Constructor explicitly


 By calling Constructor implicitly
The Constructor declaration and definition are as follows:

Syntax:
class ExampleClass { // Class
public: // Access specifier
ExampleClass() { // Constructor
cout << "Sample text.";
}
};

int main() {
ExampleClass myObj; // Create an object of ExampleClass (this will call the constructor)
return 0;
}
Unique characteristics of Constructor:

 A Constructor is declared in the public section.


 The Constructor has no return type, not even void.
 The Constructor is called automatically when the class object is created.
 The Constructor cannot be inherited, although the derived class can call the base class constructor.
 Like other functions, constructors can have default arguments.
 You cannot refer to the address of the Constructor.
 A Constructor cannot be virtual.
Types of Constructor
C++ offers four types of constructors. These are:

1. Do Nothing Constructor
2. Default Constructor
3. Parameterized Constructor
4. Copy Constructor

Do Nothing Constructor
Do nothing Constructor is the type of Constructor that does not contain any statement. It takes no arguments and has no return type.

Default Constructor
The default constructor takes no arguments. It has no arguments, but the programmer can write some initialization statements within.

Syntax:
class_name()
{
// Constructor definition
}
Example:
#include <iostream>
using namespace std;

class Calc {
int val;

public:
Calc()
{
val = 20;
}
};
int main()
{
Calc c1;
cout << [Link];
}
A default constructor is critical to initialize object members, so the compiler automatically provides a default constructor even if we don't explicitly define the
Constructor.

A default constructor has no parameters, but programmers can add and use parameters within a constructor if required. It helps programmers assign initial values
to an object at the creation time.

Example:
#include <iostream>
using namespace std;

class Calc
{
int val2;
public:
Calc(int x)
{
val2=x;
}
};

int main()
{
Calc c1(10);
Calc c2(20);
Calc c3(30);
cout << c1.val2;
cout << c2.val2;
cout << c3.val2;
}

Copy Constructor
C++ provides a particular type of Constructor that takes an object as an argument and is used to copy the values of data members of one object to another. In this
case, the copy constructor is used to declare and initialize an object from another object.

Example:
Calc C2(C1);
Or
Calc C2 = C1;
The process of initializing through a copy constructor is called copy initialization.

Syntax:
class-name (class-name &)
{
...
}
Example:
#include <iostream>
using namespace std;

class CopyCon {
int a, b;

public:
CopyCon(int x, int y)
{
a = x;
b = y;
cout << "\nHere is the initialization of Constructor";
}
void Display()
{
cout << "\nValues : \t" << a << "\t" << b;
}
};

void main()
{
CopyCon Object(30, 40);
//Copy Constructor
CopyCon Object2 = Object;
[Link]();
[Link]();
}

What Is Destructor in C++?


ame implies, the destructor destroys objects that the Constructor has created within a C++
As the n
program. The name of the destructor is the same as the name of the class, except that it has a
tilde (~) before its name. It is a good practice to declare the destructor after the Constructor has
finished using it.

Here's the basic declaration procedure of a destructor:

Syntax:

~ExampleClass()
{

}
The destructor neither takes an argument nor returns any value, and the compiler implicitly
invokes upon the exit from the program to clean up storage that is no longer accessible.
C++ Destructor
A destructor works opposite to constructor; it destructs the objects of classes. It can be defined only once in a class. Like constructors, it is invoked automatically.

A destructor is defined like constructor. It must have same name as class. But it is prefixed with a tilde sign (~).

Note: C++ destructor cannot have parameters. Moreover, modifiers can't be applied on destructors.

C++ Constructor and Destructor Example

Let's see an example of constructor and destructor in C++ which is called automatically.

1. #include <iostream>
2. using namespace std;
3. class Employee
4. {
5. public:
6. Employee()
7. {
8. cout<<"Constructor Invoked"<<endl;
9. }
10. ~Employee()
11. {
12. cout<<"Destructor Invoked"<<endl;
13. }
14. };
15. int main(void)
16. {
17. Employee e1; //creating an object of Employee
18. Employee e2; //creating an object of Employee
19. return 0;
20. }

You might also like