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.

Totally Immersed: <strong>C++</strong> Classes and Object-Oriented Programm<strong>in</strong>g<br />

Anatomy of a Class<br />

A class, like a structure, has a declaration. The class declaration is usually conta<strong>in</strong>ed <strong>in</strong> a header<br />

file. In simple cases, both the class declaration and the def<strong>in</strong>ition can be conta<strong>in</strong>ed <strong>in</strong> a s<strong>in</strong>gle<br />

source file, but you typically won’t do that for real applications. Usually you create a class<br />

source file with a filename closely match<strong>in</strong>g the class name and with a .cpp extension. Because<br />

W<strong>in</strong>dows 95 and W<strong>in</strong>dows NT both support long filenames, you can use filenames that<br />

exactly match your class name if you want. The header file for the class usually has the same<br />

name as the source file but with the extension .h. For example, if you had a class called<br />

MyClass, you would have a source file named MYCLASS.CPP and a header named MYCLASS.H.<br />

Class Access Levels<br />

Classes can have three levels of access: private, public, or protected. Each of these<br />

access levels is def<strong>in</strong>ed <strong>in</strong> this section.<br />

Class access levels control how a class is utilized by users of the class. As a sole programmer,<br />

you might be the class’s creator but also a user of the class. In team programm<strong>in</strong>g<br />

environments, one programmer might be the creator of the class and other programmers are<br />

users of the class.<br />

NEW TERM<br />

NOTE<br />

Let me clarify a couple comments I made on Day 2, “Wad<strong>in</strong>g In<br />

Deeper.” I said that a structure is a class <strong>in</strong> which all data members and<br />

functions are public. In fact, <strong>in</strong> <strong>C++</strong> this is the only th<strong>in</strong>g that dist<strong>in</strong>guishes<br />

a structure from a class. A structure can have functions as well<br />

as data members. A structure cannot use the access-modifier keywords<br />

(private, protected, and private) because a structure can only have<br />

public access. I also said that you probably won’t use structures very<br />

much <strong>in</strong> your <strong>C++</strong> programs. Because a class and a structure are nearly<br />

the same, you will probably prefer to use classes over structures.<br />

To understand what role levels of access play <strong>in</strong> class operation, you first need to understand<br />

how classes will be used. In any class there is the public part of the class, which the outside<br />

world has access to, and there is the private part of a class. The private part of a class is the<br />

<strong>in</strong>ternal implementation of the class—the <strong>in</strong>ner work<strong>in</strong>gs, so to speak.<br />

97<br />

4

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

Saved successfully!

Ooh no, something went wrong!