Soft eng Flashcards
(41 cards)
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
Member data represents the state of the object and we only want it to be accessible from within the object and from the outside via abstractions provided by the class –> hence member data is private
.
Insertion of «_space;operator - is it a member function or global function and why?
It is a member function for the ostream class which deals with basic types such as int, float, etc. For all other types and classes (such as strings/user defined types) it is a global function (and possibly friend function).
This is because for a binary operator to be a member function of a class, it has to be a member function of the object on the left hand side of the operator. However with the insertion operator the left hand side is almost always a stream and we would like to override it for the operand on the right hand side. To make the insertion operator a member function of other classes would mean having to re-write it for the standard library which implements ostream, which does not make sense. Hence it is implemented as a global function.
Syntax for writing object oriented code
When expressing inheritence in code, write
class A {}; class B: public A {};
Within the class put semicolon after any function declarations but not definitions. (as you would outside a class). Semicolons after all variable declarations and definitions.
Explain the concept of a type in OOP and how it relates to inheritence.
The type of an object is related what operations it can perform/can be performed on it and this is determined by the class it is an instance of.
Since subclasses inherit all the operations of the base class, they are of the same type as the base class.
What is an abstract class and what is its role in software engineering
An abstract class is one that cannot be instantiated as it contains at least one pure virtual function - a member function which is declared but not defined.
All subclasses to implement their own meaningful versions of the virtual function and the abstract class provides a convenient interface from which to call these functions regardless of the actual object to which they belond (polymorphism).
eg: class Shape{ virtual int calculateArea() = 0; };
class Square{ virtual int calculateArea(...) {...} };
What is the initialization list
Initialisation list is a way to initialise member data from the constructor of a class, often using copy constructors of other classes in the process.
eg: class Triangle which contains 3 instances of class Point: class Triangle{ Triangle(const point& p1_in, const point& p2_in, const point& p3_in): p1(p1_in), p2(p2_in), p3(p3_in) {}
Why is an initialization list important
- Initialising using the copy constructor is more efficient than using a default constructor then overriding using assignment operator
- Assignment operator is not available for some types
- const member data has to be initialised with initialisation list
What are friend functions
Friend functions are global functions that are given access to private member data of a class. eg:
class A{ public: friend void func(A& a); private: int data; };
void func(A& a){ a.data = 10; ... }
int main(){ A object1; func(object1); return 0; }