Past Exam Question Preparation Flashcards
What is overloading in C++?
Overloading in C++ is the ability to create multiple functions with the same name but different parameters. This allows functions to behave differently based on the input arguments.
void print(int i);
void print(double d);
Example of overloading
void print(double d);
void print(const std::string& s);
Provide an example illustrating the use of namespaces.
namespace First {
int value() { return 5; }
}
namespace Second {
int value() { return 10; }
}
int val = First::value(); // val is 5
What is scope in C++ and what is the relevance of namespaces to scope?
Scope determines the visibility and lifetime of a variable or function. Namespaces are used to define a scope for identifiers to avoid name collisions, allowing the same name to be used in different contexts.
What does the stream manipulator endl do?
endl is used to insert a newline character into the output stream and flush the stream.
How are reference variables and pointers declared in C++? Give an example of each.
A reference variable is declared with an & symbol, a pointer with a *. Examples:
Give an example using endl.
std::cout «_space;“Hello” «_space;std::endl; // Outputs “Hello” followed by a newline, and flushes the output stream.
What role do assertions play in defensive programming?
Assertions are used to ensure that certain conditions hold true in a program. If an assertion fails, the program is terminated. This helps catch bugs by validating assumptions during development.
Provide an example of using an assertion.
include <cassert></cassert>
int divide(int num, int denom) {
assert(denom != 0); // Program terminates if denom is 0
return num / denom;
}
If a collection of type X contains X and Y objects, how are X and Y related?
X and Y are related by inheritance. Y is likely a subclass of X, meaning Y inherits from X. This allows a collection of type X to store objects of type Y.
How do you declare an instance of a class Example with a member function void function(); and use it?
First, declare the instance of Example, then call the function() on it.
Example myInstance;
myInstance.function();
How are the roles of constructors and destructors related to the lifetime of an object?
Constructors initialize an object when it is created, and destructors clean up before the object is destroyed. They manage resource allocation and deallocation, ensuring proper object lifecycle management.
Provide an example to illustrate inheritance used in a collection.
class X {};
class Y : public X {};
std::vector<X*> collection;
collection.push_back(new X()); // X object
collection.push_back(new Y()); // Y object, which is a type of X
Explain how we manage command line arguments to a program we have written.
Command line arguments are managed in the main function, which can accept two parameters: int argc (argument count) and char* argv[] (argument vector), representing the number of arguments and the arguments themselves.
What does it mean for a class to be abstract, and how do we make a class abstract?
A class is abstract if it has at least one pure virtual function, which means it cannot be instantiated. We make a class abstract by declaring a pure virtual function using the syntax virtual ReturnType FunctionName() = 0;.