Tech

Declarations, Prototypes, Definitions, & Implementations–C++ Notes (C Plus Plus Notes)

Declarations–a declaration introduces a name into a scope. Generally speaking, a scope is either an entire ‘.cpp’ file (source file) or anything in code delimited by {} (braces), be it a function, a loop within a function, or even an arbitrarily placed block of {} within a function. A name introduced, is visible within the scope from the point at which it is declared to the end of that scope. A declaration merely tells the compiler how to use something, it does not actually create anything.

Prototypes–a prototype is just another name for a declaration of a function.

Definitions–a definition fully specifies an entity. Definitions are where the actual creation of the entity in memory takes place. All definitions are also declarations, BUT not all declarations are definitions. A definition is a statement that introduces a new name into a program and sets aside memory for a variable.

Implementations–an implementation is another name for a definition of a function. That is, the implementation is the actual code of the function itself. Any entity can be declared multiple times, but can only be defined once.

Header files (ex: file.h) allow us to make the interface visible to other ‘.cpp’ files (source files), while keeping the implementation in its own .cpp file.

The ‘#include‘ statement is basically like a copy/paste operation. The compiler will “replace” the #include line with the actual contents of the file you’re including when it compiles the file.