Congratulations! You have been appointed as Chief Technology Officer for CatBook, a new social media start-up which allows users to post pictures of their cats.
A user's news feed consists of cat pictures. There are two types of pictures that CatBook supports: cute cats and grumpy cats.
-
Write an abstract base class
CatPicture
with a pure virtual member functionstd::string get_description() const
. -
Write a class
CuteCatPicture
which inherits fromCatPicture
. Its constructor should take an argument which is astd::string
of the cat's name. It should implementget_description()
, returning the stringA picture of
<cat name>
looking adorably cutewhere
<cat name>
was the name provided to theCuteCatPicture
constructor. -
Write another similar class
GrumpyCatPicture
which again inherits fromCatPicture
, and again allows you to pass the cat name to its constructor. This time, the implementation ofget_description()
should returnA picture of
<cat name>
looking really grumpy
Hooray, CatBook is starting to attract users! It's time to take this opportunity
to improve your code. The CuteCatPicture
and GrumpyCatPicture
subclasses do
very similar things: you can move some of this common code into the CatPicture
base class.
-
Add a constructor to the
CatPicture
base class which takes astd::string
and stores it in a field namedcat_name
. Add a member functionget_cat_name()
which only derived classes may call. -
Refactor your
CuteCatPicture
andGrumpyCatPicture
subclasses to call the new base class constructor. Use the newget_cat_name()
function in each implementation ofget_description()
. -
Everybody likes cat pictures. Add a new member function
like()
which, when called, increments thelike_count
of a picture. Add a member functionget_num_likes()
which returns a picture's currentlike_count
.
Oh no, your investor funding has run out! CatBook needs to make money, fast.
We need to remodel the code so that body CatPicture
s and Advert
s can be
displayed in the news feed.
-
Add a new abstract base class
NewsItem
which represents an item in the news feed. This class should have aget_item_text()
pure virtual member function. -
Make
CatPicture
inherit fromNewsItem
, and implementget_item_text()
appropriately. -
Write an
Advert
class which takes astd::string slogan
in its constructor. Implement theget_item_text()
function so that it returns theslogan
.