Constructors and Destructors in C++
Use of Constructors and Destructors Explained in C++
In this article you will learn about use of constructors and destructors in C++. You will also get to know about default and Parameterized Constructors.
Constructors
Constructor is a kind of function used to initialize the data members of a class while defining an object. When we define an object of the class then the data members assigned to that object need to be initialized. For this purpose constructors are used.
A constructor is just like a function having following characteristics:
- It cannot return a value
- It has same name as class name
For instance the following code shows a constructor having name Abc() at line 10 being used to initialize the data members and does not return any value.
If we execute the above code then it will display the following result. The values displayed as output are assigned by the constructor.
It is not necessary to define the constructor within the body of the class. We can declare the constructor within the class and define outside the class as below.
Default Constructors
In the above code Abc() constructor is defined at line 10 having same name as class name. It is used to initialize the data members declared in the class. This type of constructor is called default or no-argument constructor. Default constructor does not accept any argument and is used to assign the same values to the data members when called. In the above code, the default constructor is called at the time of abject creation (line 20).
Default constructor is not necessary to be defined. If we do not define default constructor then the data members are initialized automatically according to their data types. For example if we remove the default constructor from above code or write an empty default constructor then still the data members will be assigned a value.
It is important to note that in this case the default value of data members depends on the version of compiler being used. In some compilers, the default values can be 0 where as other compilers may assign garbage as default values. To avoid this uncertainty it is recommended to use the default constructor as written in the above example to make it sure that always the required values will be assigned to the data members as default values.
Parameterized Constructors
Apart from default constructors, we need to have a constructor that will accept the values from us as parameters and assign to the data members at the time of object creation. This kind of constructor is called parameterized constructor as shown in the code given below.
In the above code, parameterized constructor is defined at line 10 and called at line 20. We can see that while object of the class is created at line 20 then the default values are also given to be stored in the data members at the same time.
At line 20, the three parameters with object x mean to call a constructor defined in class Abc which can accept three integer parameters. These parameters are then assigned to the data members by the parameterized constructor as shown at lines 12, 13 and 14.
Constructors Overloading
If we define more than one constructor in a class with different signatures then this is called constructors overloading. For example we can define both default and parameterized constructors in the same class as shown in the following code. We can also define more than one parameterized constructors in the same class but each of them must have different number of parameters or different types of parameters.
If we define more than one constructors in the same class then the number and type of parameters at the time of object creation decides that which constructor will be called for data members initialization. For example in the above example, object is created at line 26 calling a constructor which can accept three integer parameters. If the code in main function is as below, then it will call the default constructor.
Abc x;
Destructors
When an object of a class is defined, then it occupies some memory. Destructors are used to release the memory being used by an object. They come into play when the object is no more required so the memory being occupied should be made available for some other useful tasks. It is important to note that destructors are called automatically and every class can have only one destructor. Destructor is defined same as constructor but prefixed by tilde ‘~’ sign as shown at line22 in the following example.
More Related Articles For You
- What is C++
- C++ and Object Oriented Programming OOPS concepts
- Syntax and Structure of C++ program
- Data Types in C++
- C++ Variables
- Types of operators in C++
- Decision making in C++
- C++ Loop Types
- Storage Classes in C++
- Functions in C++
- Classes and Objects in C++
- Access controls in C++ Classes
- Defining Class and Object in C++
- Accessing Public and Private Data Members in C++
- Member Functions in Classes
- Types of Member Functions in C++
- Inline Functions in C++
- Namespaces in C++
- Function Overloading in Classes in C++
- Static Keywords in C++
- Const Keyword in C++ Programming
- References in C++
- Copy Constructor in C++
- Pointer to Members in C++ Classes
- Introduction to Inheritance
- Types of Inheritance
- Order of Constructor Call in Inheritance
- Upcasting in C++
- Function Overriding in C++
- Virtual Functions in C++
- Abstract Class and Pure Virtual Functions in C++
- Virtual Destructors in C++
- Operator Overloading in C++
C++ Tutorial
C++ Quizzes