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.

96 Day 4<br />

So, uh…What’s a Class?<br />

A class, like a structure, is a collection of data members and functions that work together to<br />

accomplish a specific programm<strong>in</strong>g task. In this way a class is said to encapsulate the task.<br />

Classes have the follow<strong>in</strong>g features:<br />

■ The capability to control access<br />

■ Constructors<br />

■ Destructors<br />

■ Data members<br />

■ Member functions<br />

■ A hidden, special po<strong>in</strong>ter called this<br />

Before div<strong>in</strong>g <strong>in</strong>to an exam<strong>in</strong>ation of those features, let me give you a quick example of how<br />

a class might work. Let’s use a typical W<strong>in</strong>dows control as an example—a check box, for<br />

<strong>in</strong>stance. A class that represents a check box could have data members for the caption of the<br />

check box and for the state (checked or unchecked). This class would also have functions that<br />

would allow you to set and query both the check box caption and the check state. These<br />

functions might be named GetCheck(), SetCheck(), GetCaption(), and SetCaption(). Once<br />

the class has been written, you can create an <strong>in</strong>stance of the class to control a check box <strong>in</strong><br />

W<strong>in</strong>dows. (It’s not quite that simple, but this is just an example, after all.) If you have three<br />

check boxes, you could have three <strong>in</strong>stances of the CheckBox class that could then be used to<br />

control each check box <strong>in</strong>dividually. Here’s an example:<br />

MyCheckBox check1(ID_CHECK1);<br />

MyCheckBox check2(ID_CHECK2);<br />

MyCheckBox check3(ID_CHECK3);<br />

check1.SetCaption(“Th<strong>in</strong>gamabob Option”);<br />

check1.SetCheck(true);<br />

check2.SetCaption(“Doohickey Options”);<br />

check2.SetCheck(false);<br />

check3.SetCaption(“Whodyacallum Options”);<br />

check3.SetCheck(true);<br />

if (check1.GetCheck()) DoTh<strong>in</strong>gamabobTask();<br />

if (check2.GetCheck()) DoDoohickeyTask();<br />

// etc.<br />

In this example, each <strong>in</strong>stance of the class is a separate object. Each <strong>in</strong>stance has its own data<br />

members, and the objects operate <strong>in</strong>dependently of one another. They are all objects of the<br />

same type, but are separate <strong>in</strong>stances <strong>in</strong> memory. With that brief <strong>in</strong>troduction, let’s roll up<br />

our sleeves once more and go to work on understand<strong>in</strong>g classes.

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

Saved successfully!

Ooh no, something went wrong!