Categories

Jul 16, 2019 in C++

Q: What is Copy Constructor?

1 Answer

Jul 16, 2019

A copy constructor is a member function which initializes an object using another object of the same class. A copy constructor has the following general function prototype: ClassName (const ClassName &old_obj);

Point(int x1, int y1) { x = x1; y = y1; }

// Copy constructor

Point(const Point &p2) {x = p2.x; y = p2.y; }

When is copy constructor called?

In C++, a Copy Constructor may be called in following cases:

  1. When an object of the class is returned by value.
  2. When an object of the class is passed (to a function) by value as an argument.
  3. When an object is constructed based on another object of the same class.
  4. When compiler generates a temporary object.
Click here to read more about C++
Click here to read more about Insurance

Related questions

Madanswer
Jul 16, 2019 in C++
Jul 16, 2019 in C++
Jul 16, 2019 in C++
...