Difference Between Copy Constructor and Assignment Operator in C++


C++ is a General purpose, middle-level, case sensitive, platform independent programming language that supports object oriented programming concept. C++ was created by Bjarne Stroustrup at Bell Labs in 1979. Since C++ is a platform independent programming language, it can be used on a variety of systems, including Windows, Mac OS, and various UNIX versions.

Operators in C++ are used to perform specific operations on values and variables. The following are the list of operators used in C++

  • Arithmetic operators

  • Assignment operators

  • Comparison operators

  • Relational Operators

  • Logical operators

  • Bitwise operators

  • Unary operators

  • Ternary/Conditional operators.

From the above groups, Assignment operators are used to assign values to variables.

Before learning about copy constructors, let’s get a brief idea about constructors. A Constructor in C++ is a special method, same name as class name with parenthesis "( )", that is automatically invoked when an object is created. Constructor is used to initialize the variables of the object which is created newly.

A Copy Constructor is a type of constructor that uses another object from the same class which has been created previously, to initialize an object.

Now let’s go through the detailed concept and compare and contrast the various features of copy constructor and assignment operator.

What is Assignment operator

The use of Assignment operator is to assign a value to a variable. The left operand of the assignment operator is variable name and the right operand of the operator is value to that variable. The datatype must be same for both the operands, if not so a compilation error will be raised.

The types of assignment operators are

  • = operator − It only assigns the value to the variable. For example, "a=10", here the value 10 will be assigned to variable "a".

  • += operator −This operator first adds the current value of the variable by the value which is on right side and then assigns the new value to variable.

  • –= operator − This operator first subtracts the current value of the variable by the value which is on right side and then assigns the new value to variable.

  • *= operator − This operator first multiplies the current value of the variable by the value which is on right side and then assigns the new value to variable.

  • /= operator − This operator first divides the current value of the variable by the value which is on right side and then assigns the new value to variable.

Example on Assignment Operator

Let’s see an example of assignment operator. Here we are using the assignment operator to assign values to different variables.

#include <iostream>
int main() {
   // Write C++ code here
   int a=5,b;
   b=a;
   std::cout << "The value of a is "<<a<<"\n";
   std::cout << "The value of b is "<<b;
   return 0;
}

Output

In the above example, we have taken two variables "a" and "b" and at first we have assigned the value of a to 5 through assignment operator "=". And we have assigned the value of a to variable b. The above code will result the output as given below.

The value of a is 5.
The value of b is 5.

What is a Copy Constructor?

This is often required in programming to make a separate copy of an object without impacting the original. In these cases, the copy constructor comes in use. The copy constructor is a constructor that creates an object by initializing it using a previously created object of the same class. There are two types of copy constructor.

  • Default Copy Constructor − When the copy Constructor is not declared, the C++ compiler creates a default Constructor that copies all member variables as they are.

  • User-Defined Copy Constructor − The copy constructor defined by the user is called user defined copy constructor.

Syntax

The syntax for Copy Constructor is −

Class_Name(Class_name & oldobject)
{
   // Body of constructor
}

Copy Constructor - Example

The copy constructor is used to initialize one object from another object of the same class, to copy an object to pass as an argument to a function, and to copy an object to pass as a parameter to a function. To return an object from a function, copy the object.

Example

Let’s see an example to understand how exactly we can use a copy constructor.

#include<iostream>
using namespace std;
class Example {
   int x, y; //data members
   public:
   Example(int a, int b) {
      x = a;
      y = b;
   }
   /* Copy constructor */
   Example(Example & ex) {
      x = ex.x;
      y = ex.y;
   }
   void display() {
      cout << x << " " << y << endl;
   }
};
/* main function */
int main() {
   Example obj1(20, 30); // Normal constructor
   Example obj2 = obj1; // Copy constructor
   cout << "Normal constructor : ";
   obj1.display();
   cout << "Copy constructor : ";
   obj2.display();
   return 0;
}

In the above example, we have taken the class name as Example, and created a constructor and passed the value 20 and 30 to the constructor. The statement Example (Example &ex) indicates the copy constructor. It copies the value of the data members previously created.

Output

The above code will produce the following output −

Normal constructor : 20 30
Copy constructor : 20 30

In our example, we have created two objects obj1 and obj2 and we are assigning the value of obj1 to obj2.

Comparison between Copy Constructor and Assignment Operator

The main purpose of both the concepts in C++ is to assign the value, but the main difference between both is copy constructor creates a new object and assigns the value but assignment operator does not create a new object, instead it assigns the value to the data member of the same object.

The following table highlights the major differences between copy constructor and assignment operator.

Basis of Comparison
Copy Constructor
Assignment Operator
Basic Comparison
  • Copy constructor is a form of overloaded constructor.

  • Copy Constructor initializes a new object by an already existing object of the same class.

  • Assignment operator is simply an operator which assigns some value to data members, objects.

  • It assigns the value of one object to another object that are already created.

Memory Management
The old object which was created and the new object which is invoked, will share different memory locations.
The first object and second object which is assigned the value of first object, shares same memory locations.
Syntax
Syntax for copy constructor is: Class_name(Class_name &Old_obj);
Syntax for assignment operator is: Class name Obj1, Obj2; Obj2=Obj1;
Invoke
Copy Constructor is invoked when a new object is initialized with old object and also invoked when the object is passed to a function as non-reference parameter.
Assignment operator is invoked when the value of an old object is assigned to a new object.

Conclusion

The difference between a copy constructor and an assignment operator is that a copy constructor helps to create a copy of an already existing object without altering the original value of the created object, whereas an assignment operator helps to assign a new value to a data member or an object in the program.


Advertisements