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.

104 Day 4<br />

NOTE<br />

use <strong>in</strong> all functions of the class. Depend<strong>in</strong>g on the data member’s access level, it might be<br />

visible outside the class as well. Private and protected data members, for <strong>in</strong>stance, are private<br />

to the class and cannot be seen outside the class. Public data members, however, can be<br />

accessed from outside the class, but only through an object. Take the Rect class declared<br />

previously, for example. It has no public data members. You could try the follow<strong>in</strong>g, but<br />

you’d get a compiler error:<br />

Rect rect(10, 10, 200, 200);<br />

<strong>in</strong>t x = rect.left; // compiler error!<br />

The compiler error will say Rect::left is not accessible. The compiler is tell<strong>in</strong>g you that<br />

left is a private data member and you can’t get to it. If left were <strong>in</strong> the public section of the<br />

class declaration, this code would compile.<br />

You can use getters and setters to change private data members. That is, getters are functions<br />

that get the value of a private data member, and setters are functions that set the value of a<br />

private data member. Both getters and setters are public member functions that act on private<br />

data members.<br />

To illustrate, let’s say that for the Rect class you had the follow<strong>in</strong>g getters and setters for the<br />

left data member:<br />

<strong>in</strong>t Rect::GetLeft()<br />

{<br />

return left;<br />

}<br />

void Rect::SetLeft(<strong>in</strong>t newLeft)<br />

{<br />

left = newLeft;<br />

}<br />

Now, when you want to obta<strong>in</strong> the value of the left member of the Rect class, use this:<br />

TRect rect;<br />

<strong>in</strong>t x = rect.GetLeft();<br />

In some cases this is overkill. Setters have one ma<strong>in</strong> advantage, though—they allow you to<br />

validate <strong>in</strong>put. By validat<strong>in</strong>g <strong>in</strong>put, you can control the values your data members conta<strong>in</strong>.<br />

Some OOP extremists say that data members should never be public.<br />

They would say that you should use getters and setters to access all data<br />

members. On the other end of the spectrum is the group that says to<br />

make all your data members public. The truth lies somewhere <strong>in</strong><br />

between. Some data members are noncritical and may be left public if<br />

it is more convenient. Other data members are critical to the way the<br />

class operates and should not be made public. If you are go<strong>in</strong>g to err, it<br />

is better to err on the side of mak<strong>in</strong>g data members private.

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

Saved successfully!

Ooh no, something went wrong!