C++ Program to Show the Use of Constructor

Constructors are special member functions in C++ that are automatically called when an object of a class is instantiated. They are primarily used to initialize objects. This article will guide you through the concept and implementation of constructors in C++ with detailed explanations and examples.

Prerequisites

Before diving into the implementation, ensure you have a basic understanding of the following concepts:

  1. Classes and Objects: Understanding of how classes and objects work in C++.
  2. Member Functions: Familiarity with member functions and their purpose.

1. Introduction to Constructors

1.1 Definition

A constructor is a special member function that has the same name as the class. It is automatically invoked when an object of the class is created. Constructors do not have a return type, not even void.

1.2 Purpose

Constructors are used for:

  1. Initializing objects.
  2. Allocating resources like memory, file handles, or network connections that the object might need.

2. Types of Constructors

  1. Default Constructor: A constructor that takes no arguments.
  2. Parameterized Constructor: A constructor that takes arguments to initialize an object with specific values.
  3. Copy Constructor: A constructor that creates a new object as a copy of an existing object.

3. Implementation of Constructors in C++

3.1 Example 1: Default Constructor

Let’s start with a basic example to demonstrate the use of a default constructor in C++.

C++
#include <iostream>
using namespace std;

class MyClass {
public:
    MyClass() {
        cout << "Default constructor called." << endl;
    }
};

int main() {
    MyClass obj;
    return 0;
}

Output:

C++
Default constructor called.

Explanation:

  • The default constructor is called when the object obj is created.

3.2 Example 2: Parameterized Constructor

In this example, we will use a parameterized constructor to initialize an object with specific values.

C++
#include <iostream>
using namespace std;

class MyClass {
private:
    int x;
public:
    MyClass(int a) : x(a) {
        cout << "Parameterized constructor called with value: " << x << endl;
    }
};

int main() {
    MyClass obj(42);
    return 0;
}

Output:

C++
Parameterized constructor called with value: 42

Explanation:

  • The parameterized constructor is called with the value 42 to initialize the member variable x.

3.3 Example 3: Copy Constructor

This example demonstrates the use of a copy constructor to create a new object as a copy of an existing object.

C++
#include <iostream>
using namespace std;

class MyClass {
private:
    int x;
public:
    MyClass(int a) : x(a) {
        cout << "Parameterized constructor called with value: " << x << endl;
    }

    MyClass(const MyClass &obj) : x(obj.x) {
        cout << "Copy constructor called with value: " << x << endl;
    }
};

int main() {
    MyClass obj1(42);
    MyClass obj2(obj1); // Copy constructor is called
    return 0;
}

Output:

C++
Parameterized constructor called with value: 42
Copy constructor called with value: 42

Explanation:

  • The parameterized constructor initializes obj1 with the value 42.
  • The copy constructor creates obj2 as a copy of obj1.

3.4 Example 4: Constructor with Dynamic Memory Allocation

In this example, we use a constructor to allocate dynamic memory.

C++
#include <iostream>
using namespace std;

class MyClass {
private:
    int* data;
public:
    MyClass(int size) {
        data = new int[size];
        cout << "Constructor: Memory allocated for array of size " << size << endl;
    }

    ~MyClass() {
        delete[] data;
        cout << "Destructor: Memory deallocated." << endl;
    }
};

int main() {
    MyClass obj(10);
    return 0;
}

Output:

C++
Constructor: Memory allocated for array of size 10
Destructor: Memory deallocated.

Explanation:

  • The constructor allocates memory for an array of integers.
  • The destructor deallocates the memory when the object obj is destroyed, ensuring there are no memory leaks.

Conclusion

Constructors are essential for initializing objects and allocating resources in C++. They ensure that objects are in a valid state when created. This article provided a comprehensive guide to the use of constructors in C++, demonstrating their application through examples of default constructors, parameterized constructors, copy constructors, and constructors with dynamic memory allocation.