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.

336 Day 9<br />

Add<strong>in</strong>g Functions and Data Members<br />

to Code<br />

As you know by now, <strong>C++</strong>Builder is a great tool for quickly creat<strong>in</strong>g the user <strong>in</strong>terface portion<br />

of a W<strong>in</strong>dows application. It creates event handlers for you so that you can beg<strong>in</strong> enter<strong>in</strong>g<br />

code to drive your application. It won’t be long, however, before you f<strong>in</strong>d the need to start<br />

add<strong>in</strong>g more complicated code to your applications. That means add<strong>in</strong>g your own data<br />

members and functions to the code that <strong>C++</strong>Builder generates. For example, a simple<br />

application might conta<strong>in</strong> two dozen event handlers of various types. <strong>C++</strong>Builder creates all<br />

these event handlers for you; you simply fill <strong>in</strong> the blanks with work<strong>in</strong>g code. In order to make<br />

the application a viable, work<strong>in</strong>g application, however, you might have to write another two<br />

dozen functions of your own.<br />

Add<strong>in</strong>g your own functions and data members to <strong>C++</strong>Builder-generated code is not a<br />

difficult task, but you need to know the rules or you can get <strong>in</strong>to trouble.<br />

How <strong>C++</strong>Builder Manages Class Declarations<br />

As you know, when you create a new form <strong>in</strong> the Form Designer, <strong>C++</strong>Builder creates three<br />

files for you: the form file, the source code unit, and the unit’s header. When <strong>C++</strong>Builder<br />

creates the class declaration <strong>in</strong> the header, it essentially creates two sections. The first section<br />

is the part of the class declaration that <strong>C++</strong>Builder manages. The second section is the part<br />

that you manage. On Day 7 you created the ScratchPad program. If you did the exercises at<br />

the end of that chapter, you also created an About box for the program and added a few more<br />

buttons. List<strong>in</strong>g 9.1 conta<strong>in</strong>s the ma<strong>in</strong> form’s header as it appears after add<strong>in</strong>g these<br />

enhancements.<br />

List<strong>in</strong>g 9.1. SPMa<strong>in</strong>.h.<br />

1: class TScratchPad : public TForm<br />

2: {<br />

3: __published: // IDE-managed Components<br />

4: TPanel *Panel1;<br />

5: TBevel *Bevel1;<br />

6: TSpeedButton *FileOpenBtn;<br />

7: TSpeedButton *FileSaveBtn;<br />

8: TStatusBar *StatusBar;<br />

9: TMa<strong>in</strong>Menu *Ma<strong>in</strong>Menu;<br />

10: TMenuItem *FileMenu;<br />

11: TMenuItem *FileOpen;<br />

12: TMenuItem *FileSave;<br />

13: TMenuItem *FileSaveAs;<br />

<strong>14</strong>: TMenuItem *N1;<br />

15: TMenuItem *FilePr<strong>in</strong>tSetup;<br />

16: TMenuItem *N2;

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

Saved successfully!

Ooh no, something went wrong!