C++ Basics and OOP Flashcards

Learn the basics of the C++ language and Object Oriented Programming

1
Q

What is the difference between an object and a class?

A

A class is a blueprint that specifies the methods, variables and their visibility. An object is an instance of that blueprint.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Briefly describe abstraction and why we use it in OOP?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is encapsulation?

A

Encapsulation is about binding related variables and methods together. This is achieved in OOP with classes.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is inheritance? Why use it?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is polymorphism in OOP? What are the two types available?

A

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).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is the difference between method overloading and overriding?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is the difference between method overriding and hiding?

A

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().

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What are virtual and abstract (pure virtual) methods?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What are the steps in compiling a program?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Why are C++ files typically split up into header (.h) and source files (.cpp)?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is a forward declaration? List two useful features they can provide.

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is the difference between inheritance and composition? Why may the latter be preferred?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What are preprocessor directives? List some examples.

A

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).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

List three C++ compilers.

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

List 3 C++11 features.

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

List 3 C++14 features.

A

Automatic return type deduction.
Generic lambdas - can use ‘auto’ for parameters in lambda arguments.
[deprecated] tag.
Binary literals (given by prefix 0b, followed by binary digits).
Digit separators (‘) to improve readability.

16
Q

List 3 C++17 features.

A

Nested namespaces (e.g. namespace Game::Graphics::Physics).
Constexpr if - this allows conditional branches to be pruned at compile time. This is useful for template functions.
Fold expressions - simplifies code for aggregate operations on variadic arguments.
Inline variables - similar to inline functions. Signals to linker that only one instance of the variable should exist across multiple compilation units. Useful for creating global variables in header files.
Switch / if statement init - you can now initialise a variable before doing a comparison.

17
Q

List 3 C++20 features.

A

3-way comparisons - uses form (x <=> y) - it essentially compares to objects then compares thatresult to zero.
Modules - they promise to do away with separate header and source files and improve compilation times.
Range-based for loop with initialisation - allows variable initialization before running loop.
Likely/unlikely attributes - optimizer hint on how likely a code body is going to be executed.