Skip to main content

Constructors and Destructors in C++

 


Constructors and Destructors in C++

Constructors are special class functions which performs initialization of every object. The Compiler calls the Constructor whenever an object is created. Constructors initialize values to object members after storage is allocated to the object.

Whereas, Destructor on the other hand is used to destroy the class object.

Following is the syntax of defining a constructor function in a class:

class A
{
 public:
    int x;

    // constructor
    A()

    {
        // object initialization

    }
};

 

While defining a constructor you must remember that the name of constructor will be same as the name of the class, and  constructor will never have a return type.

Constructors can be defined either inside the class definition or outside class definition using class name and scope resolution :: operator.

class A
{
    public:
    int i;
    A(); // constructor declared
};
 
// constructor definition
A::A()  
{
    i = 1;
}

Types of Constructors in C++

Constructors are of three types:

1.   Default Constructor

2.   Parametrized Constructor

3.   Copy Constructor


Default Constructors

Default constructor is the constructor which doesn't take any argument. It has no parameter.

Syntax:

class_name(parameter1, parameter2, ...)
{
    // constructor Definition
}

For example:

class Cube
{
    public:
    int side;
    Cube()
    {
        side = 10;
    }
};
 
int main()
{
    Cube c;
    cout << c.side;
}

 10


A default constructor is so important for initialization of object members, that even if we do not define a constructor explicitly, the compiler will provide a default constructor implicitly.

class Cube
{
    public:
    int side;
};
 
int main()
{
    Cube c;
    cout << c.side;
}

 0 or any random value

In this case, default constructor provided by the compiler will be called which will initialize the object data members to default value, that will be 0 or any random integer value in this case.


Parameterized Constructors

These are the constructors with parameter. Using this Constructor you can provide different values to data members of different objects, by passing the appropriate values as argument.

For example:

class Cube
{
    public:
    int side;
    Cube(int x)
    {
        side=x;
    }
};
 
int main()
{
    Cube c1(10);
    Cube c2(20);
    Cube c3(30);
    cout << c1.side;
    cout << c2.side;
    cout << c3.side;
}

 

10
20
30

By using parameterized construcor in above case, we have initialized 3 objects with user defined values. We can have any number of parameters in a constructor.


Copy Constructors

These are special type of Constructors which takes an object as argument, and is used to copy values of data members of one object into other object. We will study copy constructors in detail later.


Constructor Overloading in C++

Just like other member functions, constructors can also be overloaded. Infact when you have both default and parameterized constructors defined in your class you are having Overloaded Constructors, one with no parameter and other with parameter.

You can have any number of Constructors in a class that differ in parameter list.

class Student
{
    public:
    int rollno;
    string name;
    // first constructor
    Student(int x)
    {
        rollno = x;
        name = "None";
    }
    // second constructor
    Student(int x, string str)
    {
        rollno = x;
        name = str;
    }
};
 
int main()
{
    // student A initialized with roll no 10 and name None
    Student A(10);
   
    // student B initialized with roll no 11 and name John
    Student B(11, "John");
}

We have defined two constructors with different parameters, hence overloading the constructors.

One more important thing, if you define any constructor explicitly, then the compiler will not provide default constructor and you will have to define it yourself.

In the above case if we write Student S; in main(), it will lead to a compile time error, because we haven't defined default constructor, and compiler will not provide its default constructor because we have defined other parameterized constructors.


Destructors in C++

Destructor is a special class function which destroys the object as soon as the scope of object ends. The destructor is called automatically by the compiler when the object goes out of scope.

The syntax for destructor is same as that for the constructor, the class name is used for the name of destructor, with a tilde ~ sign as prefix to it.

class A
{
    public:
    // defining destructor for class
    ~A()
    {
        // statement
    }  
};

 

Destructors will never have any arguments.


Example to see how Constructor and Destructor are called

Below we have a simple class A with a constructor and destructor. We will create object of the class and see when a constructor is called and when a destructor gets called.

class A
{
    // constructor
    A()
    {
        cout << "Constructor called";
    }
 
    // destructor
    ~A()
    {
        cout << "Destructor called";
    }
};
 
int main()
{
    A obj1;   // Constructor Called
    int x = 1
    if(x)
    {
        A obj2;  // Constructor Called
    }   // Destructor Called for obj2
} //  Destructor called for obj1


Constructor called
Constructor called
Destructor called
Destructor called
 



Comments

Popular posts from this blog

C++ Lec-2 Introduction to Object-Oriented Programming (OOP) in C++

  Introduction to Object-Oriented Programming (OOP) in C++ Object-Oriented Programming (OOP) is a programming paradigm that uses objects and classes to organize software design. It allows for modelling real-world entities and relationships in a program. C++ is an object-oriented programming language that provides features to implement OOP concepts effectively. Key Concepts of OOP   Class and Object o    Class : A blueprint for creating objects. It defines properties and behaviours of objects.   class Car { public:     string brand;     string model;     int year; };   o    Object : An instance of a class.   Car car1; car1.brand = "Mahindra"; car1.model = "THAR"; car1.year = 2024;   Encapsulation o    Encapsulation is the bundling of data and methods that operate on that data within a single unit, or class. It restricts direct acces...

C++ Lec-1 (Introduction to Programming Languages)

  Introduction to Programming Languages ________________________________________________________________________ P rogramming languages are formal languages comprising a set of instructions that produce various kinds of output. They are used in computer programming to implement algorithms and control the behavior of machines. Language Generation Programming languages have evolved through several generations: First Generation (1GL) : Machine language, the most basic level of programming languages, consisting of binary code. Second Generation (2GL) : Assembly language, a low-level language with a strong correspondence to the machine language instructions. Third Generation (3GL) : High-level languages like C, C++, Java, Python, which are more abstract and easier for humans to understand. Fourth Generation (4GL) : Languages closer to human language, often used for database querying and report generation (e.g., ...

Flow Charts - What Is a Flow Chart? When to Use a Flowchart? Flowchart Symbols & Components

  Flow Charts     Flow charts are a useful tool in many situations, as we make a process easy to understand at a glance. Using just a few words and some simple symbols, they show clearly what happens at each stage and how this affects other decisions and actions.   What Is a Flow Chart?     In 1921, the Frank and Lillian presented what was only a "graphic-based method" in a presentation titled: “ Process Charts: First Steps in Finding the One Best Way to do Work ”, to members of the American Society of Mechanical Engineers (ASME). When to Use a Flowchart?     Flowchart is a very simple yet powerful tool to improve productivity in both our personal and work life. Here are some ways flowchart can be helpful:   ·      Document a process ·      Present solution to a problem ·      Brainstorm ideas in a meeting ·      Design an operation system ·    ...