Read our blogs, tips and tutorials
Try our exercises or test your skills
Watch our tutorial videos or shorts
Take a self-paced course
Read our recent newsletters
License our courseware
Book expert consultancy
Buy our publications
Get help in using our site
551 attributed reviews in the last 3 years
Refreshingly small course sizes
Outstandingly good courseware
Whizzy online classrooms
Wise Owl trainers only (no freelancers)
Almost no cancellations
We have genuine integrity
We invoice after training
Review 30+ years of Wise Owl
View our top 100 clients
Search our website
We also send out useful tips in a monthly email newsletter ...
Software ==> | Visual C# (55 exercises) |
Topic ==> | Inheritance (5 exercises) |
Level ==> | Average difficulty |
Subject ==> | C# training |
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.
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!
You can find other training resources for the subject of this exercise here:
Kingsmoor House
Railway Street
GLOSSOP
SK13 2AA
Landmark Offices
99 Bishopsgate
LONDON
EC2M 3XD
Holiday Inn
25 Aytoun Street
MANCHESTER
M1 3AE
© Wise Owl Business Solutions Ltd 2024. All Rights Reserved.