How to create an object in C++

In this discussion, let’s understand different ways of creating objects in C++. First, let’s know a few things about a class and an object.

Every entity, whether living or not, may be represented as an object using C++. Let’s look at an example to better understand classes and objects.

All animals in the animal kingdom have some colour and frequently engage in behaviours including eating, sleeping, and walking. And although the species in this class go by various names, they all exhibit the same behaviour and properties.

A class can be defined as a blueprint with certain characteristics (data members) and behaviours (member functions) shared by all class objects. So, In order to build reusable code, it’s necessary for us to learn how to create classes and objects in C++.

Ways to create objects in C++ :

There are two ways to build objects in C++. The only difference is where they will be stored, or where the memory for those objects will be allocated.

  • Static initialization
  • Dynamic initialization.

Objects are stored in a stack during static initialization and have an automatic storage duration, i.e., the object will be destroyed automatically at the end of the scope, but during dynamic initialization, objects are stored in a heap and we must manually delete the object after use.

In this example, let’s create a class book going into detail about how to create an object.

class book {
    int pages;
public:
    void set(int pages)
    {
        this->pages = pages;
        cout << "No. of pages in the book are "<< pages << "\n";
    }
};

The book class contains data member pages and a member method named set. We will use the two techniques discussed above for this book class to create objects.

Static way: 

class_name object_name;

Dynamic way :

class_name *object_name = new class_name();

delete object_name;

Code : 

#include <bits/stdc++.h>
using namespace std;
class book {
    int pages;
 
public:
    void set(int pages)
    {
        this->pages = pages;
        cout << "No. of pages in the book are "<< pages << "\n";
    }
};


int main()
{
    
    book obj1; //static storage object
    obj1.set(200); //to access the functions in class

    
    book* obj2 = new book(); // dynamic storage object
    obj2->set(400); //to access the functions in class
 
    delete obj2;//  deleting dynamic storage object
 
    return 0;
}

Output :

No. of pages in the book are 200
No. of pages in the book are 400

Since obj1 is created statically, no need to delete it manually. Whereas, obj2 should be deleted explicitly as it was created dynamically. We have used the new keyword and a pointer to dynamically create/instantiate the objects. Instantiation is a process of creating new objects of a class, where objects are known as instances of a class. In this process, the constructor is invoked automatically and memory is allocated for that object. Constructors are generally used to initialize the data members of the new object and it has the same name as the class name.

Create Object Through different types of constructors :

We can further categorize the creation of objects through constructors in three ways.
  • Through Default Constructors like the one we have discussed in the above example
  • Through Parameterized constructors. Here we will explicitly create a constructor in the class with some parameters which are used to initialize the data members of the class.
  • Through Copy constructors. Generally, The copy constructor is a constructor that creates an object by initialising it with an object of the same class that is already created. Let’s understand how to create an object using parameterized constructor and copy the constructor using a simple code.

Code :

#include <bits/stdc++.h>
using namespace std;
class book {
    string author;
public:
    book(string author)
    {
        this->author = author;
        cout<<"Book Author was : "<<author<<"\n";
    }
    book(book& obj)
  {
    author = obj.author;
    cout << "In copy constructor Book Author was : "<< author << "\n";
  };
    
};

int main()
{
    
    book obj1("A.V.Thomas"); //static storage object with parameter
    
    
    book* obj2= new book("A.V.R.S.Stefen"); // dynamic storage object with parameter
  	
  	book obj3(obj1); // copy constructor 
  	//we can also use  	book obj3 = obj1;
 
  	book* obj4 = new book(*obj2); // copy constructor
  	
 
    delete obj2;
    delete obj4;//  deleting dynamic storage object
 
    return 0;
}

Output : 

Book Author was : A.V.Thomas
Book Author was : A.V.R.S.Stefen
In copy constructor Book Author was : A.V.Thomas
In copy constructor Book Author was : A.V.R.S.Stefen

 

In the above example, obj1 is created by static memory allocation through a parametrized constructor where the string “A.V.Thomas” is passed as a parameter. and this is passed as a parameter for the obj3 copy constructor. Similarly, obj2 is created dynamically with parameterized constructor where the string “A.V.R.S.Stefen” is passed as a parameter and that obj2 is passed as a parameter to copy the constructor of obj4.

Leave a Reply

Your email address will not be published. Required fields are marked *