C++ Flashcards
What is the keyword to add to prevent reassignment?
const
How to instantiate an array? How to access elements in the array?
int array[ARRAY_CAPACITY];
array[INDEX];
What are statements that start with #?
Preprocessor directives
What happens to the preprocessor directives when we compile a C++ program?
When we compile, the first step is preprocessing. The preprocessor scans the cpp file for any lines starting with #. It replaces these lines with the actual content of the included file. The output of the preprocessor is a modified version of the source code.
Does the preprocessor need to understand C++ semantics?
No, all it does is text-based manipulations. It just modifies the code before the compiler sees it.
Why is it beneficial to have a header file separate from the source code?
The source code will be compiled into a library .o. It allows other programs to use the functions by including the header file only. Since the library implementation is already compiled into an object file, it saves time during compilation.
What is a library?
A collection of precompiled object files packaged together.
What does it mean to compile into a library?
- Compile a .cpp file into an object file
- Package those object files into a library
Why is having a library useful?
The library can be reused across programs. The developer does not have to distribute the source code and recompile every time, only the library file.
What is the difference between a static library vs a dynamic/shared library?
When you link a program with a static library, all the required code is copied into the final executable. When you use a dynamic library, the library code is not copied. The executable refers to the library, which is loaded into memory at runtime.
Would there be errors if you include the same header file multiple times into your program (either directly or indirectly)?
Yes
What is the directive to prevent a header file from being included multiple times in one cpp file?
pragma once
What are include guards? How to write them?
ifndef MYHEADER_H
They are a traditional way to prevent multiple inclusions.
#define MYHEADER_H
…
…
#endif
What does a cpp file need to have to be compiled into an executable?
It needs a int main(), if not, it can only be compiled into an object file.
Does a main() always return an int?
Yes, it is always defined as int main() and returns an int. The return value serves as an exit status, so it always expects 0, indicating a successful execution of the program.