Creating a New Object with the CritterName Program

You have begun to understand the benefits of OOP, but the real fun begins when you create your own objects. Although making the Critter program work without using OOP principles is possible, the point of this chapter is to show how to create objects, so that’s what you do next.

Figure 4.10 shows the output of the Critter Name program you create in this section.

Figure 4.10. This version of theCritter programfeatures a critterthat knows itsname.


Creating the Basic Critter

A critter is an object. Objects, as you recall, can have properties, methods, and events. An object has a class (which is like a recipe–it defines the cookie but isn’t a cookie) and instances (the cookies made with the recipe). To create a critter object, you have to create a class and make an instance of that class. You can start with a very simple version of the Critter class. Later in the chapter you’ll give it more capabilities, such as changing its name and talking. Here’s the code for the simpler Critter class:

using System;

namespace CritterName {
  /// <summary>
  /// Critter Name
  /// Creating a simple class
  /// Andy Harris, 12/13/01
  /// </summary>

  class Menu {
    static void Main(string[] args) {
      …
    } // end main

    static int showMenu(){
      …
    } // end showMenu
  } // end class

  class Critter {
    public string name;
  } // end class

 } // end namespace

Note that I collapsed the Main() and showMenu() methods in this code listing– because there are very few changes in the code (I’ll show you those changes shortly) and because I wanted you to see the general structure of the program. For the first time, your program has more than one class in it. You can add as many classes to a project as you want. The Menu class is largely the same, but the Critter class is new. You create a new class with the class keyword, followed by the name of the class and a pair of braces ({ }). Classes do not have parentheses because they do not accept parameters.

The properties, methods, and events a class owns are collectively known as members. This version of the Critter class has only one member, name.

Using Scope Modifiers

A scope modifier is a special word used to determine whether a method or variable is visible to other methods and classes.

When you look back at the code for the Critter class above, the definition of the variable name is unique. It looks like most variable definitions, except that it starts with the keyword public. This term indicates that the variable name should be available to elements both inside and outside the Critter class. As you’ll see momentarily, this is not such a great idea, but I wanted to show you the simplest kind of class first.

The private keyword is an example of a scope modifier. C# supports a number of scope modifiers, but for now, you need to know only two. If you want a variable to be available outside the class, you declare it as public, as I did with name in this example. If you want the variable to exist only inside the class, you either declare the variable private or leave out a scope modifier (because the default scope is private).

Using a Public Instance Variable

Because the name variable is declared inside the Critter class and is used by instances of that class, it’s an example of an instance variable. Instance variables are variables defined inside a class but outside any method definitions. All this terminology seems overwhelming, but you’ll run across it later, so you had better learn the lingo. In any case, the formal description of name would be “a public instance variable of the Critter class.” Right now, all the critter object has is this one public instance variable. This makes the class very simple to build, but it has some limitations. You’ll improve the Critter class throughout the chapter, but at least it works for now, so you can concentrate on how to use a custom class inside your programs.

Creating an Instance of the Critter

The Main() method of the CritterName program is almost identical to that of the Critter Menu program, except that I added two lines before the while loop:

static void Main(string[] args) {
  bool keepGoing = true;
  int choice;

  Critter myCritter = new Critter();
  myCritter.name = "George";

  while (keepGoing){
    …
  } // end while loop
} // end main

Again, I removed some of the code (specifically, the while loop) so that you can concentrate on the part of the program that is new. The complete code is available on the CD-ROM accompanying this book.

The line containing new Critter() creates a new instance of the critter object and assigns it to a variable named myCritter. After you define a class, it is essentially a new variable type, and you can create a variable of that type. The new keyword informs the computer that you will be making an instance of the Critter() class. Critter is the class (or cookie recipe), and myCritter is an instance (a cookie) of that class. In most situations, you work not with the class but with instances of the class. Generally, whenever you create a custom class, you also make an instance (or several instances) of that class. In the next line, you’ll see that you can assign a value to myCritter.name. In fact, when you type myCritter into the IDE, a pop-up menu appears, listing all the possible members of myCritter. You can choose name from this automatic menu in the same way you choose WriteLine or ReadLine from the console object’s automatic menu system.

Referring to the Critter’s Members

I also made a modification to the switch because I wanted the critter to say its name when you talk to it. Take a look at the appropriate section of the switch statement in the Main() method:

case 1:
   Console.WriteLine("The critter says: my name is {0}", myCritter.name); 
   break;

This code refers to the name member of the critter object. After you define an object and add members to it, using the object is easy. You don’t have to know exactly how the member is defined or works. You simply refer to it. This is another example of the joys of encapsulation.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.221.126.56