Kittu said:
In which situations, we need to overload copy constructors? What is
exactly practical and theoritically difference between using copy
constructor and assignment operator? Please give the complete answer
in detail as I am not getting satisfactory answer regarding this.
It is extremly simple:
copy constructor
As all constructors, it is used when a *new* object is created.
But this time, the new object should be a copy of an already existing
object (it should be initialized with that object).
Hence the name: *copy* constructor
assignment operator
Is used, whenever an already existing object is assigned the value
of another object.
When you don't write any of them, then the compiler generates one for you.
The generated ones do: memberwise initialization resp. memberwise assignment
You have to write each of those functions by yourself, if this default
behaviour is not sufficient. Any book on C++ will enlighten you, when this
case arises. Alternatively you can search the web for: 'Rule of three' which
is somewhat related to that and its explanation usually also contains an
explanation of when you need to write those functions by yourself.