Skip to main content

C++ Constructors and Destructors

C++ Constructors and Destructors

Providing the initial value as described in the earlier chapters of C++ does not conform to the philosophy of C++. So C++ provides a special member function called the constructor which enables an object to initialize itself at the time of its creation. This is known as automatic initialization of objects. This concept of C++ also provides another member function called destructor which is used to destroy the objects when they are no longer required. In this chapter, you will learn about how constructors and destructors work, types of constructors and how they can be implemented within C++ program.

What are constructors?

The process of creating and deleting objects in C++ is vital task. Each time an instance of a class is created the constructor method is called. Constructors is a special member functions of class and it is used to initialize the objects of its class. It is treated as a special member function because its name is the same as the class name. These constructors get invoked whenever an object of its associated class is created. It is named as “constructor” because it constructs the value of data member 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 declaration and definition of constructor is as follows

syntax:
class class_name
{
int g, h;
public:
class_name
(void); // Constructor Declared
. . .
};
class_name
:: class_name()
{
g
=1; h=2; // Constructor defined
}
Special characteristics of Constructors:
  • They should be declared in the public section
  • They do not have any return type, not even void
  • They get automatically invoked when the objects are created
  • They cannot be inherited though derived class can call the base class constructor
  • Like other functions, they can have default arguments
  • You cannot refer to their address
  • Constructors cannot be virtual

Types of Constructors

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 constructors are that type of constructor which does not contain any statements. Do nothing constructor is the one which has no argument in it and no return type.

Default Constructor

Default constructor is the constructor which doesn’t take any argument. It has no parameter but programmer can write some initialization statement there.
Syntax:
class_name()
{
// Constructor Definition ;
}

//Code Snippet:
#include <iostream>
using namespace std;

class Calc {
int val;

public:
Calc()
{
val
= 20;
}
};
int main()
{
Calc c1;
cout
<< c1.val;
}
A default constructor is very important for initializing object members, that even if we do not define a constructor explicitly, the compiler automatically provides a default constructor implicitly.

Parameterized Constructor

A default constructor does not have any parameter, but programmers can add and use parameters within a constructor if required. This helps programmers to assign initial values to an object at the time of creation.
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 special type of constructor which takes an object as argument, and is used to copy values of data members of one object into other object. In this case, copy constructors are used to declare and initialize object from another object.
Example:
Calc C2(C1);
Or
Calc C2 = C1;
The process of initializing through a copy constructor is called the 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;
Object.Display();
Object2.Display();
}

What are Destructors?

As the name implies, destructors are used to destroy the objects that have been created by the constructor within the C++ program. Destructor names are same as the class name but they are preceded by a tilde (~). It is a good practice to declare the destructor after the end of using constructor. Here’s the basic declaration procedure of a destructor:
~Cube()
{

}
The destructor neither takes a argument nor returns any value and the compiler implicitly invokes upon the exit from the program for cleaning up storage that is no longer accessible.

Comments

For Programs Click Here

Popular posts from this blog

Syllabus

Syllabus  C Programming Tutorials C Tutorial C Introduction History of C Programming Language C Installation C Program Structure C Input and Output (I/O) C Format Specifiers Declaration & Assignments C Tokens C Identifiers C Keywords C Constants C Operators C Data Types C Variables C Preprocessors C Type Casting C Custom Header File Flow Control C Decision Making C if Statements C if-else Statements C Nested if-else Statements C else-if Statements C goto Statement C switch Statements C Loops C while loops C do while loops C for loops Functions C Functions C Function Arguments C Library Functions C Variable Scope Arrays & Strings C Arrays C Strings Pointers C Pointers C Dynamic Memory Allocation Structure & Union C Structures C Unions File I/O C File Handling C fopen C fclose C getc C putc C getw C putw C fprintf C fscanf C fgets C fputs C feof                                     ...

Python Tutorial

               Python Tutorials             Python Tutorial Python is a general purpose object-oriented programming language with high-level programming capabilities. This Python tutorial series will help you to get started in Python programming language from scratch. Prerequisites To learn Python Programming Language you haven’t required any previous programming knowledge, but the basic understanding of any other programming languages will help you to understanding the Python Programming Concepts quickly. Python Programming Example A quick look at the example of  Hello, World!  in Python programming, and detailed description is given in the  Python Basics  page. Example: #!/usr/bin/python print "Hello, World!" Output: Hello, World! The above example has been used to print  Hello, World!  text on the screen.