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.

<strong>C++</strong> Class Frameworks and the Visual Component Model<br />

TRect rect;<br />

rect->left = 20;<br />

rect->top = 20;<br />

rect->right = 220;<br />

rect->bottom = 220;<br />

OWL also has a TRect class. In OWL you can create a TRect object and supply the top, left,<br />

bottom, and right members through the constructor:<br />

TRect rect(20, 20, 220, 220);<br />

Obviously, specify<strong>in</strong>g the rectangle’s parameters <strong>in</strong> the constructor saves you some typ<strong>in</strong>g.<br />

In addition, the OWL version of TRect has several constructors to create TRect <strong>in</strong>stances <strong>in</strong><br />

different ways. MFC’s CRect class has basically the same type of constructors as OWL’s TRect.<br />

What this means is that <strong>in</strong> some cases you have to give up the flexibility that <strong>C++</strong> allows you<br />

when construct<strong>in</strong>g VCL classes. It’s a m<strong>in</strong>or sacrifice <strong>in</strong> most cases.<br />

VCL Does Not Have Default Parameters for Functions<br />

To illustrate, let’s exam<strong>in</strong>e the W<strong>in</strong>dows API function MessageBox(). This function takes<br />

four parameters: the w<strong>in</strong>dow handle of the w<strong>in</strong>dow that is display<strong>in</strong>g the message box, the<br />

message box text, the message box title, and a Flags parameter that controls which buttons<br />

and icons are displayed on the message box. In MFC or OWL, you can call a message box<br />

by just specify<strong>in</strong>g the message box text:<br />

MessageBox(“This is a message.”);<br />

This is possible because the OWL and MFC versions of MessageBox() have default<br />

parameters for the message box title and style flags. You can specify the additional parameters,<br />

but if you don’t, the default values will be used. This is convenient for throw<strong>in</strong>g up message<br />

boxes with a m<strong>in</strong>imum of fuss. Here is the VCL equivalent to the previous l<strong>in</strong>e:<br />

Application->MessageBox(“This is a message”, “Message”, MB_OK);<br />

Because VCL does not have default parameters, you have to specify all the parameters. It’s<br />

not convenient, but not the end of the world, either. Note that <strong>in</strong> all three cases (MFC, OWL,<br />

and VCL), the framework takes care of supply<strong>in</strong>g the w<strong>in</strong>dow handle parameter.<br />

VCL Classes Do Not Support Multiple Inheritance<br />

This means that you cannot create a new component derived from two exist<strong>in</strong>g components.<br />

I don’t see this as a serious restriction because multiple <strong>in</strong>heritance is not widely used. Regular<br />

<strong>C++</strong> classes that you write for use <strong>in</strong> your <strong>C++</strong>Builder applications may use multiple<br />

<strong>in</strong>heritance.<br />

155<br />

5

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

Saved successfully!

Ooh no, something went wrong!