Copy Constructor in C++ – All you need to get started!

Filed Under: C++
Copy Constructor

Hello, readers! In this article, we will understand and implement a copy constructor in C++

So, let us begin!


What is Copy Constructor in C++?

As we all know, Constructors play an important role in Object-Oriented Programming.

Constructors are important sub-routines that are used to create and initialize objects in the environment.

There are different types of constructors as mentioned below–

  • Parameterized Constructor
  • Non-parameterized Constructor
  • Copy Constructor

A copy constructor is a subroutine (method) that is used to initialize an object which uses another object of the same class.

That is, it creates an object and initializes it with another object of the same class which consists of these objects.

Copy Constructors help us achieve the following functionalities:

  • In order to copy an object in order to return from a method call.
  • To create and initialize an object from another object of the same class.
  • To copy an object and pass it as a parameter to a method.

Syntax of C++ Copy Constructor

Have a look at the below syntax!

Class (const Class &old_obj); 
  • obj: Reference to the object that would be used to initialize another object within the same class.
  • Class: Name of the class.

Why do we need a Copy Constructor?

Read the below question carefully….

When we already have constructors in place, then why has the need for a copy constructor arose?

Isn’t it a valid question, folks??

Well! I do have an answer to satisfy the question.

At times, we need to dynamically allocate resources at run-time within the class and the usual constructors do member-wise operations.

This is when we need “copy constructors“. Moreover, if we do not define any copy constructor, the compiler would create its own copy constructor that would perform member wise operations and would fail for runtime data.


Implementation of a Copy Constructor

In this example, we have created a class AA with data members i and j. Further, we have created a parameterized constructor to initialize the data values at run time.

Now, we have created a copy constructor to initialize an object with the existing object as shown below–

Example:

#include<iostream>
using namespace std;
class AA
{
    
    int i,j;   
     
    public:
    AA(int ii, int jj)
    {
        i = ii;
        j = jj;
    }
     
     
    AA (const AA &ob) // Copy constructor
    { 
        i = ob.i;
        j = ob.j;
    }
     
    void display ()
    {   
        cout<<i<<" "<<j<<endl;
    }
};
 
int main()
{
    AA obj(12,13);     
    AA obj11 = obj;      // Calling the Copy constructor
    cout<<"Using object obj11 to display the data members of object obj\n";
    obj11.display();
    return 0;
}

Output:

Using object obj11 to display the data members of object obj
12 13

Conclusion

By this, we have come to the end of this topic. Feel free to comment below, in case you come across any question. For more such posts related to C++, Stay tuned @ C++ with JournalDev.

Till then, Happy Learning!! 馃檪

Leave a Reply

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

close
Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages