Define and initialize a variable to store the value 4.2
float a{4.2};
double a{4.2};
The symbol appended to a data type in a function parameter list to declare a parameter as pass-by-reference.
What is & (ampersand)
Members marked with this access specifier can be directly accessed from anywhere in the program, including outside the class implementation (such as inside main()).
What is public?
An instance of a class created in memory that holds both state (member variables) and behavior (member functions).
What is an object?
This C++ keyword is placed before a function or class declaration to indicate that it operates with generic types.
What is template?
How do you negate a boolean value?
Using the not ('!') operator
What is printed from the following code:
void Triple(int& num) { num *= 3; }
void Run() {
int val = 10;
Triple(val);
std::cout << val;
}
30
Public member functions designed specifically to safely inspect or update the values of private member variables are commonly referred to by these two terms.
What are getters and setters (or accessors and mutators)?
When a stack-allocated object goes out of scope, this special member function is automatically executed to clean up its resources.
What is a destructor?
This standard library container stores elements as unique key-value pairs sorted automatically in ascending order by key.
What is std::map?
Add 42 to the vector my_ints
my_ints.push_back(42);
Passing large standard library objects (such as std::vector or std::string) by this parameter type avoids expensive dynamic memory allocations and copies while keeping the original object read-only.
What is const reference (or const std::vector<T>&)?
This constructor is provided for you even if you don't explicitly define one.
What is the default constructor?
Adding this keyword to a function definition prevents the modification of data passed by reference.
What is 'const'?
When iterating over a std::map using a range-based for loop, this member name of the element pair accesses the key.
What is first (e.g., pair.first)?
Fix the following for loop so that it iterates 10 times total:
for(int i = 0; i <= 10; i++){
sum += 1;
}
'<=' becomes '<'
i = 1
Unlike pointers, a reference in C++ cannot be set to nullptr and cannot undergo this operation after it is initialized.
What is re-binding (or re-assigning to reference a different variable)?
According to the Google C++ Style Guide, all private data members of a class should end with this specific character to distinguish them from local variables and parameters.
What is a trailing underscore (_)?
If a class contains a reference variable as a member (T& member_), it must be initialized in this constructor section because references cannot be default-declared.
What is the member initializer list
What is printed by the following code:
std::map<std::string, int> scores = {{"Alice", 90}, {"Bob", 85}};
scores["Alice"] = 95;
scores["Charlie"] = 100;
std::cout << scores.size();
3
Write a function that takes two integers and returns their average.
double avg_calc(int first, int second){
return (first + second) / 2;
}
Returning a non-const reference to a variable declared inside that same function creates this dangerous runtime bug.
What is a dangling reference (or undefined behavior)?
In standard C++ project architecture, class declarations and public interfaces are placed in header files (.h), while member function implementations are written in files with this extension.
What is .cpp?
What problem would occur if an object with a reference member variable was copied?
There would be more than one object that has a member variable pointing to the same data.
Calling Max(5, 3.14) on template <typename T> T Max(T a, T b) causes a compilation error for this reason.
Template expects the same type; to pass different types, only one would be templatized