Quiz: Inheritance and Virtual Dispatch

Module 3 of 8 · Medium

Quick Quiz

1. Given `Matrix* p = new SquareMatrix(3, data);`, which method body is invoked by `p->name()` when `name()` is declared `virtual` in `Matrix` and overridden in `SquareMatrix`?

2. What is the vtable (virtual dispatch table) and when is it set up?

3. A `SquareMatrix` derived class declares `std::string Name() const;` (capital N) intending to override `virtual std::string name() const;` in `Matrix`. Without `override`, the compiler:

4. The following code is well-defined: `Matrix* p = new SquareMatrix(3, data); delete p;` — provided `Matrix` declares `virtual ~Matrix() = default;`.

5. What is object slicing and what is the correct way to avoid it in a polymorphic quant model hierarchy?

6. If `Matrix::Matrix()` calls a virtual method `computeRank()`, and `SquareMatrix` overrides `computeRank()`, then during construction of a `SquareMatrix` object the call will invoke `SquareMatrix::computeRank()`.