Exercise: Inheritance

Finish a café menu's Drink and Pastry classes so they inherit from MenuItem, with constructors that call base(...), an overridden Price and an overridden ToString().

Apprentice · C# · OOP · Updated

A café's menu has plain items, such as a muffin at $2.50, and two special kinds: drinks, priced by size, and pastries, which can be warmed up. MenuItem is written. You'll finish Drink and Pastry so they build on it instead of repeating it.

Get the exercise code

Open this lesson's GitHub repository, github.com/ask-codey/exercise-inheritance, also linked from the Exercise box beside the lesson. Click Code, then Download ZIP, and unzip it into your bootcamp folder. Or clone it with Git. Then open CafeMenu.slnx in Visual Studio.

File What's in it
CafeMenu/MenuItem.cs The base class: Name, BasePrice, Price, PriceFor(quantity) and ToString(). Read it first.
CafeMenu/Drink.cs A drink in a size: Small, Medium (+$0.50) or Large (+$1.00). For you to finish.
CafeMenu/Pastry.cs A pastry, which costs $0.75 more when warmed. For you to finish.
CafeMenu.Tests Tests for Drink and Pastry

What to do

  1. The constructors. Each derived class's constructor passes "" and 0 to MenuItem for now. Pass the real name and base price on with base(...), and keep the class's own value.
  2. Price. A drink is priced by its size, and a warmed pastry costs more. Let MenuItem allow its Price to change, and change it in each derived class.
  3. ToString. A drink's menu line starts with its size (Large Latte, $5.00), and a warmed pastry's ends with (warmed) (Croissant, $3.75 (warmed)). Reuse MenuItem's line rather than writing it again.

Run the program with Ctrl+F5 to see the menu, and the tests with Test > Run All Tests. At first every test fails; each step turns more of them green.

Hints

  • virtual first. Only a virtual member can be overridden, and a property can be virtual and override just like a method.
  • base.ToString() returns MenuItem's line, already with the right price, because it uses Price, and a drink's Price is the drink's own.
  • Leave PriceFor alone. Once Price is overridden, it works for drinks and pastries without a change.
  • One test holds a drink in a MenuItem variable and checks its price. If that one fails while the others pass, look for a missing override.

When you're done

Every test passes, and the program prints:

Today's menu
Blueberry Muffin, $2.50
Large Latte, $5.00
Croissant, $3.75 (warmed)

Notice how little Drink and Pastry hold: a constructor, one value and two overrides. Everything else comes from MenuItem.

Submit your exercise

The exercise is how you finish this lesson. When your work is done, put it on GitHub in a repo of your own, then submit that repo's address here.

  1. On GitHub, click +, then New repository. Name it (for example exercise-inheritance), choose Public, and click Create repository. Leave out the README: your folder already has one.

  2. In a terminal in your exercise folder (Git Bash on Windows), send your work to your new repo. If you downloaded the ZIP:

    git init -b main
    git add .
    git commit -m "My solution"
    git remote add origin https://github.com/your-name/exercise-inheritance.git
    git push -u origin main
    

    If you cloned the starter instead, point your copy at your own repo, then commit and push:

    git remote set-url origin https://github.com/your-name/exercise-inheritance.git
    git add .
    git commit -m "My solution"
    git push -u origin main
    
  3. Paste your repo's address, such as https://github.com/your-name/exercise-inheritance, into Submit your exercise on this page. The site checks that the repo is public and holds your commits, and then the lesson is complete.

Changed something after you submitted? Push it, and submit again: your latest submission is the one that counts.

So what

That's inheritance doing its job: shared code in one base class, and each derived class holding only what makes it different. In the next chapter, a whole order of mixed items is priced by the same loop.

Key terms

Related