A header file has the extension .h and a program (or source) file has the extension .cpp. The header file contains the class declarations for one or more classes, and the program file contains method definitions. The program file includes the header so that it knows about the declarations.
The scope operator :: is used when defining methods. If I have a class called Foo and it has a method called myMethod, when defining the function in the .cpp file, I would call it Foo::myMethod. The scope operator is needed because a .cpp file could contain method definitions for multiple classes, so we need to know for which class each method is being defined.
Three: public, private, and protected
Inlining occurs in C++ when you define
methods in the header file. Inlining is typically used only
for very short methods. For short routines, C++ can
frequently optimize things which are declared inline by
inserting the code for the routine directly
in the calling function, thus avoiding the large overhead of
calling a function. So, it is often to your advantage to
inline definitions of short, frequently called functions
that don't require use of an external library. For instance:
//Color.h
class Color
{
public:
Color();
int getRed() {return _red;} // inline declaration
void setRed(int red) {_red=red;} // inline declaration
/* same for green and blue */
protected:
int _red, _green, _blue;
};
Medium and large-sized functions should not be inlined, even if they are frequently used. Since the the compiler will insert the code for inlined functions wherever the function is called, inlining large functions will increase the size of your program, possibly slowing it down.
int myNumber;
int* myPtr;
int myNumber = 0;
Two ways of commenting are available:
/* */ comments a block sequence // comments single lines
|
|
|