Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
|
C# Programming questions specific to the Microsoft C# language. See also the forum Beginning Visual C# to discuss that specific Wrox book and code.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old July 27th, 2004, 03:23 PM
Authorized User
 
Join Date: Jul 2004
Posts: 59
Thanks: 0
Thanked 0 Times in 0 Posts
Default Beginning Visual C# Exercises - Chapter 09

1. Sealed classes can not serve as a base class (no inheritance)

2. The constructor is declared private preventing object instances of the class

3. The class acts as a container for static members allowing global access to each property,
            method and field without bounds of object encapsulation
        Once the namespace is set up, static members of the class may be called throughout the code
            as when invoking methods of the Console class from the System namespace

4. using System;

        namespace Vehicles
        {
            public interface iPassengerCarrier
            {
            }
            public interface iHeavyLoadCarrier
            {
            }
            public class VehicleType
            {
            }
            public class Train : VehicleType
            {
            }
            public class PassengerTrain : Train, iPassengerCarrier
            {
            }
            public class FreightTrain : Train, iHeavyLoadCarrier
            {
            }
            // Note: 424DoubleBogey is an illegal C# name
            public class DoubleBogey : Train
            {
            }
            public class Car : VehicleType
            {
            }
            public class Compact : Car, iPassengerCarrier
            {
            }
            public class Pickup : Car, iPassengerCarrier, iHeavyLoadCarrier
            {
            }
            public class SUV : Car, iPassengerCarrier
            {
            }
        }

5. using System;
        using Vehicles;

        namespace Traffic
        {
            class Class1
            {
                public static int census;

                private static void AddPassenger(object accomodation)
                {
                    try
                    {
                        // Cast a vehicle class into the passenger interface
                        iPassengerCarrier transport = checked((iPassengerCarrier)accomodation);
                        // Only vehicle objects derived from passenger interface arrive here
                        Console.WriteLine(transport.ToString() + " will carry " +
                            Convert.ToString(census) + " passengers.");
                    }
                    catch (System.InvalidCastException)
                    {
                        // The vehicles without passenger interface, so ignore
                    }
                    catch (Exception e)
                    {
                        // Report an unanticipated exception
                        Console.WriteLine(e.ToString());
                    }
                }

                [STAThread]
                public static void Main(string[] args)
                {
                    Compact carCompact = new Compact();
                    census = 5;
                    AddPassenger(carCompact);

                    FreightTrain boxCar = new FreightTrain();
                    census = 15;
                    AddPassenger(boxCar);

                    PassengerTrain railCar = new PassengerTrain();
                    census = 120;
                    AddPassenger(railCar);

                    SUV miniVan = new SUV();
                    census = 8;
                    AddPassenger(miniVan);

                    Pickup farmHaul = new Pickup();
                    census = 3;
                    AddPassenger(farmHaul);

                    DoubleBogey monsterCar = new DoubleBogey();
                    census = 1000;
                    AddPassenger(monsterCar);
                }
            }
        }





Similar Threads
Thread Thread Starter Forum Replies Last Post
Beginning Visual C# Exercises - Chapter 18 seblake C# 1 October 9th, 2004 11:33 PM
Beginning Visual C# Exercises - Chapter 12 seblake C# 1 August 21st, 2004 02:55 PM
Beginning Visual C# Exercises - Chapter 07 seblake C# 0 July 20th, 2004 02:07 PM
Beginning Visual C# Exercises - Chapter 06 seblake C# 1 July 19th, 2004 09:15 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.