Can someone tell me the correct way of using header files in c++?
So far this is what I have: //Rectangle.h class rectangle { public: rectangle(int a, int b); int x, y; int area(); }; //Rectangle.cpp #include "Rectangle.h" rectangle::rectangle(int a, int b) { x = a; y = b; } int rectangle::area() { return x*y; } The main.cpp file simply uses these as if the class were a part of the main.cpp file itself. The program works fine, but is it the best way? Is it the most efficient way? Is it even what headers are made for?
That is the correct use of a header/cpp file.
Great. Thanks
correct... but int x, y should be private. Just put it above public: to do so, or add private: for clarity(optional). This allows only member functions to access the variables... and aids in encapsulation.
ok
Join our real-time social learning platform and learn together with your friends!