Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Microsoft CSharp Programming For The Absolute Beginner (2002) [eng]-1.pdf
Скачиваний:
46
Добавлен:
16.08.2013
Размер:
15.71 Mб
Скачать

while (keepGoing){ myCritter.age(); choice = showMenu(); switch (choice){

case 0:

keepGoing = false; break;

case 1: Console.WriteLine(myCritter.Talk()); break;

case 2: myCritter.Eat();

Console.WriteLine ("You have fed the critter"); break;

case 3: myCritter.Play();

Console.WriteLine("You have played with the critter"); break;

case 4:

Console.WriteLine("Current name: {0}", myCritter.Name); Console.Write("Change name to: ");

myCritter.Name = Console.ReadLine(); break;

default:

Console.WriteLine("That was not a valid input"); break;

} // end switch

} // end while loop

}// end main

The main program still reports the user’s actions, but all the real work is delegated to methods of the Critter class. Each time through the loop, the program calls the critter’s Age() method, and then each menu item results in a call to the appropriate method of the Critter class. If the user wants to feed the critter, the menu calls the critter’s Eat() method. If the user wants to play with the critter, the menu calls the critter’s Play() method. The code that deals with user interaction happens in the Menu class, and the code that deals with the details of the critter’s behavior goes in the Critter class. Encapsulation is great.

Summary

In this chapter you looked at several forms of encapsulation. You learned about dividing a program into manageable pieces, using several strategies. You built methods to encapsulate code fragments and saw how those methods can receive values through parameters and output values with the return statement. You learned how to build basic classes to encapsulate data and methods and how to make an instance of a class. You added methods and properties to your classes and learned some basic exception handling. With mastery of these skills, you are no longer simply a programmer. You are well on your way to becoming an object−oriented programmer.

Challenges

Modify the program so that it shows the critter’s age. Give a special reward to the owner who can keep a critter happy for a certain number of turns.

Add another method to the critter, such as tickle() or sleep(). Determine how this method would relate to the critter’s variables. Don’t forget to change the menu to take advantage of your new method.

99

Add a cash variable. Maybe decrease the amount of money in your account each time the user feeds the critter, but award cash for longevity milestones.

Create the critter’s capability to do tricks. For example, if the critter reaches a certain level of happiness, a new trick or method is made available. You might combine this with a cash variable so a critter that can do fancy tricks can earn his own keep. (If only they paid cats for scratching couches...)

Modify the program to make a virtual version of one of your pets. (One person I know sent out object−oriented birth announcements to his programming friends!)

100

Chapter 5: Constructors, Inheritance, and

Polymorphism: The Snowball Fight

With the ability to create custom objects, you begin your adventure into object−oriented programming (OOP). However, you have yet to learn some other very important characteristics of objects. In this chapter you will explore the essential techniques of OOP and learn how

To write a constructor to customize the way a class is instantiated

To overload constructors for flexibility

Inheritance is used to reuse code

Polymorphism is used in OOP

Introducing the Snowball Fight

With surprisingly little work, you can use objects to model complex behavior. In this chapter you will make a model of a snowball fight. Each player is an object, and the menu system is a third object. Figures 5.1, 5.2, and 5.3 illustrate the snowball fight in progress.

Figure 5.1: Begin by entering the player names, which are important for the play−by−play description of the game.

Figure 5.2: Players have a limited number of snowballs and are more likely to hit their target when they are close to it.

101

Соседние файлы в предмете Программирование на C++