Tuesday 14 February 2017

Creating own classes

Visual C++ provides several convenient ways to add error-free class declarations to your project.
ClassView, WizardBar, ClassWizard, and the New Class command (Insert menu) assist you with adding classes to an application. All the tools use the same interface (the New Class dialog), except that ClassWizard is limited to MFC-based classes. With ClassView, WizardBar, and the New Class command, you can create MFC (Microsoft Foundation Classes), ATL (Active Template Library), or generic (user-defined) classes. The type of class you add depends on your project type. For example, the default class for an MFC project is an MFC class. However, you can add ATL support to your MFC projects, just as you can add MFC support to your ATL projects.
You can also use the New Class dialog box to add a new class to existing class header and implementation files.
For ATL–based and MFC automation-enabled classes, you define the COM interfaces for the class when you create it. Defining interfaces is not part of creating a user-derived class.
The New Class Dialog Box
ClassView, WizardBar and ClassWizard all use the New Class dialog box to assist you in adding classes to your projects.

To open the New Class dialog box
Right-click on the project icon in ClassView, and click New Class on the shortcut menu.
-or-
On the WizardBar Action menu, click New Class.
-or-
On the Visual C++ Insert menu, click New Class.
-or-
On the View menu, click ClassWizard. Click ClassWizard's Add Class button, and on the menu click New.
The New Class dialog box has four variants: one for MFC classes, one for ATL classes, one for Generic classes, and one for Form-based classes. The development environment detects the active project type, and modifies the New Class dialog box accordingly. The following table describes the class types that are available from the New Class dialog box for MFC and ATL projects.
Adding an MFC Class
To add classes derived from MFC (Microsoft Foundation Class Library) classes to your project, you can use any of the following tools: ClassView, WizardBar, ClassWizard, or theNew Class command on the Insert menu. You simply specify the new class's name, select the base class from which it is to be derived, and select the ID of the dialog box with which it is to be associated (if any). WizardBar creates a header file and an implementation file for the new class.
Note   You can add MFC classes to an ATL COM application if you initially created the application with MFC support.
To add an MFC class to your project
  1. Open the New Class dialog box as described in the New Class dialog box.
  2. In the Class type drop-down list, select MFC Class.
  3. In the Name box, specify the name of the new class.
The name you specify is reflected in the read-only File name box, where the .cpp file is specified. By default, the header file and the implementation file have the same name as the class file.
    • To change the names of the header and implementation files for this class, click Change and type the names in the Change Files dialog box.
    • To add the new class to existing files, click Change, then click the Browse button and locate the files you wish to add it to.
  1. Choose a base class from which to derive the new class. There are two kinds of MFC classes:
    • Those, such as CButton, that do not require a resource ID Classes such as CButton or CEdit that are derived from base classes other than CDaoRecordViewCDialogCFormViewCPropertyPage, or CRecordView do not require a resource ID. Because these classes do not require a resource ID, the New Class dialog's Dialog ID drop-down list is not active.
    • Those, such as CDialog, that do require a resource ID.
For classes that require a resource ID (classes derived from CDaoRecordViewCDialogCFormViewCPropertyPage, or CRecordView), it is more efficient if you first use the dialog editor to create the resource and its ID, then use ClassWizard to create a class associated with that resource ID.
However, if you create the class first, then the resource, you can later associate the class to the resource using the procedure described in Associate an Existing Class with a Resource.
For information on using the resource editors, see Resource Editor Topics (Specific to Visual C++).
For information about creating database classes (CRecordViewCRecordset) and OLE classes, see the article ClassWizard: Database Support.
  1. For dialog-based classes, select the resource with which the class is to be associated from the Dialog ID drop-down list.
  2. Select the type of Automation support: NoneAutomation, or Createable by type ID. (Note that the base class must support Automation in order for any Automation options to be available.)
    • Select None if you want the new class to have no Automation capability.
    • Select Automation if you want to expose the capabilities of the new class through Automation.
If you select this option, the newly created class will be available as a programmable object by automation client applications, such as Microsoft Visual Basic™ and Microsoft Excel. This option is available only for some classes.
    • Select Createable by type ID if you want to allow other applications to create objects of this class using Automation.
With this option, automation clients can directly create an Automation object. The type ID in the text box is used by the client application to specify the object to be created; it is system-wide and must be unique. This option is available only for some classes.
  1. Note   Use ClassWizard's Automation tab to add Automation methods and properties to an existing class. These methods and properties define a dispatch interface that Automation clients can use.
  2. Click OK to create the new class and add it to the project.
The new class immediately appears in ClassView, and you can view its header and implementation files in the FileView pane of the 
Adding a Generic Class
A generic class is a class that you define or that is derived from a class that you define. To add a generic class, you can use ClassView, WizardBar, or the New Class command (Insert menu).
To add a generic class to your project
  1. Open the New Class dialog as described in the New Class dialog box.
  2. From the Class type drop-down list, select Generic Class.
  3. In the Name box, specify the name of the new class.
The name you specify is reflected in the read-only File name box, where the .cpp file is specified. By default, the header file and the implementation file have the same name as the class file.

    • To change the names of the header and implementation files for this class, choose the Change button and type the names into the Change Files dialog.
    • To add the new class to existing files, click Change, then click the Browse button and locate the files you wish to add it to.
  1. Under Base Classes, in the Derived From column, type the name of the base class from which to derive the new class. In the As column, click the access the new generic class is to have with regard to the base class: publicprotected, or private.

No comments:
Write comments