Constructor and Destructor in C++ With Easy Example

0
530
Constructor and Destructor in c++

We are starting a fresh series of C++ Programs for Beginners on Programmingy. we will upload well-explained articles for C++ Programs with Examples to make codes easy. Our target is simple, write codes with easy to learn logic. Here in this article, we are going to cover up two main features of any Object Oriented Programming Language that are Constructor and Destructor in C++. Without any further dues, let’s just start.

Constructor and Destructor in C++ Explanation

Let’s make things easy first. A constructor is that method(Functions that you make in a class are called Methods), that will be executed as soon as the object of that class is being created and Very simply, Destructor is a method that is executed just before an object is destroyed. You can see the C++ example below to understand more about Constructors and Destructors in C++ below.

Constructor: the constructor is a member function that is executed automatically whenever an object is created. A constructor is generally used to initialize values of an object.

The constructor has the same name as of the class. It does not have any return type. A class can have multiple constructors, which is called constructor overloading. Constructors are generally used to initialize the class data members.

Destructor: It is similar to the constructor, every class can have single destructor with the same name of the class but preceded by a tilde sign(~).

Destructors cannot be overloaded in the class. Generally, destructors are used to release any memory or closing any open file. A destructor is called when the object goes out of scope. The destructor does not have any return type and no arguments can be passed to the destructor.

 

Constructor and Destructor in C++ With Easy Example

Let us take an example to illustrate the working and initialization of the constructor:

Class part

{

Public:

int model_no,part_no;

part() //constructor of the class part

{

Cout<<”enter model and part no:”;

Cin>>model_no>>part_no;

}

part(int m,int p) //constructor

{

Model_no=m;

Part_no=p;

}

Void display()

{

Cout<<model_no<<part_no;

}

};

Void main()

{

Part ob;

Part obj(20,5);

Ob.display();

Obj.display();

}

In this Constructor and Destructor in C++ program we have created a class named ‘part’. The data members ’model_no’ and ’part_no’ are declared as public members. In this program we have made two constructors named ‘part’ as the constructor should have the same name as that of class. We are also showing constructor overloading here as more than one constructor is used in this program.

Now coming at to the main we have created an object named ‘ob’ and ‘obj’, the obj is parameterized with some values. The first ‘ob.display()’ will call the first constructor and will ask the user to enter the model and part number. So the first output will be according to the data entered by the user. The next ‘ob.display()’ will call the second constructor and pass the value 20 and 5 in this constructor and will only show 20 as model number and 5 as the part number in this case.

Ob obj

M 20

P 5

M 97

P 7

Dummy Constructor in C++

Evolution of c++ from c has brought about many more features that make programming more easy and comfortable. The c++ has a concept of dummy constructor. If we do not provide any constructor the c++ runtime system provides dummy constructor which performs no operation. It takes no argument, whereas if we provide a constructor to class, then the dummy constructor is not provided by the c++ run-time system.

I hope you liked my article of Constructor and Destructor in C++. If you have any doubt regarding any

LEAVE A REPLY

Please enter your comment!
Please enter your name here