Ask your own question, for FREE!
Computer Science 8 Online
OpenStudy (anonymous):

Can someone tell me the correct way of using header files in c++?

OpenStudy (anonymous):

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?

OpenStudy (rsmith6559):

That is the correct use of a header/cpp file.

OpenStudy (anonymous):

Great. Thanks

OpenStudy (anonymous):

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.

OpenStudy (anonymous):

ok

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!