Drill 16: Improve it — Inheritance and polymorphism
Add one improvement and explain the benefit.
What you will be able to do
- Add one improvement and explain the benefit.
Practice task
Add one improvement and explain the benefit.
#include <iostream>
#include <memory>
#include <string>
#include <vector>
class Lesson {
public:
virtual ~Lesson() = default;
virtual std::string describe() const = 0;
};
class Video : public Lesson {
public:
std::string describe() const override {
return "video lesson";
}
};
class Quiz : public Lesson {
public:
std::string describe() const override {
return "quiz lesson";
}
};
int main() {
std::vector<std::unique_ptr<Lesson>> lessons;
lessons.push_back(std::make_unique<Video>());
lessons.push_back(std::make_unique<Quiz>());
for (const auto& lesson : lessons) {
std::cout << lesson->describe() << "\n";
}
}Your turn
EasyWrite your answer
Add one improvement and explain the benefit.
TEXT · starter
Your answer…Study tools
Make it yours.
Keep a private note for this lesson, then turn the key ideas into flashcards you can revisit.
Sign in to save notes and flashcards.Both are private to your account and stay with your progress.
Sign inFinished reading?
Lock in what you just learned.
0%
Mark this lesson complete to save your place and move one step closer to the certificate.