Quiz: Abstract Classes and the Clone Pattern

Module 4 of 8 · Medium

Quick Quiz

1. You have an abstract base class `Model` with a `virtual Model* clone() const = 0;` method. A `PathSimulator` stores `const Model* _model`. In the `PathSimulator` copy constructor, which of the following correctly performs a deep copy of the model?

2. The following code compiles successfully: ```cpp Distribution d("base"); // Distribution has a pure virtual pdf() d.cdf(0.5); // cdf() is non-virtual ```

3. What is **object slicing** in the context of a `Distribution` hierarchy?

4. The `clone()` method returns `NormalDistribution*` (not `Distribution*`) in the `NormalDistribution` override. This is called a **covariant return type**. What is the primary benefit?

5. If `Distribution` has a `virtual ~Distribution() = default;` destructor, then `delete d_ptr;` where `d_ptr` is a `Distribution*` pointing to a `NormalDistribution` object will correctly invoke `NormalDistribution`'s destructor.

6. In the copy assignment operator for `NormalDistribution`, why must you call `Distribution::operator=(other)` explicitly?