Adding Member Variables to Classes

  1. Start Microsoft Visual Studio .NET, select New from the File menu, and then select Project. The New Project dialog box appears.

  2. Set the Project Type to Visual C++ Projects, and set the Template to Console Application (.NET). Type Variables in the Name text box, and choose a location for your new project. Click OK.

  3. From the Project menu, select Add Class. The Add Class dialog box appears.

  4. Set the Template to Generic C++ Class. Click Open. The Generic C++ Class Wizard dialog box appears.

  5. Type Animal in the Class name text box. Notice how default names for the header and source files are created automatically.

  6. Leave the Base class text box blank.

  7. In the Access list, select public. (Note that public is the default, so it should already be selected.)

  8. Check the Virtual destructor check box.

    click to expand

  9. Click Finish.

    Note that the header file for the new class, Animal.h, has been loaded into the main window. In Solution Explorer, the Animal.cpp file has been added to the Source Files folder and the header file, Animal.h, has been added to the Header Files folder. In the Class View window, if you expand the Variables project node, you’ll see that the new Animal class has been added to the project. If you expand the Animal class, you’ll see the default constructor, Animal(void), and destructor, ~Animal(void), for the class.

  10. In the Class View window, expand the Variables node, right-click the Animal class, select Add, and then select Add Variable. Accept the default access modifier, public.

  11. Select int from the Variable type drop-down menu.

  12. Type legs in the Variable name text box.

    click to expand

  13. Click Finish.

In this exercise, you’ve seen how to create a new class using Visual Studio and how to add members to it.

Microsoft Visual C++  .NET(c) Step by Step
Microsoft Visual C++ .NET(c) Step by Step
ISBN: 735615675
Year: 2003
Pages: 208 © 2008-2017.
If you may any questions please contact us: