Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
C++ For Dummies (2004) [eng].pdf
Скачиваний:
84
Добавлен:
16.08.2013
Размер:
8.09 Mб
Скачать

Chapter 13: Making Classes Work 181

This class definition contains nothing more than a prototype declaration for the function deposit(). The function definition appears separately. The member function prototype declaration in the structure is analogous to any other prototype declaration and, like all prototype declarations, is required.

Notice how the function nickname deposit() was good enough when the function was defined within the class. When defined outside the class, how­ ever, the function requires its extended name.

Overloading Member Functions

Member functions can be overloaded in the same way that conventional func­ tions are overloaded (see Chapter 6 if you don’t remember what that means). Remember, however, that the class name is part of the extended name. Thus, the following functions are all legal:

class Student

{

public:

//grade -- return the current grade point average float grade();

//grade -- set the grade and return previous value float grade(float newGPA);

//...data members and other stuff...

};

class Slope

{

public:

//grade -- return the percentage grade of the slope float grade();

//...stuff goes here too...

};

// grade -- return the letter equivalent of a numerical grade char grade(float value);

int main(int argcs, char* pArgs[])

{

Student s;

s.grade(3.5); // Student::grade(float) float v = s.grade(); // Student::grade()

char c = grade(v); // ::grade(float)

Slope o;

float m = o.grade(); // Slope::grade() return 0;

}

182 Part III: Introduction to Classes

Each call made from main() is noted in the comments with the extended name of the function called.

When calling overloaded functions, not only the arguments of the function but also the type of the object (if any) with which the function is invoked are used to disambiguate the call. (The term disambiguate is object-oriented talk for “decide at compile time which overloaded function to call.” A mere mortal might say “differentiate.”)

In the example, the first two calls to the member functions,

Student::grade(float) and Student::grade(), are differentiated by their argument lists and the type of object used. The call to s.grade() calls

Student::grade() because s is of type Student.

The third call has no object, so it unambiguously denotes the non-member function ::grade(float).

The final call is made with an object of type Slope; it must refer to the member function Slope::grade().

Chapter 14

Point and Stare at Objects

In This Chapter

Examining the object of arrays of objects

Getting a few pointers on object pointers

Strong typing — getting picky about our pointers

Navigating through lists of objects

C++ programmers are forever generating arrays of things — arrays of ints, arrays of floats, so why not arrays of students? Students stand in

line all the time — a lot more than they care to. The concept of Student objects all lined up quietly awaiting their name to jump up to perform some mundane task is just too attractive to pass up.

Defining Arrays of and Pointers to Simple Things

An array is a sequence of identical objects much like the identical houses on a street that make up one of those starter neighborhoods. Each element in the array carries an index, which corresponds to the number of elements from the beginning of the array — the first element in the array carries an offset of 0.

C++ arrays are declared by using the bracket symbols containing the number of elements in the array:

int array[10];

// declare an array of 10 elements

The individual elements of the array can be accessed by counting the offset from the beginning of the array:

array[0] = 10; // assign 10 to the first element

array[9] = 20; // assign 20 to the last element

184 Part III: Introduction to Classes

The program first assigns the value 10 to the first element in the array — the element at the beginning of the array. The program then assigns 20 to the last element in the array — element at offset nine from the beginning.

Always remember that C++ indices start at 0 and go through the size of the array minus 1.

I like to use the analogy of a street with houses. The array name represents the name of the street, and the house number in that street represents the array index. Similarly, variables can be identified by their unique address in com­ puter memory. These addresses can be calculated and stored for later use.

int variable;

// declare an int object

int* pVariable = &variable;

// store its address

 

// in pVariable

*pVariable = 10;

// assign 10 into the int

 

// pointed at by pVariable

The pointer pVariable is declared to contain the address of variable. The assignment stores 10 into the int pointed at by pVariable.

If you apply the house analogy one last time (I promise):

variable is a house.

pVariable is like a piece of paper containing the address of the house.

The final assignment delivers the message 10 to the house whose address is written on pVariable just like a postman might (except unlike my postman, computers don’t deliver mail to the wrong address).

Chapter 7 goes into the care and feeding of arrays of simple (intrinsic) vari­ ables, and Chapter 8 and Chapter 9 describe simple pointers in detail.

Declaring Arrays of Objects

Arrays of objects work the same way arrays of simple variables work. Take, for example, the following snippet from ArrayOfStudents.cpp:

// ArrayOfStudents - define an array of Student objects

//

and access an element in it. This

//

program doesn’t do anything

class Student

 

{

 

public:

int semesterHours; float gpa;

float addCourse(int hours, float grade);

};