WISE OWL EXERCISES
C# EXERCISES
VISUAL C# EXERCISES
- Creating forms (4)
- Coding form events (1)
- Laying out your code (2)
- C# variables (4)
- Enumerations and constants (2)
- Conditions (2)
- Modular code (3)
- Arrays (2)
- Looping (2)
- Files and folders (3)
- Properties in C# (3)
- Using lists (3)
- Validating forms (6)
- Toolbars, menus and status bars (1)
- FileDialogs and StreamReaders (1)
- Debugging and trapping errors (1)
- Introduction to DataGridViews (1)
- DataGridView events (3)
- Complex DataGridViews (2)
- Creating classes (4)
- The form as a class (1)
- Data structures (6)
- Inheritance (5)
- Interfaces (2)
- Delegates and events (2)
- Writing LINQ (2)
- Advanced LINQ (2)
- Entity Frameworks (1)
- LINQ with Entity Frameworks (4)
- Grouping using LINQ (2)
- LINQ to SQL (2)
Visual C# | Inheritance exercise | Create a toy class, and a cuddly toy class to inherit it
This exercise is provided to allow potential course delegates to choose the correct Wise Owl Microsoft training course, and may not be reproduced in whole or in part in any format without the prior written consent of Wise Owl.
The answer to the exercise will be included and explained if you attend the relevant Wise Owl course (sadly, only in the UK for now).
Software ==> | Visual C# (79 exercises) |
Version ==> | Any version of C# |
Topic ==> | Inheritance (5 exercises) |
Level ==> | Average difficulty |
You need a minimum screen resolution of about 700 pixels width to see our exercises. This is because they contain diagrams and tables which would not be viewable easily on a mobile phone or small laptop. Please use a larger tablet, notebook or desktop computer, or change your screen resolution settings.
Create a simple form containing a single button. When you click on the button, it should run the following code to "buy" two toys and display your "purchases" in a message::
// buy a couple of normal toys
Toy t1 = new Toy("Games console", 99.99);
t1.Buy();
Toy t2 = new Toy("Lego set", 11.99);
t2.Buy();
string s = "";
foreach (Toy t in Toy.Toys)
{ s += t.Description + "\n"; }
MessageBox.Show(s.Trim(),"Purchases");
When you run this code, you should get the following message:

The code concatenates together the descriptions of all the toys bought.
See below for how to do this!
To get started, create a new Toy class and add into it a static list containing the toys bought:

The start of your toy class.
Add the following members to this class:
Type of member | Name | How used |
---|---|---|
Read-write property | Price | The price of the toy |
Read-write property | What | The name of the toy |
Read-only property | Description | The name of the toy followed by the price in brackets |
Method | Buy | This should add this toy to the list of toys |
Add a constructor for this class which takes in the toy name and price as arguments, then write and run the code shown at the start of this exercise to check everything you've done so far works.
Now add a CuddlyToy class such that the following code works:
// buy a couple of normal toys
Toy t1 = new Toy("Games console", 99.99);
t1.Buy();
Toy t2 = new Toy("Lego set", 11.99);
t2.Buy();
// buy a cuddly toy (saying what type it is)
CuddlyToy t3 = new CuddlyToy("Flopsy rabbit", 5.99);
t3.Animal = "Rabbit";
t3.Buy();
string s = "";
foreach (Toy t in Toy.Toys)
{
s += t.Description + "\n";
}
MessageBox.Show(s.Trim(),"Purchases");
Your CuddlyToy class should inherit from the Toy class. You'll need to add a constructor to it which runs the base constructor, add a write-only Animal property and override the Description property in the base class!
Check that when you run your system now, you get a longer list:

The revised list now includes a prefix in upper case for each cuddly toy bought.
You've now cracked object-oriented programming!