C++ Flashcards
(23 cards)
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.
Give a version of int main() that takes arguments.
int main(int argc, char* argv[])
argc is the argument count (includes program name)
argv is argument vector
What are variables defined in functions, classes, elsewhere called respectively?
Functions: local
Classes: member
Elsewhere: global
What are functions defined in classes, elsewhere called?
Classes: member
Elsewhere: global
What is static used for local variables, global variables, functions, classes?
Local variables: makes the variable only accessible within the function and the value persists between function calls
Global variables: makes the variable local to the file
Functions: restrict the function to file scope (only visible to other functions within the same file)
Classes: class scope (shared across all objects of the class)
How to define a namespace?
namespace i_am_lia {
…
}
How to access variables outside of the namespace?
i_am_lia::sleeping();
using namespace i_am_lia;
Is using namespace used in production code?
No