C++ Intro Flashcards
C++ but before reading the codebase or any advanced books
What does it mean that C++ is a “compiled language”?
It means the compiler takes object files, and turns them into executables.
What does it mean that C++ is “statically-typed”?
It means that the type of every object must be known to the compiler when it’s used.
What is a “built-in type”?
It’s one that can be built from the fundamental types.
What is a “user-defined type”?
It’s one that is built out of other types using abstraction mechanisms.
Give two examples of user-defined types.
Classes, enums.
How does the compiler handle virtual functions?
It creates the “virtual function table”, a table of pointers to functions.
What is it called when you do like: #indef FOO_BAR_BAZ_H__ #define FOO_BAR_BAZ_H__ … #endif // FOO_BAR_BAZ_H__
It’s called an include guard.
Why would you use an include guard?
To prevent the same header file from being included multiple times during compilation.
What are #ifndef, #define, and #endif called?
They are called “preprocesssor directives”
What does the “extern keyword mean” e.g. extern const int foo?
You use it in header files to say “I have this variable”. It’s up to the source file to define it.
How much memory is allocated when you put “extern” in your header file?
None.
What happens if you declare an extern variable in your header file, and then don’t define it in your source file?
You get a linker error like “unresolved external symbol”?
Why isn’t the extern keyword used for class declarations?
Because a class is a type, not a variable – when you say “class” you are just defining the type, not instantiating a specific variable.
What does the static keyword do?
It is similar to “private” in Java, but it’s about file scope – which files can access the property. Static means that only this file can access them. This is called “internal linkage”.
What does “internal linkage” mean and how do you indicate it?
It means the identifier is only visible in the source file where it is included. You indicate it with the “static” keyword.
Why is almost everything in a header file static?
Because you don’t need other files to be able to see those properties, just the source file.
Would you declare a public interface function as static?
No, you want every file to be able to see it, not just the source file.
What does it mean when a variable definition – not just declaration – is static?
This is declaring a singleton, ensuring it is only created once.
Under what two circumstances is there code in the header file rather than declarations?
You would declare small inline functions that you want inserted directly at the call site, to eliminate function call overhead. And you would declare templates, since the compiler needs the complete definition so it can generate code wherever the template is used.
What is the difference between include and import?
Include processes the complete text of the file every time it’s included, and the order matters. Import is just done once.
What are the things that you import called?
Modules
How do you define a module?
You put “module;” at the top of the file, and then “export module ModuleName”.
What version of C++ introduced modules?
C++ 20.
What is a namespace?
A namespace is something you use to group declarations together without worrying about naming conflicts.
When you do “#include ” you have to put “std::” in front of everything. What are two ways to not have to do this?
You could do “using std::foo” to specifically let you say “foo”, or “using namespace std” so you don’t have to use the prefix anywhere at all.
What is namespace scope and how far does it extend?
Namespace scope means the name is defined in a namespace and not in any particular function or class. It extends to the end of the namespace.
What are the two parts of a class?
Interface and implementation
What is the difference between interface and implementation?
Interface is used by everyone. Implementation is the private details the class keeps to itself.
What keywords to interface and implementation correspond to?
Public (interface) and private (implementation)
What is the syntax for a constructor?
ClassName(params) :var{assignment}, var{assignment} { stuff }
How do you do variable assignment in the constructor? What is the syntax?
You declare initializers before the body. The syntax is “:var{assignment}”.
What is it called when you do “:var{assignment}”
The member initializer list
What does it mean when you define a method as const?
It means it will not modify the object that it’s a member of.
What is the syntax for defining a const method?
You put “const” before the body: double real() const { … }
What is it called when you separate the function body definitions from their declarations?
Separate compilation
Where do the declarations go, and where do the definitions go, for a class?
Declarations go in the header file, definitions go in the source file.
What is the difference between an enum class and an enum?
In an enum class, the enumerators (enum values) are placed in the class’s scope. So you would access them like EnumClassName::Red. In a raw enum, the values just go in the surrounding scope as aliases for ints.
What is an operator function?
It’s when you define an actual operator, like + or =.
What happens at compile time when you do “foo + 1”?
The compiler replaces it with FooType::operator+(foo, FooType{1})
What does the “virtual” keyword do?
It defines a virtual function, which is like an abstract function.
What is a virtual function?
It is a function that may be redefined later in a derived class.
What is a PURE virtual function and how do you define it?
It is a function where the derived class MUST define the function. You define it by putting “= 0” at the end.
What is it called when a class has a pure virtual function?
The class is called an abstract class.
What is special about an abstract class?
You can not instantiate it.
What is a polymorphic type?
It’s when you define an abstract class, like “Shape”, that you want to be the parent of a bunch of other classes.
What does it mean when a function has “= 0” after it?
It means it’s a pure virtual function – an abstract function that MUST be overridden by the child class.
What is the syntax for class inheritance?
class Triangle : public Shape {
How do you override a virtual function in the derived class?
You use the “override” keyword
What does it mean when a function has the “override” keyword after it?
It means it is overriding a virtual function in the parent class.
What is the one situation where you don’t have to use the override keyword for a virtual function?
When you’re overriding the destructor.
What are the six essential operations?
Constructor, Destructor, copy+move, copy+move assignment.
What does it mean when an essential operation has “= default” at the end?
It means just use the default implementation.
What is the default implementation of copy?
Run copy on every single member variable.
What is an example scenario where you would not want to use the default implementation?
When you have pointer members, copying them would be disastrous.
What do you do if you want to prevent an essential operation from being called at all?
Use the “delete” keyword.
What does the “delete” keyword do?
It’s used on essential operations to prevent them from being called.
What is the “rule of zero”?
It means a class should define either all or none of its essential operations.
What does “explicit” mean, for a constructor?
It means that the constructor must be explicitly called, and can’t be implicitly called by passing in its initializer variables to a function that requires it.
What is “this”?
A variable referencing the object for which the function is being called.
What is a reference variable?
It’s like an alias for an existing object.
How do you define a reference variable?
You put & at the end of the type, like “std::string& foo”.
What is the difference in & placement between a reference and an address?
& in the type is a reference. & in the variable name is its addresss.