1.4
Identifier names that start with a capital letter are typically used for structs, classes, and enumerations (all of which we will cover later).
int MyVariableName; // incorrect (should start with lower case letter)
Second, you should avoid naming your identifiers starting with an underscore, as these names are typically reserved for OS, library, and/or compiler use.
typically the name of the header file with a _H appended to it is used. For example, MATH_H
Identifier names that start with a capital letter are typically used for structs, classes, and enumerations (all of which we will cover later).
int MyVariableName; // incorrect (should start with lower case letter)
Second, you should avoid naming your identifiers starting with an underscore, as these names are typically reserved for OS, library, and/or compiler use.
1.6 — Whitespace and basic formatting
Newlines are not allowed in quoted text:
1
2
|
cout << "Hello
world!" << endl; // Not allowed!
|
1.7 — Forward declarations and definitions
because the function prototype is a statement, it ends with a semicolon.
int add(int x, int y); // function prototype includes return type, name, parameters, and semicolon. No function body!
or
1
|
int add(int, int);
|
1.9 — Header files
A header file only has to be written once, and it can be included in as many files as needed. This also helps with maintenance by minimizing the number of changes that need to be made if a function prototype ever changes
Header files consist of two parts.The first part is called a header guard.The second part is the actual content of the .h file, which should be the declarations for all of the functions we want other files to be able to see.
Rule: Use angled brackets to include header files that come with the compiler. Use double quotes to include any other header files.
Rule: Each .cpp file should explicitly #include all of the header files it needs to compile.
when you #include a file, the entire content of the included file is inserted at the point of inclusion. This means that any definitions you put in your header get copied into every file that includes that header.
- Always include header guards.
- Do not define variables in header files unless they are constants. Header files should generally only be used for declarations.
- Do not define functions in header files.
- Each header file should have a specific job, and be as independent as possible. For example, you might put all your declarations related to functionality A in A.h and all your declarations related to functionality B in B.h. That way if you only care about A later, you can just include A.h and not get any of the stuff related to B.
- Give your header files the same name as the source files they’re associated with (e.g. grades.h goes with grades.cpp).
- Try to minimize the number of other header files you #include in your header files. Only #include what is necessary.
- Do not #include .cpp files.
add.h:
main.cpp that includes add.h:
1.10 — A first look at the preprocessor
Object-like macros can be defined in one of two ways:
The top definition has no substitution text, whereas the bottom one does. Because these are preprocessor declarations (not statements), note that neither form ends with a semicolon.
Macros of this form work like you might expect: when the identifier is encountered by the preprocessor (outside of another preprocessor directive), it is removed and replaced by nothing!
directives are only valid from the point of definition to the end of the file in which they are defined. Directives defined in one code file do not have impact on other code files in the same project.
3
4
5
6
|
#ifndef SOME_UNIQUE_NAME_HERE
#define SOME_UNIQUE_NAME_HERE
// your declarations and definitions here
#endif
|
Comments
Post a Comment