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

27 Dec, 2021
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:
Through Default constructors.Through Parameterized constructors.Through Copy constructors.
Through Default constructors.
Through Parameterized constructors.
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 exampleclass example {    int x; public:    void set(int x)    {        this->x = x;        cout << "The value of x is: "             << x << "\n";    }}; // Driver codeint 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;}
Explanation:In the above code, there are two types of different ways of instantiating an object-
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.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.
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.
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 exampleclass example {    int x; public:    // Parameterized constructor    example(int y)    {        x = y;        cout << "The value of x is: "             << x << "\n";    }}; // Driver codeint 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;}
Explanation:In the above code, there are three ways of instantiating an object-
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.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.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.
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.
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.
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 classclass 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 codeint 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;}
 Explanation:In the above code, there are three ways of instantiating an object using a copy constructor-
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.Method 2:example obj3(obj1): This line is invoking copy constructor and creates a new object obj3 that is a copy of object obj1.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.
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.
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.
Method 2:example obj3(obj1): This line is invoking copy constructor and creates a new object obj3 that is a copy of object obj1.
example obj3(obj1): This line is invoking copy constructor and creates a new object obj3 that is a copy of object obj1.
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.
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.
 
sweetyty
C++-Class and Object
C++
CPP
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Iterators in C++ STL
Operator Overloading in C++
Friend class and function in C++
Polymorphism in C++
Sorting a vector in C++
Inline Functions in C++
Convert string to char array in C++
List in C++ Standard Template Library (STL)
std::string class in C++
Exception Handling in C++