The assignment operator is used to copy the values from one object to another already existing object. The key words here are “already existing”. Consider the following example:
Cents cMark(5); // calls Cents constructor
Cents cNancy; // calls Cents default constructor
cNancy = cMark; // calls Cents assignment operator
In this case, cNancy has already been created by the time the assignment is executed.
Consequently, the Cents assignment operator is called. The assignment operator must be overloaded as a member function.
What happens if the object being copied into does not already exist? This is where the copy constructor is called.
Consider the following example:
Cents cMark(5); // calls Cents constructor
Cents cNancy = cMark; // calls Cents copy constructor!
Because the second statement uses an equals symbol in it, you might expect that it calls the assignment operator. However, it doesn’t! It actually calls a special type of constructor called a copy constructor. A copy constructor is a special constructor that initializes a new object from an existing object.
The purpose of the copy constructor and the assignment operator are almost equivalent — both copy one object to another. However, the assignment operator copies to existing objects, and the copy constructor copies to newly created objects.
The difference between the copy constructor and the assignment operator causes a lot of confusion for new programmers, but it’s really not all that difficult. Summarizing:
a - If a new object has to be created before the copying can occur, the copy constructor is used.
b - If a new object does not have to be created before the copying can occur, the assignment operator is used.
I apologize for the length, but I hope I helped clear things up.