Sealed classes and methods: Difference between revisions
Content added Content deleted
(Removed mention of private methods in description) |
(Update C++ entry) |
||
Line 84: | Line 84: | ||
public: |
public: |
||
explicit MovieWatcher(std::string_view name) : m_name{name}{} |
explicit MovieWatcher(std::string_view name) : m_name{name}{} |
||
⚫ | |||
virtual void WatchMovie() = 0; // must be overridden by derived classes |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
virtual void EatPopcorn() |
virtual void EatPopcorn() |
||
{ |
{ |
||
Line 97: | Line 102: | ||
public: |
public: |
||
explicit ParentMovieWatcher(std::string_view name) : MovieWatcher{name} {} |
explicit ParentMovieWatcher(std::string_view name) : MovieWatcher{name} {} |
||
}; |
|||
⚫ | |||
⚫ | |||
{ |
|||
public: |
|||
explicit ChildMovieWatcher(std::string_view name) |
|||
: MovieWatcher{name}{} |
|||
// EatPopcorn() cannot be overridden because it is 'final' |
|||
⚫ | |||
⚫ | |||
{ |
{ |
||
std::cout << m_name << " is |
std::cout << m_name << " is eating too much popcorn\n"; |
||
} |
} |
||
}; |
}; |
||
class YoungChildMovieWatcher : public ChildMovieWatcher |
|||
⚫ | |||
⚫ | |||
{ |
{ |
||
int m_age; |
|||
public: |
public: |
||
explicit YoungChildMovieWatcher(std::string_view name) |
|||
: |
: ChildMovieWatcher{name}{} |
||
// WatchMovie() cannot be overridden because it is 'final' |
// WatchMovie() cannot be overridden because it is 'final' |
||
void WatchMovie() final override |
void WatchMovie() final override |
||
{ |
{ |
||
std::cout << "Sorry, " << m_name << |
|||
std::cout << "Sorry, " << m_name << |
|||
", you are too young to watch the movie.\n"; |
", you are too young to watch the movie.\n"; |
||
else |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
if (m_age < 15) |
|||
std::cout << m_name << " is eating popcorn in the lobby\n"; |
|||
else |
|||
std::cout << m_name << " is eating too much popcorn\n"; |
|||
} |
} |
||
}; |
}; |
||
Line 133: | Line 134: | ||
int main() |
int main() |
||
{ |
{ |
||
// A container for the MovieWatcher base class objects |
|||
std::vector<std::unique_ptr<MovieWatcher>> movieWatchers; |
std::vector<std::unique_ptr<MovieWatcher>> movieWatchers; |
||
// Add some movie wathcers |
|||
movieWatchers.emplace_back(new ParentMovieWatcher("Donald")); |
movieWatchers.emplace_back(new ParentMovieWatcher("Donald")); |
||
movieWatchers.emplace_back(new ChildMovieWatcher("Lisa" |
movieWatchers.emplace_back(new ChildMovieWatcher("Lisa")); |
||
movieWatchers.emplace_back(new |
movieWatchers.emplace_back(new YoungChildMovieWatcher("Fred")); |
||
// Send them to the movies |
// Send them to the movies |
||
Line 151: | Line 155: | ||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Donald is watching the movie |
Donald is watching the movie |
||
Lisa |
Lisa is watching the movie |
||
Sorry, Fred, you are too young to watch the movie. |
Sorry, Fred, you are too young to watch the movie. |
||
Donald is enjoying the popcorn |
Donald is enjoying the popcorn |
||
Lisa is eating too much popcorn |
Lisa is eating too much popcorn |
||
Fred is eating |
Fred is eating too much popcorn |
||
</pre> |
</pre> |
||