C++ examples for Class:Constructor
The copy constructor must accept an argument of the same class type and create a duplicate for the class.
A copy constructor should be defined with a const reference parameter, so for the Pool class it looks like this:
class Pool { private: double length {1.0}; double width {1.0}; double height {1.0}; public: // Constructors Pool(double lv, double wv, double hv); Pool(const Pool& pool); Pool(double side); // Constructor for a cube Pool() {} // No-arg constructor double volume(); // Function to calculate the volume of a pool }; Pool::Pool(const Pool& pool) : length {pool.length}, width {pool.width}, height {pool.height} {}
The form of the copy constructor is the same for any class:
Type::Type(const Type& object) { // Code to duplicate of object... }