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

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

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

Each <strong>in</strong>stance of your class gets its own copy of the class’s data members <strong>in</strong> memory. The<br />

exception to this is that if any class data members are declared with the static storage<br />

modifier, all <strong>in</strong>stances of the class will share the same copy of that data member <strong>in</strong> memory.<br />

In that case only one copy of that data member will exist <strong>in</strong> memory. If any one <strong>in</strong>stance of the<br />

class changes a static data member, it changes <strong>in</strong> all the classes. Use of static data members<br />

<strong>in</strong> classes is not common, so don’t worry about it if this doesn’t make sense right now.<br />

HOUSE RULES: CLASS DATA MEMBERS<br />

■ Use as many data members as you need for vital class operations, but use local<br />

variables where possible.<br />

■ Don’t make all data members public.<br />

■ Use getters and setters for data members that you want to rema<strong>in</strong> private but that<br />

you need to be able to access.<br />

■ Validate data <strong>in</strong> your setters to ensure that improper values are not be<strong>in</strong>g <strong>in</strong>put.<br />

■ Initialize all data members either <strong>in</strong> the <strong>in</strong>itializer list or <strong>in</strong> the body of your<br />

constructor.<br />

■ Don’t forget to delete any data members that dynamically allocate memory.<br />

Member Functions<br />

Class member functions are functions that belong to your class. They are local to the class and<br />

do not exist outside the class. Class member functions can be called only from with<strong>in</strong> the class<br />

itself or through an <strong>in</strong>stance of the class. They have access to all public, protected, and private<br />

data members of the class. Member functions can be declared <strong>in</strong> the private, protected, or<br />

public sections of your class. Good class design requires that you th<strong>in</strong>k about which of these<br />

sections your member functions should go <strong>in</strong>to.<br />

Public member functions represent the user <strong>in</strong>terface to the class. It is through the public<br />

member functions that users of the class access the class <strong>in</strong> order to ga<strong>in</strong> whatever<br />

functionality the class provides. For example, let’s say you have a class that plays and records<br />

wave audio. Public member functions might <strong>in</strong>clude functions like Open(), Play(), Record(),<br />

Save(), Rew<strong>in</strong>d(), and so on.<br />

Private member functions are functions that the class uses <strong>in</strong>ternally to do its th<strong>in</strong>g. These<br />

functions are not <strong>in</strong>tended to be called by users of the class; they are private <strong>in</strong> order to be<br />

hidden from the outside world. Frequently a class has startup chores to perform when the class<br />

is created. (For example, you have already seen that the constructor is called when a class is<br />

created.) In some classes the startup process<strong>in</strong>g might be significant, requir<strong>in</strong>g many l<strong>in</strong>es of<br />

105<br />

4

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

Saved successfully!

Ooh no, something went wrong!