12.12.2012 Views

Teach Yourself Borland C++ in 14 Days - portal

Teach Yourself Borland C++ in 14 Days - portal

Teach Yourself Borland C++ in 14 Days - portal

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.

90 Day 3<br />

NOTE<br />

Default parameters are helpful for many reasons. For one th<strong>in</strong>g, they make your life easier.<br />

You may have a function that you call with the same parameters 99 percent of the time. By<br />

giv<strong>in</strong>g it default parameters, you shorten the amount of typ<strong>in</strong>g required each time you make<br />

a call to the function. Whenever you want to supply parameters other than the defaults, all<br />

you have to do is plug <strong>in</strong> values for the default parameters.<br />

Any default parameters must come at the end of the function’s parameter<br />

list. The follow<strong>in</strong>g is not a valid function declaration:<br />

<strong>in</strong>t MyFunction(<strong>in</strong>t x, <strong>in</strong>t y = 10, <strong>in</strong>t t = 5, <strong>in</strong>t z);<br />

In order for this function declaration to compile, the default parameters<br />

must be moved to the end of the function list:<br />

<strong>in</strong>t MyFunction(<strong>in</strong>t x, <strong>in</strong>t z, <strong>in</strong>t y = 10, <strong>in</strong>t t = 5);<br />

If you don’t put the default parameters at the end of the parameter list,<br />

the compile will generate a compiler error.<br />

Class Member Functions<br />

NEW TERM<br />

As you will f<strong>in</strong>d out <strong>in</strong> this section, classes can conta<strong>in</strong> their own functions. Such<br />

functions are called member functions because they are members of a class.<br />

Class member functions follow the same rules as regular functions: They can be overloaded,<br />

they can have default parameters, they can take any number of parameters, and so on.<br />

Class member functions can be called only through an object of the class to which the<br />

function belongs. To call a class member function, you use the direct member operator (<strong>in</strong><br />

the case of local objects) or the <strong>in</strong>direct member operator (for dynamically created objects)<br />

just like you did when access<strong>in</strong>g data members of a structure on Day 2. For example, let’s say<br />

that you had a class called Airplane that was used to track an airplane for aircraft-control<br />

software. That class would probably have the capability to retrieve the current speed of a given<br />

aircraft via a function called GetSpeed(). The follow<strong>in</strong>g example illustrates how you would<br />

call the GetSpeed() function of an Airplane object:<br />

Airplane plane; // create a class <strong>in</strong>stance<br />

<strong>in</strong>t speed = plane.GetSpeed();<br />

cout

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

Saved successfully!

Ooh no, something went wrong!