📄 ch16.htm
字号:
the existing functionality of the base object. This allows you to define the descendentobject in terms of how it's different from the base object.</P><P>Let's look at how this could work with a thermostat. Suppose you had a basic thermostatthat you could use in just about any setting. You could set the temperature for itto maintain, and it would turn on the heating or the air-conditioning as needed tomaintain that temperature. Now let's say you needed to create a thermostat for usein a freezer. You could start from scratch and build a customized thermostat, oryou could take your existing thermostat and specify how the freezer version differsfrom the original. These differences might include that it's limited to turning onthe air conditioning and could never turn on the heater. You would probably alsoput a strict limit on the range of temperatures to which the thermostat could beset, such as around and below 32[infinity] Fahrenheit, or 0[infinity] Celsius. Likewise,if you needed a thermostat for an office building, you would probably want to limitthe temperature range to what is normally comfortable for people and not allow thetemperature to be set to an extremely cold or hot setting.</P><P>With inheritance in creating your own classes, this method just described representsthe same principle that you want to apply. If possible, you should start with anexisting C++ class that has the basic functionality that you need and then programhow your class is different from the base class that you inherited from. You havethe ability to add new data elements, extend existing functionality, or overrideexisting functionality, as you see fit.</P><P><H3><A NAME="Heading4"></A>Visual C++ Class Types</H3><P>In most application projects, when you are creating a new class, you have a fewoptions on the type of class that you are creating. These options are</P><P><UL> <LI>Generic class <P> <LI>MFC class <P> <LI>Form class</UL><P>Which of these types of classes you choose to create depends on your needs andwhat your class will be doing. It also depends on whether your class needs to descendfrom any of the MFC classes.</P><P><H4>Generic Class</H4><P>You use a generic class for creating a class that is inherited from a class youhave already created. This class type is intended for creating classes that are notinherited from any MFC classes (although you have already seen where you need touse it to create classes that are based on MFC classes). If you want to create amore specialized version of the CLine class, for instance, a CRedLine class, thatonly drew in red, you create it as a generic class because it's inherited from anotherclass that you created.</P><P>When you create a generic class, the New Class Wizard tries to locate the declarationof the base class (the header file with the class declared). If it cannot find theappropriate header file, it tells you that you might need to make sure that the headerfile with the base class definition is included in the project. If the base classhappens to be an MFC class that is not accessible as an MFC class (such as CObject),then you can ignore this warning because the correct header file is already partof the project.</P><P><H4>MFC Class</H4><P>If you want to make a reusable class that is based on an existing MFC class, suchas an edit box that automatically formats numbers as currency, you want to createan MFC class. The MFC class type is for creating new classes that are inherited fromexisting MFC classes.</P><P><H4>Form Class</H4><P>The form class is a specialized type of MFC class. You need to create this typeof class if you are creating a new form style window. It can be a dialog, form view,or database view class. This new class will be associated with a document class foruse with the view class. If you are building a database application, you will probablycreate a number of this style of classes.</P><P><H2><A NAME="Heading5"></A>Creating Library Modules</H2><P>When you create new classes for your application, they might be usable in otherapplications as well. Often, with a little thought and effort, classes you createcan be made flexible enough so that they could be used in other applications. Whenthis is the case, you need some way of packaging the classes for other applicationswithout having to hand over all your source code. This is the issue that librarymodules address. They allow you to compile your classes and modules into a compiledobject code library that can be linked into any other Visual C++ application.</P><P>Library modules were one of the first means available to provide compiled codeto other programmers for use in their applications. The code is combined with therest of the application code by the linker as the final step in the compilation process.Library modules are still a viable means of sharing modules with other developers.All the developer needs is the library (.lib) file and the appropriate header filesthat show all the exposed classes, methods, functions, and variables, which the otherprogrammer can access and use. The easiest way to do this is to provide the sameheader file that you used to create the library file, but you can also edit the headerso that only the parts that other programmers need are included.</P><P>By using library files to share your modules with other programmers, you are arrangingthat your part of the application is included in the same executable file as therest of the application. Your modules are not included in a separate file, such asa DLL or ActiveX control. This results in one less file to be distributed with theapplication. It also means that if you make any changes to the module, fix any bugs,or enhance any functionality, then the applications that use your module must berelinked. Using library files has a slight disadvantage to creating DLLs, where youmay be able to just distribute the new DLL without having to make any changes tothe application, but you'll learn all about that tomorrow.</P><P><H2><A NAME="Heading6"></A>Using Library Modules</H2><P>To get a good idea of how to use library modules, it's helpful to create a librarymodule, use it in another application, and then make some modifications to the librarymodule. For today's sample application, you'll create a module that generates a randomdrawing on the window space specified. It'll be able to save and restore any of thesedrawings. You'll then use this module in an SDI application, where every time a newdocument is specified, a new drawing is generated. The initial module will only useeight colors and will generate only a limited number of line sequences. Later, you'llmodify the module so that it will generate any number of colors and will generatea larger number of line sequences.</P><P><H3><A NAME="Heading7"></A>Creating the Library Module</H3><P>To create a library module project, you need to specify in the New dialog thatyou want to create a Win32 Static Library, as shown in Figure 16.1. This tells VisualC++ that the output from the project compilation will be a library module insteadof an executable application. From there, all you have to do is define the classesand add the code. You have the options of including support for MFC and using precompiledheaders in your project, as in Figure 16.2, the only step in the Project Wizard.</P><P>The library that you will create for today's sample application will consist oftwo classes. The first class will be the CLine class that you first created on Day10, "Creating Single Document Interface Applications." The second classwill be the class that creates the random drawings on the drawing surface. This classwill contain an object array of the CLine objects that it will create and populatewith each of the drawing efforts. This second class will also need functionalityto save and restore the drawing, as well as to delete the existing drawing so thata new drawing can be started. It will need to know the dimensions of the drawingarea so that it can generate a drawing that will fit in the drawing area. Once youcreate this module, you'll take a look at how you can use this module in an applicationproject.</P><P><A HREF="javascript:popUp('16fig01.gif')"><B>FIGURE 16.1.</B></A><B> </B><I>Specifyinga library module project.</I></P><P><A HREF="javascript:popUp('16fig02.gif')"><B>FIGURE 16.2.</B></A><B> </B><I>Specifyingproject support options.</I></P><P><I></I><H4>Creating a Library Project</H4><P>To start the library project for today's example, you need to create a new project,specifying that the project is a Win32 Static Library project. Give the project asuitable name and click OK to create the project.</P><P>For today's sample project, specify on the one wizard step to include both MFCand precompiled header support. Although the precompiled header support is not necessary,it will speed up most compiles that you perform while building the module.</P><P>Once you create your module project, you'll find yourself working with a projectthat has no classes. You've got a blank slate from which you can create
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -