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.

NOTE<br />

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

button->Caption = “New Button”;<br />

button->Left = 100;<br />

button->Top = 100;<br />

button->Show();<br />

// more code<br />

}<br />

In this code you can see that this is used <strong>in</strong> the constructor (this sets the Owner property of<br />

the button, but I’ll get <strong>in</strong>to that later when I cover VCL components on Day 8, “VCL<br />

Components”) and also that it is assigned to the Parent property of the newly created button.<br />

This will be how you use the this po<strong>in</strong>ter the vast majority of the time <strong>in</strong> your <strong>C++</strong>Builder<br />

applications.<br />

Earlier I said that static member functions can’t access class data<br />

members. This is true because static member functions do not have a<br />

hidden this parameter as regular class member functions do. Without<br />

this, a function cannot access class members.<br />

Don’t get too hung up on this…er, this (whatever!). When you beg<strong>in</strong> to use VCL, it will<br />

quickly become clear when you are required to use this <strong>in</strong> your <strong>C++</strong>Builder applications.<br />

An Example<br />

Right now it would be nice if you had an example that uses classes. The follow<strong>in</strong>g list<strong>in</strong>gs<br />

conta<strong>in</strong> a program that implements classes. This program allows you to play air traffic<br />

controller by issu<strong>in</strong>g commands to three aircrafts. List<strong>in</strong>g 4.1 is the header for the Airplane<br />

class, List<strong>in</strong>g 4.2 is the source code for the Airplane class, and List<strong>in</strong>g 4.3 is the ma<strong>in</strong> program.<br />

List<strong>in</strong>g 4.1. AIRPLANE.H.<br />

1: //------------------------------------------------------<br />

2: #ifndef airplaneH<br />

3: #def<strong>in</strong>e airplaneH<br />

4: #def<strong>in</strong>e AIRLINER 0<br />

5: #def<strong>in</strong>e COMMUTER 1<br />

6: #def<strong>in</strong>e PRIVATE 2<br />

7: #def<strong>in</strong>e TAKINGOFF 0<br />

8: #def<strong>in</strong>e CRUISING 1<br />

9: #def<strong>in</strong>e LANDING 2<br />

10: #def<strong>in</strong>e ONRAMP 3<br />

11: #def<strong>in</strong>e MSG_CHANGE 0<br />

109<br />

cont<strong>in</strong>ues<br />

4

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

Saved successfully!

Ooh no, something went wrong!