C++ Basics and OOP Flashcards
Learn the basics of the C++ language and Object Oriented Programming
What is the difference between an object and a class?
A class is a blueprint that specifies the methods, variables and their visibility. An object is an instance of that blueprint.
Briefly describe abstraction and why we use it in OOP?
Abstraction is the concept of hiding implementation details away from the user, providing only the necessary details. This is used as it makes it much simpler to interact with complex systems.
What is encapsulation?
Encapsulation is about binding related variables and methods together. This is achieved in OOP with classes.
What is inheritance? Why use it?
Inheritance enables us to define new derived classes that share the same structure and properties as their parent class. We use it to improve code reusability by recycling the original clas.
What is polymorphism in OOP? What are the two types available?
Polymorphism is the ability to assign multiple behaviours to a class method dependent on the context it is called in. The two available is run-time (method overriding) and compile-time (method overloading).
What is the difference between method overloading and overriding?
Overloading - methods with same name but different parameters. CANNOT overload on return type.
Overriding - subclasss method with the same name but with a different implementation. Changing return type would result in method hiding.
What is the difference between method overriding and hiding?
Suppose we have two classes A and B where B inherits from A. Suppose A has a method (e.g. virtual int getNum() { return 5; }). B can override and hide it by providing a method matching the signature and return type (e.g. int getNum() { return 4; }). If B instead provides a method whose return type or parameters don’t match (e.g. float getNum(float a) { return a * 1.2f }), then the new method hides, but does not override A’s original method.
If we construct an A based on type B, (A *a = new B()). Calling getNum if overriden will call B::getNum(), but calling if only hidden will call A::getNum().
What are virtual and abstract (pure virtual) methods?
A virtual method is one whose implementation can be overriden by a derived class. They are used to achieve runtime polymorphism as they always ensure the correct method version is always called for a given class.
A pure virtual method is one in which there is no default implementation provided. The class is considered abstract i.e. it cannot be instantiated. In C++, this can be achieved by setting the method equal to zero. Any inheriting classes must provide an implementation or they will also be abstract.
What are the steps in compiling a program?
Lexical - Get tokens from code text
Syntactic - Check token syntax is correct
Semantic - Check tokens make sense
Code generation - Generation of code
Optimisation - Optimize code
Why are C++ files typically split up into header (.h) and source files (.cpp)?
The purpose of doing this is to improve compilation speed. A class’s implementation in the source file is typically changed more often than its definition. By keeping them separate, changes to the implementation do not require other files that include its definition to be recompiled, saving on compilation time.
What is a forward declaration? List two useful features they can provide.
A forward declaration provides the symbol that a file can use without providing the full definition. If a class header only accesses a pointer or reference to a specific class, we can just use a forward declaration rather than include its header.
One use is to improve compilation times. By avoiding unnecessary includes, less compilation work needs to be done if the forward-declared class header is updated.
Another use is to break cyclic dependencies. If two classes require pointers to each other, one of them can use a forward declaration to avoid a cyclic dependency.
What is the difference between inheritance and composition? Why may the latter be preferred?
Inheritance allows a class to define itself based on a parent class, describes IS-A relationships.
Composition allows a class to fulfil a contract based on an interface, describes HAS-A relationships.
Composition is usually preferred due to its modularity. Changes made in base classes require many changes to derived ones, but is limited with interfaces.
What are preprocessor directives? List some examples.
A preprocessor directive is an instruction that is examined before the compiler begins the compilation process. Some examples include:
#define - Creates a macro. Wherever used, it will be directly replaced with its content.
#if - Given a condition, can execute a specific piece of code. Useful for platform-specific code or handling starting conditions.
#pragma - Special instructions (e.g. #pragma once used as an include guard).
List three C++ compilers.
MSVC - Developed by Microsoft for use with Visual Studio for Windows development.
GCC - GNU open source compiler, mostly used for Unix development.
Clang/LLVM - Open source compiler providing support for multiple languages.
List 3 C++11 features.
Lambdas - allow for local function definition
Automatic type deduction (via ‘auto’ keyword) and ‘decltype’
Uniform brace initialisation.
‘nullptr’ keyword, preferred over NULL as it cannot be cast to zero
Delegating constructors - call one constructor from another
Rvalue references
Smart pointers