⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ch19.htm

📁 好书《C++ Builder高级编程技术》
💻 HTM
📖 第 1 页 / 共 5 页
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<HTML>

<HEAD>
	<META HTTP-EQUIV="Content-Type" CONTENT="text/html;CHARSET=iso-8859-1">
	<META NAME="Author" Content="Steph Mineart">
	<TITLE>Ch 19 -- Inheritance</TITLE>
</HEAD>

<BODY 
BACKGROUND="bg1.gif" tppabs="http://pbs.mcp.com/ebooks/0672310228/buttonart/bg1.gif" BGCOLOR="#FFFFFF">

<P ALIGN="CENTER"><IMG SRC="sams.gif" tppabs="http://pbs.mcp.com/ebooks/0672310228/buttonart/sams.gif" WIDTH="75" HEIGHT="24" ALIGN="BOTTOM"
BORDER="0"><BR>
<BR>
<A HREF="index-3.htm" tppabs="http://pbs.mcp.com/ebooks/0672310228/index.htm"><IMG SRC="toc.gif" tppabs="http://pbs.mcp.com/ebooks/0672310228/buttonart/toc.gif" WIDTH="40" HEIGHT="40" ALIGN="BOTTOM"

ALT="TOC" BORDER="0" NAME="toc4"></A><A HREF="ch18.htm" tppabs="http://pbs.mcp.com/ebooks/0672310228/ch18.htm"><IMG SRC="back-1.gif" tppabs="http://pbs.mcp.com/ebooks/0672310228/buttonart/back.gif"
WIDTH="40" HEIGHT="40" ALIGN="BOTTOM" ALT="BACK" BORDER="0" NAME="toc1"></A><A HREF="ch20.htm" tppabs="http://pbs.mcp.com/ebooks/0672310228/ch20.htm"><IMG
SRC="forward.gif" tppabs="http://pbs.mcp.com/ebooks/0672310228/buttonart/forward.gif" WIDTH="40" HEIGHT="40" ALIGN="BOTTOM" 
ALT="FORWARD" BORDER="0"
NAME="toc2"></A></P>
<H2 ALIGN="CENTER"><FONT COLOR="#000077">Charlie Calvert's C++ Builder Unleashed</FONT></H2>
<P>
<H2 ALIGN="CENTER"><A NAME="Heading1"></A><FONT COLOR="#000077">- 19 -</FONT></H2>
<H2 ALIGN="CENTER"><A 
NAME="Heading2"></A><FONT COLOR="#000077">Inheritance</FONT></H2>
<P>This chapter focuses on object-oriented programming (OOP) as it applies to the
VCL. Specifically, it takes a close look at inheritance, one of the big-three topics
in object-oriented 
code. The other two key topics are encapsulation and polymorphism,
which you learn about in the next two chapters. Even if you already know all about
OOP, you should still at least skim this chapter so you can learn about the difference
between VCL 
objects and standard C++ objects.</P>
<P>In particular, this chapter covers the following topics:

<UL>
	<LI>OOP theory and basics
	<P>
	<LI>VCL object construction
	<P>
	<LI>Inheritance
	<P>
	<LI>Virtual methods
	<P>
	<LI>Aggregation
	<P>
	<LI>Form 
inheritance
</UL>

<P>The text focuses on several programs designed to show how objects are constructed.
One of the programs is developed in several stages so that you can see how an object
hierarchy emerges out of a set of raw ideas.</P>
<P>After you 
read the next three chapters on inheritance, encapsulation, and polymorphism,
the next big step is to learn how to build components. In fact, the real justification
for learning this material is that it gives you the ability to start creating your
own 
components. Building your own components is one of the most important tasks you
can tackle in BCB, so I will lay the groundwork for it carefully.</P>
<P>It is important to understand that the next three chapters are aimed at programmers
who want to 
work inside the VCL. I make no attempt to do justice to all the complex
features of the C++ object model. Instead, I try to present you with a subset of
those features as they apply to the VCL. This means that I make short shrift of interesting
topics 
such as function and operator overloading. My intent, however, is to show
you how to create components. You do not have to be an expert in C++ OOP theory to
achieve that goal.</P>
<P>For all its wonders, I don't think there is anything in C++Builder 
that even approaches
the significance of components. VCL components are the most amazing technological
achievement I have seen in contemporary programming. If you want to do something
really fantastic with your computer, then pay attention to the next 
few chapters
so that you can learn how to build great components.</P>
<P>When reading this chapter, you might want to make use of the ClassBrowser sample
program that ships with BCB. It allows you to explore the hierarchy of the VCL. This
program is 
found in the <TT>Examples/ClassBrw</TT> directory. It is far from perfect,
but it will serve to give you an overview of the VCL classes. You should also go
to <A HREF="javascript:if(confirm('http://www.object-domain.com/  \n\nThis file was not retrieved by Teleport Pro, because it is addressed on a domain or path outside the boundaries set for its Starting Address.  \n\nDo you want to open it from the server?'))window.location='http://www.object-domain.com/'" tppabs="http://www.object-domain.com/"><TT>www.object-domain.com</TT></A> and
see whether 
they have a version of Snorkle for C++Builder available. The versions
of Snorkle for Delphi that I have seen are very nice indeed, and if they can duplicate
their efforts in the world of C++, then most readers of this book will want to test
their 
technology.
<H3><A NAME="Heading3"></A><FONT COLOR="#000077">About Objects</FONT></H3>
<P>It might seem a little strange to start focusing on objects this late in the book.
After all, almost every program I have shown so far uses object-oriented code. 
So
how could I wait this long to begin talking seriously about objects? To answer this
question, I need to discuss two different issues:

<UL>
	<LI>How does BCB treat objects?
	<P>
	<LI>Why do people write object-oriented code?
</UL>

<P>The 
developers wanted BCB to be very easy to use. By its very nature, OOP is not
always a simple topic. As a result, BCB goes to considerable lengths to hide some
of the difficulties of object-oriented programming from the user. The biggest steps
in this 
direction include the automatic construction of <TT>Form1</TT> as an object
and the existence of the delegation model. The fact that the scaffolding for most
methods is produced automatically by the IDE is one of the key ways the product saves

time--and one of the key ways it eases the process of producing applications.</P>
<P>The simple fact is that some people would never be able to approach BCB if they
had to go through the process of writing all this every time they created a form:</P>

<PRE><FONT COLOR="#0066FF">//--------------------------------------------------------------------------

#ifndef Unit1H

#define Unit1H

//--------------------------------------------------------------------------

#include &lt;vcl\Classes.hpp&gt;


#include &lt;vcl\Controls.hpp&gt;

#include &lt;vcl\StdCtrls.hpp&gt;

#include &lt;vcl\Forms.hpp&gt;

//--------------------------------------------------------------------------

class TForm1 : public TForm

{

__published:

private:

public:        
// User declarations

  virtual __fastcall TForm1(TComponent* Owner);

};

//--------------------------------------------------------------------------

extern TForm1 *Form1;


//--------------------------------------------------------------------------

#endif

</FONT></PRE>
<P>I'm leaving out the implementation of the constructor, and a few other features,
but in a stripped-down form, this code is indeed the basis for most 
BCB units. It's
simple enough to write; nonetheless, it could form a barrier between the product
and certain types of programmers.</P>
<P>The next obvious question is, &quot;Why did the developers choose to write object-oriented
code if the subject 
itself can at times become somewhat complex? Why not just use
the relatively simpler framework provided by structured programming?&quot; The answer
is that although it is simpler to create small structured programs than small object-oriented
programs, 
it's easier to write large object-oriented programs than it is to write
large structured programs.</P>
<P>OOP brings discipline and structure to a project. In the long run, this makes
coding easier. The problem is the learning curve associated with 
understanding OOP.</P>
<P>Almost everyone agrees that it's easier to finish a group project if you appoint
a leader for the group; it's easier to win at sports if you practice regularly; and,
ultimately, it's easier to become a good musician if you 
sit through some boring
lessons with a professional. It also might seem at first as if structured programs
are simpler to learn how to write and, therefore, are simpler to write, but this
isn't true. Just as it helps to take lessons, practice, and 
learn discipline if you
want to become good at playing a sport or a musical instrument, it helps to learn
object- oriented code if you want to write good programs.</P>
<P>Here's another way of stating the same matter. There is nothing you can do with

object- oriented code that you can't also do with structured programming. It's just
that OOP makes it relatively easy to construct programs that are fundamentally sound
and easily maintained. This doesn't mean you can't write structured programs that

are every bit as architecturally sound as object-oriented programs. The problem,
however, is that it is very difficult to design a structured program that is truly
modularized and truly easy to maintain. Object-oriented code, on the other hand,
has a 
natural tendency to move you in the direction of a sound, well-structured design.</P>
<P>The thesis of this chapter is that object-oriented code is basically a technique
for designing robust, well-planned programs. The syntax of OOP emerged out of the

desire to help programmers design applications that work. It is perhaps arguable
as to whether or not OOP by itself succeeded in achieving its goal, though certainly
I personally believe that it is a success. However, I think it is undeniable that
OOP 
in conjunction with components is the answer to many core programming problems.
If your only experience with components is in creating ActiveX controls, then you
haven't yet seen what this technology can do. The combination of OOP and components
is 
something that can make programmers many times more productive than they had ever
imagined possible when writing structured code, or when working with either objects
or components alone.

<DL>
	<DT></DT>
</DL>



<BLOCKQUOTE>
	<P>
<HR>
<FONT 
COLOR="#000077"><B>NOTE: </B></FONT>It's probably worth pointing out that OOP
	is not a separate subject from structured programming but its natural child. OOP
	emerged out of the same types of thinking that generated structured code. Much of
	what is 
true in structured programs is also true in object-oriented programs, except
	OOP takes these theories much further. Object-based programmers should know nearly
	everything that structured programmers know and should then add another layer of
	
information on top of it. 
<HR>


</BLOCKQUOTE>

<P>OOP is certainly not the end-all and be-all of programming. Rather, it is an intermediate
step in an ongoing process that might never have an end. BCB, with its heavy use
of components, already shows 
part of what the future holds. In particular, the future
is about components and visual manipulation of objects.</P>
<P>The Object Inspector enables you to see inside objects and to start to manipulate
them visually. You can do this without having to 
write code. It is quite likely that
this trend will continue in the future, and you will start to see programs not as
code but as a series of objects depicted as a hierarchy. If programmers want to manipulate
these objects, they will be able to do so 
through tools such as the Object Inspector
or through other means currently being used only in experimental languages.</P>
<P>To take this out of the clouds for a moment, here is my list of what's best about
BCB:

<UL>
	<LI>Visual design tools
	<P>
	
<LI>A component architecture replete with a delegation model
	<P>
	<LI>A real object-oriented language
</UL>

<P>Here are the same ideas looked at again from a slightly more in-depth perspective:

<UL>
	<LI>Visual Tools: You can easily design a form 
using visual tools. To create a useful
	form, you want to be able to arrange and rearrange the elements of the visual design
	quickly and easily. BCB excels at this.
	<P>
	<LI>Components: You want to be able to manipulate objects not only as code, but 
as
	seemingly physical entities you can handle with the mouse. Components provide an
	ideal solution to this problem. For example, the plastic Lego sets you played with
	as a child were fascinating because they enabled you to build complex structures
	
out of simple, easy-to-manipulate pieces. In other words, Legos let you concentrate
	on the design of structures, making the actual construction of a robust and easy-to-maintain
	building relatively trivial. Components give the same kind of 
flexibility.
	<P>
	<LI>OOP: Objects, and particularly the ability to view object hierarchies in a browser,
	make it easy to see the overall design of a program. It's possible to see how a program
	is constructed not only by looking at the code, but 
also by looking at an object
	hierarchy made up of reusable classes. Use the ClassBrowser example or a copy of
	Snorkle to view these hierarchies as they appear in your own programs. These kinds
	of abstract, visual representations of a code base aid 
in the process of designing
	and maintaining a program. A key word here is reuse. You can write an object once
	and then use it over and over again. Reusability is what OOP is all about.
</UL>

<P>OOP, then, is part of a theory of design that is 
moving increasingly in the direction
of reusable, visual components that can be manipulated with the mouse. Undoubtedly,
this means that some types of programs that are difficult to construct today will
become trivial to build in the future. BCB has 
already performed this magic with
databases. A 10-year-old child could use BCB to construct a simple database application.
However, creating complex programs will probably always be difficult, simply because
it is so hard to design a good program that 
performs anything more than trivial tasks.
First printing presses, then typewriters, and finally word processors have made writing
much easier than it used to be, but they have not succeeded in making us all into
a race of Shakespeares.

<DL>
	
<DT></DT>
</DL>



<BLOCKQUOTE>
	<P>
<HR>
<FONT COLOR="#000077"><B>NOTE:</B></FONT><B> </B>One thing that is not built into
	BCB that can help you create robust programs is a good object-modeling tool. There
	are some tools, such as the products 
called WithClass and Snorkle, that are designed
	to work with the Delphi VCL and should soon appear in a BCB-based format. <BR>
	<BR>
	It is worth pointing out that it is easy to draw object hierarchies using some form
	of custom or agreed upon 
notation. Programs such as Visio or Playground can help
	with this process. Even if there is no direct code generation involved, there is
	still an enormous benefit to be derived from this process. <BR>
	<BR>
	I have worked on projects I thought had 
gone hopelessly astray and could not ever
	be salvaged. These &quot;lost causes&quot; were saved by simply drawing out my object
	hierarchy with a tool that would let me rearrange its elements in several different
	patterns. C++ is a great language, 
but it offers no means for providing an overview
	of your object hierarchy. Drawing the object hierarchy with a simple object notation
	can help enormously when it is not clear how to design a particular feature or when
	you need to try to salvage a 
product that has gone astray. 
<HR>


</BLOCKQUOTE>

<P>BCB's object-oriented, component-based architecture makes programming easier than
it used to be. That doesn't mean that now everyone will be able to program. It just
means that now the best 
programmers can make better applications. The key terms are
reuse, visual design tools, components, and objects. If you can find an object-modeling
tool that can aid in program development, you will be even further ahead.
<H3><A 
NAME="Heading6"></A><FONT COLOR="#000077">Creating Simple Objects</FONT></H3>
<P>To start a discussion of objects, it might be a good idea to cut the VCL out of
the picture as much as possible. This will eliminate the complex object hierarchy

associated with the VCL. In its place, you can construct some very simple objects
with a known hierarchy that is easy to define. As the discussion progresses, the
VCL can be introduced into the programs in a planned and sensible manner.

<DL>
	
<DD><B>1.</B> Start a new project.<BR>
	<BR>
	<B>2.</B> Bring up the Project Manager from the View menu and remove <TT>Form1.cpp</TT>
	and the project resource file.<BR>
	<BR>
	<B>3.</B> Go to the View menu again and choose Project Source.<BR>
	<BR>
	
<B>4.</B> Go to Options | Project | Linker and choose Console application, as shown
	in Figure 19.1.
</DL>

<P><BR>
<A NAME="Heading7"></A><A HREF="19ebu01.jpg" tppabs="http://pbs.mcp.com/ebooks/0672310228/art/19/19ebu01.jpg">FIGURE 19.1.</A><FONT COLOR="#000077">
</FONT><I>Creating a console application in 
BCB.</I></P>
<P>Edit the main source file for the project so it looks like this:</P>
<PRE><FONT COLOR="#0066FF">#include &lt;stdio.h&gt;

int main(void)

{

  printf(&quot;Daughters of Time, the hypocritic Days,\n&quot;);

  printf(&quot;Muffled and 
dumb like barefoot dervishes\n&quot;);

  printf(&quot;-- Ralph Waldo Emerson&quot;);

  return 0;

}

</FONT></PRE>
<P>Save this file as <TT>Object1.mak</TT>. It is now a complete application that
circumvents the VCL. If you open up a DOS window and 
run the program from the DOS
prompt, the output looks like Figure 19.2.<BR>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -