C++ Basics Flashcards
What is the auto keyword? Provide an example.
Auto can be used during assignment to automatically set a variable to the type of it’s assigned variable.
What are the three ways to assign a value to a variable (e.g. int)?
a = 5;
b(3);
c{2};
Can you create a new type?
Yes, with the typedef keyword
What is the alternative way of writing false?
0
What is the alternative way of writing true?
1 (or ‘non-zero’)
What is the structure of a C++ array?
type name [size]
What is a null character in a string?
‘\0’
How do you get the memory address of that variable? E.g. int x = 7;
&x
Why do we need pointers?
-dynamically allocate memory
–you can write programs that can handle unlimited amounts of memory-allow a function to modify a variable passed to it
-easier to pass around the location of a huge amount of data than passing the data itself
How do you define a pointer?
*p;
int *y;
How can we get the content of the memory address pointed to by the pointer?
By using the dereference operator ‘*’
int x = 10; //variable x = 10
int* y = &x; //pointer variable y = Address of x
int z = *y; //z = “value pointed to by y” = x, i.e. z = x;
What is the l-value?
The expression that refer to a memory location e.g. a variable
Can be both side
What is the r-value?
The data stored in some memory address (can only be on the right side)
What are the compound data types in C++?
Any type that isn’t a primitive.
Arrays Char sequences Strings Pointers Dynamic memory Data structures
Can pointers be declared as constants?
Yes but can’t change memory address
When casting to a lower size type - what happens to the value?
You chop the size of the lower type on the higher type from the LSB up to the size of the type
0000 0001 0100 0000 (short 2 bytes) 0100 0000 (char 1 byte)
What is the difference between a class and structs?
Struct members are public by default
Why use an enum?
Restricted set of variables
How do access the attribute ‘name’ from a ‘Person’ struct Declared ‘Person person’?
person.name;
How do access the attribute ‘name’ from a ‘Person’ struct Declared ‘Person * person = new Person’?
person->name;
Is it possible to have arrays of structs?
Yes
Where would you use an array of structs?
A database
In OOP what are three things to consider with structs?
Member accessibly
Code visibility
Encapsulation
What are the five program memory components?
Code segment BSS Segment Data Segment (DS) Heap Stack
What is contained in the BSS segment?
all uninitialized, global and static
What is contained in the DS segment?
Initialized variables
What are the three types of memory allocation in C++?
Static
Automatic
Dynamic
What is static memory used for?
For static and global variables
What is automatic memory used for?
For local variables and function params
What is static memory used for?
Heap memory
For function overloading, what part of the signature isn’t considered for uniqueness?
the return type
What are the types available for integral promotion to int?
bool, char and short
What value is used for the return type in a template function?
The left most type
What issue may arise from template functions with different types?
Might end up with a type you weren’t expecting
When do you use virtual?
To declare an overridable method in a parent class