SLIDE 5 5
Parameter initialization list
Box::Box (int h, int w, int d) { height=h; width=w; depth=d; }
Can also be written using parameter initialization list (this way is more efficient).
Box::Box (int h, int w, int d): height(h), width(w), depth (d) {} // no assignment is made.
Parameter initialization list
class PairOfBoxes { private: Box b1; Box b2; public: PairOfBoxes (); }; class Box { private: int height, width, depth; public: Box (int h, int w, int d); };
Parameter initialization list
- Must use initialization list
PairOfBoxes::PairOfBoxes(): b1 (1,2,3), b2(4,5,6) {}
Copy constructor
- Used to construct an object from another
- bject of the same class
- Object has access to non-public members of
- bjects of the same class
Copy constructor
class Box { private: int height, width, depth; public: Box (const Box& B); Box (int h, int w, int d); }; Box::Box (const Box& B) : height (B.height), width (B.width), height (B.height) {}
Copy constructor
Box B (1,2,3); Box C(B); Or Box B (1,2,3); Box C = B; // initialization not assignment