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 />

The first constructor is a default constructor by virtue of the fact that it takes no parameters.<br />

It simply <strong>in</strong>itializes each data member to 0. The second constructor takes the parameters<br />

passed and assigns them to the correspond<strong>in</strong>g class data members. The variable names <strong>in</strong> the<br />

parameter list are local to the constructor, so each of the variable names beg<strong>in</strong>s with an<br />

underscore to differentiate between the local variables and the class data members.<br />

TIP<br />

NEW TERM<br />

Remember that an un<strong>in</strong>itialized variable will conta<strong>in</strong> random data.<br />

This is true for class data members as well as other variables. To be safe,<br />

you should set class member variables to some <strong>in</strong>itial value.<br />

Instantiation is the creation of an object, an <strong>in</strong>stance, or a class.<br />

It’s important to understand that you can’t call a constructor directly. So how do you use one<br />

of these constructors over the other? You do that when you create or <strong>in</strong>stantiate an object or<br />

a class. The follow<strong>in</strong>g code snippet creates two <strong>in</strong>stances of the Rect class. The first uses the<br />

default constructor, and the second uses the second form of the constructor:<br />

Rect rect1; // object created us<strong>in</strong>g default constructor<br />

Rect rect2(0, 0, 100, 100); // created us<strong>in</strong>g 2nd constructor<br />

You can have as many constructors as you like, but be sure that your constructors don’t have<br />

ambiguous parameter lists (as per the rules on function overload<strong>in</strong>g).<br />

Initializer Lists<br />

NEW TERM<br />

<strong>C++</strong> provides a means by which you can <strong>in</strong>itialize class data members <strong>in</strong> what<br />

is called an <strong>in</strong>itializer list.<br />

The follow<strong>in</strong>g is the proper way to <strong>in</strong>itialize data members of a class. Rather than try<strong>in</strong>g to<br />

expla<strong>in</strong> how to use an <strong>in</strong>itializer list, let me show you an example. Let’s take the two<br />

constructors for the Rect class and <strong>in</strong>itialize the data members with an <strong>in</strong>itializer list rather<br />

than <strong>in</strong> the body of the function as I did before. It looks like this:<br />

Rect::Rect() :<br />

left(0),<br />

top(0),<br />

bottom(0),<br />

right(0)<br />

{<br />

}<br />

101<br />

4

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

Saved successfully!

Ooh no, something went wrong!