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