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.

106 Day 4<br />

SYNTAX<br />

▼<br />

▲<br />

code. To remove clutter from the constructor, a class might have an Init() function that is<br />

called from the constructor to perform those startup tasks. This function would never be<br />

called directly by a user of the class. In fact, more than likely bad th<strong>in</strong>gs would happen if this<br />

function were to be called by a user at the wrong time, so the function is private <strong>in</strong> order to<br />

protect both the <strong>in</strong>tegrity of the class and the user.<br />

Protected member functions are functions that cannot be accessed by the outside world but can<br />

be accessed by classes that are derived from this class. I haven’t talked yet about classes be<strong>in</strong>g<br />

derived from other classes, so I’ll save this discussion for a little later when it will make more<br />

sense. I discuss deriv<strong>in</strong>g classes <strong>in</strong> the section “Inheritance.”<br />

The <strong>in</strong>l<strong>in</strong>e function, Form 1:<br />

ClassName {<br />

public:<br />

ReturnType FunctionName();<br />

};<br />

<strong>in</strong>l<strong>in</strong>e ReturnType ClassName::FunctionName() {<br />

statements<br />

}<br />

The function FunctionName is declared with<strong>in</strong> the body of the class ClassName. The function<br />

def<strong>in</strong>ition (the function itself) is def<strong>in</strong>ed outside the class declaration us<strong>in</strong>g the <strong>in</strong>l<strong>in</strong>e<br />

keyword. FunctionName must be proceeded by ClassName and the scope resolution operator.<br />

The <strong>in</strong>l<strong>in</strong>e function, Form 2:<br />

ClassName {<br />

public:<br />

ReturnType FunctionName()<br />

{<br />

statements<br />

}<br />

};<br />

The function FunctionName is declared and def<strong>in</strong>ed entirely with<strong>in</strong> the ClassName declaration.<br />

The function is an <strong>in</strong>l<strong>in</strong>e function by virtue of the fact that it is conta<strong>in</strong>ed with<strong>in</strong> the<br />

ClassName declaration. The <strong>in</strong>l<strong>in</strong>e keyword is not required.<br />

As with data members, member functions can be declared with the static modifier. A static<br />

member function operates more like a regular function than a member function. Specifically,<br />

a static member function cannot access data members of the class. (I’ll tell you why this<br />

restriction exists <strong>in</strong> just a bit.) Most of the time you will not use static member functions, but<br />

sometimes you will be required to. For <strong>in</strong>stance, some W<strong>in</strong>dows API functions use callbacks<br />

to perform repeated tasks. If you used this k<strong>in</strong>d of function <strong>in</strong> your class, the callback function<br />

would have to be declared as static.

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

Saved successfully!

Ooh no, something went wrong!