17.8) Class Implementation File
Assuming that the definition of the Rectangle
class is in a file called rect.hpp, the corresponding method implementations can go in a file called rect.cpp that looks like this:
#include "rect.hpp" Rectangle::Rectangle(int x, int y, int w, int h) { corner_x = x; corner_y = y; width = w; height = h; } int Rectangle::get_x() { return corner_x; } ...
Key Points
- The names of all methods in the implementation file (including the constructor) are prefixed with the class name and two colons
- The constructor does not have a return type!
- The constructor would normally do some validation of the parameters – which we omit here