C++ Flashcards

1
Q

How should one always give arguments to a function if possible?

A

as const reference

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

one line if statement

A

“expression” ? “command if true” : “command if false”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

what should you always check when creating new functions?

A

const correctness

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

what is a static member variable?

A

A variable which belongs not to the instance but to the class -> multiple instances access the same value

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

what is :: called?

A

scope resolution operator -> then searches global namespace

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

If you do not specify the access modifier in a class, are it’s members private, protected or public?

A

private

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

what is a std::map?

A

A container, that stores key-value pairs. Keys must be unique (otherwise the old entry is overwritten). A map is sorted through the keys through the < operator, which can also be provided.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

what are std::vector and std::list?

A

both are containers
vector: elements in one block of memory, easy to add and remove elements at end, not so easy to insert, …
list: indirect adressing -> good for insertion, slower for iteration

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

what important std containers are there?

A

vector, list, map, multimap, queue, stack, deque, set, array, …

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

what is the extern keyword?

A

it says, that a variable is defined in another translation context

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

how does one define a C++ macro?

A

define “macroname” “code”

  • not recommended in modern C++
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

How do I declare a variable that stores the address of another?

A

varA = &varB;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

what is * called in C++? How can it be read?

A

Dereference operator
“value pointed to by”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

what is & called in C++? How can it be read?

A

2 meanings:
1. Address-of operator when used in expression
“address of”
2. In declaration: part of identifier, denotes a reference
“is a reference to”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

How does one declare a reference in C++?

A

“type” & “reference name” = var;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

what is the difference between a pointer and a reference?

A
  • reference is a “const pointer”. It is initialized at declaration and the addres it points to cannot be changed.
  • it is dereferenced implicitly, so no * is needed
17
Q

What is pass-by-value/reference?

A

a pass by value function takes an input object and makes a copy of it, thus not changing the objects value.
pass by reference takes a reference and thus operates on the original object.

18
Q

What does the declaration of a pass-by-reference function look like? How is it called?

A

typeA func(typeB & a){…}

typeB b = …;
func(b)

19
Q

When should objects be initialized? And why?

A

Always upon declaration to omit undefined behaviour

20
Q

Should one initialize members as declaration in the constructor or in the initialization list?

A

Always in the initialization list

21
Q

In a switch, if multiple statements shall execute the same code, how does one do it?

A

case a:
case b:
{
shared code…
} break;

22
Q

what does mutable/immutable mean in C++?

A

immutable means not changeable (const). Mutable is a keyword that can be applied to a field of a const object, which reverts it’s immutability

23
Q

which function converts const char to int? (e.g. for input args)

A

atoi()

24
Q

what is the difference between
CParid testParid = getUpdatedParidFromFile();

and
CParid testParid;
testParid = getUpdatedParidFromFile();

A

the first uses the copy constructor, while the second uses the = operator

25
Q

how does one define a lambda function?

A

“type” name = [“capture clause”] (“arguments”) -> “explicit return type (optional)”{ “function body”}

26
Q

How can I define a function in a .h file?

A

By using the inline keyword. Otherwise the linker will load it multiple times if applicable

27
Q

How is an R-value reference written

A

&&

28
Q

When should a virtual destructor be used? And when not? why?

A

Polymorphic base classes should have a virtual destructor. Final classes should NOT!
If a polymorphic base class does not have a virtual destructor, only the child classes destructor is called upon deletion, leading to a memory leak (where the “base class part” does not get deleted)
If a final class has a virtual destructor (or any virtual function), additional overhead (virtual table) is created.

29
Q

When creating an empty class, which methods are created automatically?

A

Constructor
Copy Constructor
Destructor
Copy Assignment Operator

30
Q

When is no default copy constructor, destructor or copy assignment operator created?

A

If the logic doesn’t make sense. I.e. if a member is a reference, reassignment doesn’t work. Or if the class has a base class with private destructor, since the default destructor want’s to call the base’s

31
Q

how does one declare a function, that takes a function as an argument?

A

type functionName(std::function<return type(input type)> name)

32
Q

how can a function that takes an argument be called without arguments?

A

If default arguments are defined or it is overloaded

33
Q

What is the difference between heap and stack memory? How are variables allocated there?

A

Stack: very quick, follows call stack. Value type initialization writes there. Deleted when scope ends (by moving memory pointer back)
Heap: accessible from outside of scope and not in order. Reference types (new keyword) a lot of overhead. Danger of garbage.
Always use stack if possible

34
Q

Does static write variables into stack or heap?

A

Non of those. Static is it’s own memory segment

35
Q

what is the difference between const_iterator and iterator?

A

const iterator doesn’t allow you to change the values it points to