Open In App

Different ways to instantiate an object in C++ with Examples

Improve
Improve
Like Article
Like
Save
Share
Report

prerequisite: C++ Classes and Objects

Different Ways to Instantiate an Object

In C++, there are different ways to instantiate an objects and one of the method is using Constructors. These are special class members which are called by the compiler every time an object of that class is instantiated. There are three different ways of instantiating an object through constructors:

  1. Through Default constructors.
  2. Through Parameterized constructors.
  3. Through Copy constructors.

1. Through Default Constructor: An object can be instantiated through a default constructor in either static way or dynamic way. 

Syntax:

Automatic Storage Duration/ Static initialization 
classname objectname; 
Dynamic Storage Duration/ Dynamic Initialization 
classname *objectname = new classname(); 
Delete Dynamic Object 
delete objectname;

Below is the C++ program to demonstrate the object instantiation through default constructor-

C++14




// C++ program to implement
// the above approach
#include <bits/stdc++.h>
using namespace std;
 
// Defining class example
class example {
    int x;
 
public:
    void set(int x)
    {
        this->x = x;
        cout << "The value of x is: "
             << x << "\n";
    }
};
 
// Driver code
int main()
{
    // Creating automatic storage object
    example obj1;
    obj1.set(5);
 
    // Creating dynamic storage object
    example* obj2 = new example();
    obj2->set(10);
 
    // Explicitly deleting the obj2
    delete obj2;
 
    return 0;
}


OutputThe value of x is: 5 
The value of x is: 10

Explanation:
In the above code, there are two types of different ways of instantiating an object-

  1. example obj1: This line is instantiating an object that has automatic storage duration. This object will be deleted automatically when it will be out of scope.
  2. example *obj2 = new example(): This is the way of instantiating an object that has dynamic storage duration. This object will not be deleted automatically. The user will have to use an explicit delete statement for deleting the object.

delete obj2;

2. Through Parameterized Constructor:  Object instantiation can be done through parameterized constructor in static and dynamic way. It is possible to pass arguments to parameterized constructors. Typically, these arguments help initialize an object when it is created. 

Syntax: 

Static Initialization 
1. classname objectname(parameter); 
2. classname objectname = classname(parameter); 
Dynamic Initialization 
classname *objectname = new classname(parameter); 
Delete Dynamic Object 
delete objectname;

Below is the C++ program to demonstrate the parameterized constructor-

C++




// C++ program to implement
// the above approach
#include <bits/stdc++.h>
using namespace std;
 
// Defining the class example
class example {
    int x;
 
public:
    // Parameterized constructor
    example(int y)
    {
        x = y;
        cout << "The value of x is: "
             << x << "\n";
    }
};
 
// Driver code
int main()
{
    // Creating object with automatic
    // storage duration using Method 1
    example obj1(10);
 
    // Creating object with automatic
    // storage duration using Method 2
    example obj2 = example(8);
 
    // Creating object with dynamic
    // storage duration using Method 3
    example* obj3 = new example(20);
 
    delete obj3;
    return 0;
}


OutputThe value of x is: 10 
The value of x is: 8 
The value of x is: 20

Explanation:
In the above code, there are three ways of instantiating an object-

  1. example obj1(10): This line is instantiating an object using the parameterized constructor that has automatic storage duration. This object will be deleted automatically when it will be out of scope.
  2. example obj2 = example(8): This line is instantiating an object using the parameterized constructor that has automatic storage duration. This object will be deleted automatically when it will be out of scope.
  3. example *obj3 = new example(20): This is the way of instantiating an object using the parameterized constructor that has dynamic storage duration. This object will not be deleted automatically. The user will have to use an explicit delete statement for deleting the object.

3. Copy constructor: A copy constructor can be used to instantiate an object statically or dynamically. Static or dynamic initialization of object using a copy constructor has the following general function prototype: 

Syntax: 

Copy Constructor 
ClassName (const ClassName &old_obj); 
Instantiate an object using copy constructor 
Static Initialization 
1. classname obj1; 
classname obj2 = obj1; 
2. classname obj1; 
classname obj2(obj1); 
Dynamic Initialization 
1. classname *obj1 = new classname(); 
classname *obj2 = new classname(*obj1); 

Below is the C++ program to demonstrate the copy constructor- 

C++




// C++ program to implement
// the above approach
#include <bits/stdc++.h>
using namespace std;
 
// Defining the class
class example {
    int x;
 
public:
    // Parameterized constructor
    example(int y)
    {
        x = y;
        cout << "The value of x is: "
             << x << "\n";
    }
 
    // Copy constructor
    example(example& obj)
    {
        x = obj.x;
        cout << "The value of x in "
             << "copy constructor: "
             << x << "\n";
    };
};
 
// Driver code
int main()
{
    // Instantiating copy constructor
    // using Method 1
    example obj1(4);
    example obj2 = obj1;
 
    // Instantiating copy constructor
    // using Method 2
    example obj3(obj1);
 
    // Instantiating copy constructor
    // using Method 3
    example* obj4 = new example(10);
    example* obj5 = new example(*obj4);
 
    delete obj5;
    delete obj4;
    return 0;
}


OutputThe value of x is: 4 
The value of x in copy constructor: 4 
The value of x in copy constructor: 4 
The value of x is: 10 
The value of x in copy constructor: 10

 Explanation:
In the above code, there are three ways of instantiating an object using a copy constructor-

  1. Method 1:
    • example obj1(4): This line is instantiating an object that has automatic storage duration.
    • example obj2 = obj1: This line is invoking copy constructor and creates a new object obj2 that is a copy of object obj1.
  2. Method 2:
    • example obj3(obj1): This line is invoking copy constructor and creates a new object obj3 that is a copy of object obj1.
  3. Method 3:
    • example *obj4 = new example(10): This is the way of instantiating an object that has dynamic storage duration. This object will not be deleted automatically. The user will have to use an explicit delete statement for deleting the object.
    • example *obj5 = new example(*obj4): This line is invoking a copy constructor and creates a new object obj5 that is a copy of object obj4. The user will have to use an explicit delete statement for deleting the object.

 



Last Updated : 27 Dec, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads