11.07.2015 Views

tYSR20

tYSR20

tYSR20

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

184Part III: Introduction to ClassesThe program first assigns the value 10 to the first element in the array — theelement at the beginning of the array. The program then assigns 20 to the lastelement in the array — element at offset nine from the beginning.Always remember that C++ indices start at 0 and go through the size of thearray minus 1.I like to use the analogy of a street with houses. The array name represents thename of the street, and the house number in that street represents the arrayindex. Similarly, variables can be identified by their unique address in computermemory. These addresses can be calculated and stored for later use.int variable;int* pVariable = &variable;*pVariable = 10;// declare an int object// store its address// in pVariable// assign 10 into the int// pointed at by pVariableThe pointer pVariable is declared to contain the address of variable. Theassignment 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 whoseaddress is written on pVariable just like a postman might (exceptunlike my postman, computers don’t deliver mail to the wrong address).Chapter 7 goes into the care and feeding of arrays of simple (intrinsic) variables,and Chapter 8 and Chapter 9 describe simple pointers in detail.Declaring Arrays of ObjectsArrays 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 anythingclass Student{public:int semesterHours;float gpa;float addCourse(int hours, float grade);};

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!