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 WatchMovie()
{
std::cout << m_name << " is watching the movie\n";
}
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} {}
};

// 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 watching the movie...\n";
std::cout << m_name << " is eating too much popcorn\n";
}
}
};
};


class YoungChildMovieWatcher : public ChildMovieWatcher
// ChildMovieWatcher can be inherited from
class ChildMovieWatcher : public MovieWatcher
{
{
int m_age;
public:
public:
ChildMovieWatcher(std::string_view name, int age)
explicit YoungChildMovieWatcher(std::string_view name)
: MovieWatcher{name}, m_age{age}{}
: 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
{
{
if (m_age < 15)
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
std::cout << m_name << " likes the movie\n";
}
void EatPopcorn() override
{
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", 18));
movieWatchers.emplace_back(new ChildMovieWatcher("Lisa"));
movieWatchers.emplace_back(new ChildMovieWatcher("Fred", 10));
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 likes the movie
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 popcorn in the lobby
Fred is eating too much popcorn
</pre>
</pre>