constructor C++ example

时间:2023-03-08 21:06:05

The constructor for this class could be defined, as usual, as:

 
Rectangle::Rectangle (int x, int y) { width=x; height=y; }
 

But it could also be defined using member initialization as:

 
Rectangle::Rectangle (int x, int y) : width(x) { height=y; }
 

Or even:

 
Rectangle::Rectangle (int x, int y) : width(x), height(y) { }