copy constructor
A copy constructor is a special constructor in the C++ programming language for creating a new object as a copy of an existing object. The first argument of such a constructor is a reference to an object of the same type as is being constructed (const or non-const), which might be followed by parameters of any type (all having default values).
Example
#include <iostream> using namespace std; class Vehicle { public: int wheels; }; int main() { Vehicle Car; Car.wheels = 4; cout << "from object Car, car wheels are " << Car.wheels << endl; Vehicle smallTruck(Car); // copy constructor cout << "from copy constructor, small truck wheels are " << smallTruck.wheels << endl; getchar(); return 0; }
Output :
from object Car, car wheels are 4 from copy constructor, small truck wheels are 4