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:

List of toys bought

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:

Creating collection of toys

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:

List of toys including cuddly toy

The revised list now includes a prefix in upper case for each cuddly toy bought.

 

You've now cracked object-oriented programming!

This page has 0 threads Add post