Answer to Question #180891 in C++ for Waleed Abid

Question #180891

What is a copy constructor? Explain its usage and syntax and also explain how it is different from a regular constructor?


1
Expert's answer
2021-04-15T02:56:56-0400

A copy constructor is a member function that initializes an object using another object of the same class. A copy constructor has the following general function prototype: 

    ClassName (const ClassName &old_obj); 

MyClass t1, t2;
MyClass t3 = t1;  // ----> (1)
t2 = t1;          // -----> (2)


Copy constructor is called when a new object is created from an existing object, as a copy of the existing object. Assignment operator is called when an already initialized object is assigned a new value from another existing object. In the above example (1) calls copy constructor and (2) calls assignment operator.


Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS