Soft eng Flashcards
What is binding? What is early vs late binding?
Binding: process of matching function calls to their definitions, performed by compiler and linker.
Early binding: Also known as static binding, is the default in C++. Early binding is when the process of binding occurs at compile time.
Late binding: When binding occurs at runtime, during execution of the program.
What is polymorphism? How does binding relate to polymorphism? Illustrate with an example.
Polymorphism is the principle of providing a single interface to multiple subtypes via a base class.
Late binding is needed with polymorphism as function calls need to be bound to their matching definitions according to the subtype, not the base class. Late binding is achieved using the word “virtual”.
Example: class Base, contains a function disp() which prints "base". class Derived, inherits from base, contains function disp() which prints "derived". main function creates a derived object using Baste * ptr = new Derived. ptr->disp() prints base. If the Base class disp() has the word "virtual" in front of it, ptr->disp() now prints derived.
What are include guards?
Include guards are constructs added to header files to prevent multiple inclusion of any identifiers in the header when they get included via an #include directive.
The #ifndef directive tells the compiler to skip over a file if it has been previously included, thereby solving the problem of multiple inclusion.
guards are: #ifndef \_\_\_ #define \_\_\_ ... #endif
Draw dependency tree:
main -> triangle -> point
main -> square -> point
If i have an iterator it used on a vector v, how do i find:
Address of iterator?
Element being pointed to?
Address of element being pointed to?
Index in v currently being pointed to?
Address of iterator: &it
Element being pointed to: it
Address of element being pointed to: &(it)
Index in v currently being pointed to: it - v.begin()
Difference between using iterators on a vector of ints vs a vector of templates
ints:
std::vector::iterator it = vec.begin();
templates:
typename std::vector::iterator it = vec.begin();
Explain why the separation of function definitions from their declarations is not possible with template functions
A template function is a template for the compiler to use when it encounters a function call with actual parameter types. When compiler instantiates the actual function, it needs the full function definition to perform syntactic checks. This instantiation is performed at compile time, before linking, hence the template function’s definition cannot be seperate to its declaration.
Do you put semicolons after an initialization list in the constructor?
No semicolon, only open and closed curly braces.
A(x_in): x(x_in) {}
friend functions are declared in the class with the friend keyword in front. They are defined outside the class without the friend keyword.
Friend keyword = this function can access my private member data
.
If a function/operator takes a template class as input, what does the defintion look like?
Put a template before it, and in the declaration have a after the class name
What is exception safe code?
Exception safe code keeps state consistent and handles resources appropriately even if an exception is thrown.
Exceptions vs exit instruction
The exit instruction terminates the whole program and the only information as to the source of the error is the exit code, which is not very informative for the programmer.
Exceptions don’t terminate the entire program - only the current function being executed, and they pass control to the caller for exception handling.
Different types of exceptions, describing different errors and prescribing different handling mechanisms, can also be thrown.
If an exception is thrown, control goes to the caller and keeps going up until a try block is encountered. In that case no other instructions within the try block are executed and control passes to the catch block.
Destructors of all objects created within scope of try block are called when exiting try block.
.
virtual vs virtual = 0
virtual –> base class has an implementation for it. Subclasses, if they override it, will have their own implementation. Function calls will be resolved at run time to the implementation of the object on which they are called.
virtual = 0 –> PURE VIRTUAL, the base class is an abstract class. Every subclass must override this function and say so (using keyword override before {}).
Filled diamond vs empty diamond vs arrow in UML diagram
Filled diamond: Composition. When member variables of a class are objects of another class.
Empty diamond: Aggregation. the other class can be useful without the class that is composed of it…?
Arrow: Inheritence. Subset.
Order of catch blocks
Most specific to most general, because a specific exception inherits from general exceptions so it will get caught in the first general catch block it encounters.
Access modifiers and differences between them
Public: fields are accessible from within and outside the class Private: fields are accessible only from within the class and by friend functions, not even by subclasses Protected: fields are accessible within the class and by subclasses