A Class


A class is a template that defines attributes and methods of a real-word object. Think of a class as a cookie cutter of the letter A . The cookie cutter isn t the letter A but rather defines what the letter A looks like. If you want a letter A , then place the cookie cutter on a cookie sheet of dough. If you want to make another letter A , you use the same cookie cutter and repeat the process. You can make as many letter A s as you wish by using the cookie cutter.

The same is true about a class. When you want an object represented by the class, you create an instance of the class. An instance is the same as the letter A appearing on the cookie sheet of dough after you remove the cookie cutter.

Each instance contains the same attributes and methods that are defined in the class, although each instance has its own copy of those attributes. Instances use the same methods. Going a bit crackers with this explanation? Let s go to the cookie cutter to cut through the confusion. Remember that instance is another word for saying a real cookie that is defined by the cookie cutter (class template). Suppose the cookie cutter is in the shape of a dog. The dog s legs are of a particular width and length, as is the dog s tail. These are attributes of the dog. Each time the cookie cutter cuts into the dough, another dog cookie is created (instance). Say that you make two dog cookies this way. Each dog cookie (instance) has legs and a tail of the same width and length, but each has its own set (copy) of legs and a tail that is independent of other dog cookies.

A method is a behavior that is performed by a dog cookie. Okay, a dog cookie doesn t really do anything but sit quietly in your hand, so we ll have to use our imagination and pretend that the dog cookie can stand up on its legs only if you and you alone tell it how to stand up. The dog cookie ignores everyone else s instructions. Each dog cookie uses the same copy of the method (that is, your instructions) to perform the behavior of standing up on its legs.

Defining a Class

A class is defined in a class definition. A class definition defines attributes and methods that are members of the class. The form used to define a class is dependent on the programming language used to write the program. Here s a simple class definition using Java:

 class RegistrationForm { 
int studentNumber;
int courseNumber;
}

And here s the same class definition using C++ (notice that a semicolon must follow the closing brace ; otherwise , you ll receive a compiler error):

 class RegistrationForm { 
int studentNumber;
int courseNumber;
};

A class definition has three parts :

  • Keyword class

  • Class name

  • Class body

The keyword class tells the compiler that you are defining a class. A keyword (also known as a reserved word) is a word that has special meaning to the programming language. The class name is a symbol given by the programmer that uniquely identifies a class from another class. The name of a class should relate to the real-world object that it is emulating, and the first letter of the name should be capitalized. In the previous example, the class name is RegistrationForm and represents a form used to register students for classes.

The class body is the portion of the class definition that is identified by open and close braces. Attributes and methods are defined within those braces. Two attributes are defined in this example ”a student number and a course number. We ll include methods in the class later in this chapter.

Attributes

You probably remember from your programming course that a variable name is a reference to a memory location where you can store data. An attribute of a class is a variable called an instance variable. An instance variable references a memory address within the block of memory reserved for an instance. You can use an instance variable to store data in memory. It is called an instance variable because it is in the attribute portion of the class instance.

Memory is reserved by using a statement in a class definition that declares an instance variable. A statement is a computer instruction. Here is a declaration statement that declares two instance variables :

 class RegistrationForm { 
int studentNumber;
int courseNumber;
}

A variable and an instance variable are very similar, with a few exceptions. A variable is declared in a declaration statement within a program. Memory is reserved when the declaration statement executes. An instance variable is declared in a class definition. However, memory is reserved only when an instance is declared because a class definition is a template, and an instance is the computer s version of a real object.

Declaring an Instance Variable

An instance variable is declared within a class definition using a declaration statement. The form of a declaration is dependent on the programming language used to write the program. A declaration statement in Java or C++ consists of the following three parts, as illustrated in the class definitions shown earlier in this chapter:

  • Data type

  • Instance variable name

  • Semicolon

Data Type

A data type is a keyword that tells the computer the kind of data you want to store in a memory location. The data type implies to the computer how much memory to reserve and how to handle the data stored once it is stored at that memory location.

Data types can baffle even professional programmers, so you re in good company if you are a little intimidated by the term data type . However, it is very important that you have a firm understanding of what a data type is and how to specify a data type when declaring a variable.

Think of a data type as the term case of baseballs. You call the warehouse manager and say that you need to reserve enough space to hold one case of baseballs (see Figure 2-2). The warehouse manager knows how much space to reserve because he knows the size of a case of baseballs.

click to expand
Figure 2-2: A data type is similar to the term a case of baseballs because you and the warehouse manager know the size of a case of baseballs.

The same is true about a data type. You tell the computer to reserve space for an integer by using the data type int . The computer already knows how much memory to reserve to store an integer.

The data type also tells the computer the kind of data that will be stored at the memory location. This is important because computers manipulate data of some data types differently than data of other data types. This is similar to the warehouse manager who treats a case of fuel oil differently than a case of baseballs.

Here s another example: Suppose you want to store the number 1 in memory. You must decide whether the number 1 is an integer or a character. An integer can be used in arithmetic, whereas a character (such as the numbers in a house address) cannot be used in arithmetic until the character is converted to an integer. The data type tells the computer whether a number is an integer or a character. The computer then knows to use the appropriate steps to manipulate the data.

Each programming language has its own set of data types, commonly referred to as primitive data types, because they are basic data types. Table 2-1 contains primitive data types common to many programming languages.

Table 2-1: Primitive Data Types

Data Type

Range of Values

byte

“128 to 127

short

“32,768 to 32,767

int

“2,147,483,648 to 2,147,483,647

long

“9,223,372,036,854,775,808 to 9,223,372,036,854,775,807

char

65,000 (Unicode)

float

3.4e “038 to 3.4e+038

double

1.7e “308 to 1.7e+308

boolean

true or false

Instance Variable Name

The name of an instance variable is a symbol given to that instance variable by a programmer, and it should represent the nature of the data stored at the memory location. For example, studentNumber is a perfect name for a variable used to store a student number because the name tells you the nature of the data associated with the variable. The variable name is used throughout the program to refer to the contents of the corresponding memory location.

The declaration statement must end with a semicolon in Java and C++. Otherwise, an error message is displayed when you try building the program.

Methods

Real-world objects have behaviors such as dropping a course, which you learned about in the previous chapter. A behavior is emulated in a program by a method that is associated with a class definition called a member method . Each instance of the class accesses a member method whenever the behavior needs to be performed in the program.

Think of a method as a group of statements that performs a specific behavior and is defined by specifying the following:

  • Method name

  • Method argument list

  • Method body

  • Return value

Programmers call this a method definition .

Method Name

A method name is a symbol a programmer gives to a method, much like how an instance variable name is a symbol used to refer to a location in memory. The name of the method should reflect the kind of behavior the method performs. For example, dropCourse is a good name for a method that drops a course from a student s schedule.

Argument List

Some methods don t require data from outside the method to perform a behavior. For example, a method that erases information entered into a registration form can do this with data already in the method definition.

Other methods require data from outside the method to perform a behavior. For example, the dropCourse method needs to know the course and the student who registered for the course in order to remove the course from the student s schedule. This data isn t included in the method definition but must be provided by part of the program that calls the method (see the Calling a Method section of this chapter).

An argument list is data outside the method definition that is needed by the method to perform a behavior. For example, the dropCourse method has an argument list containing the course number and the student number needed to remove the course from the student s schedule.

Data in an argument list is called an argument, and there can be one or multiple arguments in an argument list, depending on the nature of the behavior performed by the method.

An argument is declared by specifying a data type and a name for the argument. This is similar to declaring an instance variable, except the declaration is within parentheses and appears to the right of the method name, as shown here:

 dropCourse(int courseNumber, int studentNumber) 

This example contains two arguments: int courseNumber and int studentNumber . Collectively they are called an argument list. A comma must separate argument declarations. Once an argument is declared, the name of the argument is used in statements within the method definition to refer to data that is assigned to the argument when the method is called.

Method Body

The method body is part of a method that contains statements that are executed when the method is called. A method body is defined by open and close braces, called a code block . Statements are executed within the method body sequentially, beginning with the first statement and continuing until either a return statement is executed or the end of the method body is reached.

Defining a Method Within a Class Definition

A method definition is placed within the class definition, as shown in the following example. This method is called dropCourse and has two arguments ”the course number and the student number. A value isn t returned by the dropCourse method, so the return value data type is void.

There aren t any statements in the method body because we don t want to clutter the example with unnecessary statements; instead, we included a comment showing you where statements belong:

 class RegistrationForm { 
int studentNumber;
int courseNumber;
void dropCourse(int courseNumber, int studentNumber) {
//Place statements for dropping a course here
}
}

Return Value

Some methods do their thing and don t need to return any value to the statement that called them. This is the case with the method that erases information on a registration form because nothing needs to be returned.

Other methods are required to return data back to the part of the program that called them to give it the result. Data returned by a method is called a return value . For example, a method that calculates an expression, such as adding together two numbers, returns the result of the calculation as the return value.

Two steps are required in order to return a value. First, the data type of the return value must be specified to the left of the method name in the method definition. If a method does not return a value, then void is used as the data type, as shown here (the keyword void tells the computer that nothing is returned by the method):

 void dropCourse(int courseNumber, int studentNumber) 

However, let s suppose that the dropCourse() method returns true if it successfully drops the course and returns false if it is unable to drop the course. True and false are Boolean values. Therefore, the data type of the dropCourse() method must be changed to boolean, as shown here:

 boolean dropCourse(int courseNumber, int studentNumber) 

The second step is to use a return statement within the body of the method definition. The format of the return statement is dependent on the programming language used to write the program. In Java and C++, the return statement consists of three parts: the keyword return , the value to be returned, and a semicolon. The value can be the actual data, a variable, or an argument, as shown here:

 return true; 
return status;

The first return statement returns the Boolean value true. The second return statement returns a variable called status . Some place in the body of the method, the status variable is assigned a value of true or false, depending on whether or not the course is successfully dropped.




OOP Demystified
OOP Demystified
ISBN: 0072253630
EAN: 2147483647
Year: 2006
Pages: 130

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net