
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Difference Between Copy Constructor and Assignment Operator in C++
A copy constructor is a type of constructor that uses another object from the same class that has been created previously, to initialize an object, whereas the assignment operator is used to assign a value to a variable.
In this article, we will understand the difference between the copy constructor and the assignment operator in C++.
Copy Constructor
A copy constructor creates a new object by copying an existing object of the same class which has been created previously. It is of two types, i.e., Default and User-defined copy constructor.
- In the default copy constructor, the copy constructor is not declared, but the C++ compiler creates a default copy constructor that copies all member variables as they are. It performs a shallow copy.
- The User-Defined Copy Constructor is defined by a user and performs a deep copy.
Example 1
The following example demonstrates the default copy constructor performing a shallow copy where the address is copied, not the actual content, and upon changing the original data, the copied data also gets changed:
#include <iostream> using namespace std; class Box { public: int length; Box(int l) { length = l; } void display() { cout << length << endl; } }; int main() { Box b1(10); cout << "Length of box 1: "; b1.display(); // Copy constructor copies b1 into b2 Box b2(b1); cout << "\nLength of box 2: "; b2.display(); return 0; }
The output of the above code is:
Length of box 1: 10 Length of box 2: 10
Example 2
The following example demonstrates the user-defined constructor that performs a deep copy:
#include <iostream> using namespace std; class Box { public: int length; // Constructor Box(int l) { length = l; } // User-defined copy constructor Box(const Box &b) { length = b.length; // Copy value manually } void display() { cout << length; } }; int main() { Box b1(10); cout << "Length of box 1: "; b1.display(); // User-defined copy constructor called Box b2(b1); cout << "\nLength of box 2: "; b2.display(); return 0; }
The output of the above code is:
Length of box 1: 10 Length of box 2: 10
Assignment Operator
The assignment operators in C++ are used to assign values to variables and to set or update the value stored in the variable. The left operand of the assignment operator is the variable name, and the right operand of the operator is the value of that variable. The datatype must be the same for both the operands; if not, a compilation error will be raised. Some of the assignment operators are as follows: '=', '+=', '-=', and so on.
Example
Here is an example of assignment operator:
#include <iostream> int main() { 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; }
The output of the above code is:
The value of a is 5 The value of b is 5
Differences Between Copy Constructor and Assignment Operator
The differences between the copy constructor and assignment operator are mentioned below:
Copy Constructor | Assignment Operator |
---|---|
Copy constructor is a form of overloaded constructor. | Assignment operator is simply an operator that assigns a value to data members and objects. |
Copy constructor initializes the new object with an already existing object. | The assignment operator assigns the value of one object to another object, and both objects already exist. |
In the copy constructor, both objects use separate memory locations. | In the assignment operator, one memory location is used, but different reference variables are pointing to the same location. |
Copy Constructor is invoked when a new object is initialized with an old object and also invoked when the object is passed to a function as a non-reference parameter. | Assignment operator is invoked when the value of an old object is assigned to a new object. |
In the copy constructor, the compiler automatically generates a default copy constructor if we do not define any constructor explicitly. | In an assignment operator, a default assignment operator is automatically generated by the compiler if we do not define any assignment operator explicitly. |
Example:
Box b1(50); Box b2(b1); |
Example:
Box b1(50); Box b2 = b1; |