< .Net Type, Variable & Operator 2 | Main | .Net Type, Variable & Operator 4 >


Data Types, Variables and Operators 3




The discussion and the codes used supposed to be based on the new C++ .NET. The following are the topics in this part.

  1. Adding Member Variables to Classes



Adding Member Variables to Classes


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


Invoking the C++ .Net project window


  1. Set the Project Type to CLR, and set the Template to CLR Console Application. Type Variables in the Name text box, and choose a location for your new project. Click OK.


New C++ .Net project windows - selecting project type and the respective template


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


Adding a generic class to the project


  1. Set the Categories to C++ and Template to C++ Class. Click Add. The Generic C++ Class Wizard dialog box appears.


The add class categories and templates page





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


Generic C++ .Net class wizard page - entering class information


  1. Leave the Base class text box blank.

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

  3. Check the Virtual destructor check box.


Completing the class information


  1. 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.


Project files and categories seen in Solution Explorer


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.


Class default constructor


  1. Add the following member variable to the Animal class. For Visual Studio, you may select the Animal class folder in the Class View and right click. Then select Add Member Variable menu.









Table 5


Adding new member variable to the class


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


Part 1 | Part 2 | Part 3 | Part 4 | Part 5 | Part 6 | Part 7 | Part 8 | Part 9 | Part 10



< .Net Type, Variable & Operator 2 | Main | .Net Type, Variable & Operator 4 >