CPP Practice Exercises
Based off of Sam's Teach Yourself CPP Eighth Edition
About:
- Compiled with C++17 Standard
TODO:
- Implement
move constructor
(p. 245) - Study different uses of the
static
keyword (p.247)
Notes:
Virtual Keyword:
- A function declared virtual means that an existing overriding function in a derived class is invoked.
- A virtual destructor is needed on a base class to ensure the derived class destructor gets called
- Virtual functions create a hidden pointer to a Virtual Function Table (VFT) on the classes that use them
- The sizeof an instance is larger with a class that implements a VFT
- You can create an Abstract Base Class (ABC) with the following syntax:
class AbstractBase
{
public:
virtual void DoSomething() = 0;
};
class Derived: public AbstractBase
{
public:
void DoSomething() { cout << "Implement virtual function" endl; }
};
- An ABC can never be instantiated directly and needs to have the Derived class supply an implementation for the virtual function
- An inheritance relationship declared using the virtual keyword between classes
Derived1
andDerived2
that inherits from classBase
means that another classDerived3
that inherits fromDerived1
andDerived2
still results in the creation of only one instance ofBase
during the instantiation ofDerived3
.