Home

Introduction to Microsoft Visual C++

 

Microsoft Visual C++ Fundamentals

 

Introduction

Computer programming is the art of creating written instructions that tell the computer what to do, how to do it, and when to do it. To write such instructions, a programmer uses a computer language. One of the most regularly used languages is called C. Another language, based on C, is called C++.

The earliest implementation of the C and C++ language from Microsoft used to be called Microsoft C/C++. When graphical programming started to be considered, Microsoft released Microsoft Visual C++ 1.0 which allowed performing application programming for the Microsoft Windows operating system. With the releases of Microsoft Windows 3.X versions, the operating system was using a library referred to as Win16 that contained all necessary functions and objects used to create graphical user interface (GUI) applications. This library made it a little easier to "talk" to the operating system and create fairly good looking applications.

After a while, Microsoft released Microsoft Windows 95 followed by Windows 98. The operating system library was also updated to Win32. Win32 was (in fact is) just a library. Like most or all other libraries, it is not a programming language. It is a documentation that allows programmers to know how they can use the library to create applications that can run on a Microsoft Windows-lead computer. This means that, to use it, programmers needed a language such as C, C++, or Pascal, etc, to actually create the desired applications. Win32 was written in C.

Since using Win32 requires a good level of understanding and work, Microsoft released a customized version of the Win32 library to make it easier to comprehend. The library is called Microsoft Foundation Class Library (MFC). Because Win32 was written in C and MFC was written for C/C++ developers, programmers of other languages would not understand it. To accommodate others, in fact to make Windows programming accessible to everybody, Microsoft created Microsoft Visual Basic as a true graphical programming environment. Visual Basic used a different and simpler language, called Basic as its foundation. The first consequence was that Visual Basic didn't inherently "understand" Win32. To allow Visual Basic programmers to use what was available from Win32, its programmers had to import the needed functions. This resulted in Visual C++ programmers using one library and Visual Basic programmers using another; this being only one of many problems.

With the growth of the Internet, the client/server applications, demanding databases, and communication software, etc, Microsoft decided to create a new library that can be understood and used by many different languages. The library was called the .NET Framework. This was meant to accomplish the goal of both Visual C++ and Visual Basic using the same library. Another goal was to have programmers of different languages work on the same project without having each one learning the other's language. While on the subject, Microsoft also developed other languages such as C#, J#, or JScript and other programming environments that use these languages, such as Visual C# or Visual J# to also be part of this family that share the .NET Framework library.

The .NET Framework

A library called .NET Framework combines the new classes, functions, and constants that can be used to create applications to run for a Microsoft Windows computer. This library allows you to create graphical, databases, XML-based, web services, and web-based applications. Once an application has been created and is satisfactorily functional, it can be distributed to people who can use it. To make this possible, the .NET Framework is implemented in two systems: the class library and the common language runtime (CLR).

The .NET Framework class library is a collection of fully functional object-oriented classes in the tradition of C++ covering all possible areas of application programming including Windows controls, file processing, XML, communication, web, databases, etc. This library is created to be used by various computer languages or programming environments. To take advantage of what this library offers, you must use a computer language that is adapted to "understand" this library. One of the languages that understand it is commonly referred to as C++/CLI. That's the language we will use for our lessons.

After an application has been created, it must be compiled so it can be used as necessary. Another goal of the .NET Framework is to make it possible for a library or service created from one language to be used in an application created using another language. To make this possible, code must be compiled so that an external engine, and not the environment used to create code, can compile, manage, and execute it. That's the role of the common language runtime (CLR). The CLR takes care of code execution, memory cleanup, thread management, security, etc. Such code is referred to as managed code. Code that is not compiled into the CLR is referred to as unmanaged code.

The primary objects used in a .NET Framework application are stored in the System.dll assembly. You can manually or visually include this library in your project. To include it manually, you would type the following:

#using <System.dll>

To include it visually, you would use the Add Reference dialog box to select System from the .NET property page:

The primary objects, such as data types, strings, arrays, etc, used in an application are part of the System namespace implemented in the System.dll library. To use the System namespace in your application, you can manually include it as follows:

using namespace System;

If you create a Windows Forms Application, the System.dll assembly and the System namespace would automatically be added to your project.

Our Visual C++ Lessons

Because many programs had earlier been written using the MFC library, Microsoft decided to continue publishing it and the MFC is still part of Microsoft Visual C++ 2005. This resulted in many libraries being included in Visual C++.

These lessons will address only the .NET side of Microsoft Visual C++ 2005. In other words, our lessons deal only with the way the .NET Framework is implemented in Microsoft Visual C++ 2005.

These lessons assume that you have installed either Microsoft Visual C++ 2005 Professional or Microsoft Visual C++ 2005 Express Edition. You can download Microsoft Visual C++ 2005 Express Edition free from the Microsoft web site. Additionally, you should have Microsoft SQL Server 2005. If you don't have it, you can download an evaluation edition from the Microsoft web site.

Practical LearningPractical Learning: Launching Microsoft Visual Studio 2005

  • To start Microsoft Visual Studio 2005, on the Taskbar, click Start (All) Programs -> Microsoft Visual Studio 2005 -> Microsoft Visual Studio 2005 2005
     
    Integrated Development Environment

The Integrated Development Environment: The Title Bar

After Microsoft Visual Studio 2005 has been opened, its interface is also called an Integrated Development Environment or IDE. The IDE is the set of tools you use to create a program.

The system icon is used to identify the application that you are using. Almost every application has its own system icon. The system icon holds its own list of actions. For example, it can be used to move, minimize, maximize or close (when double-clicked) a window.

When you freshly start Visual Studio 2005, the main section of the title bar displays the name of the application as Microsoft Visual Studio. Later on, if you start a project, the title bar would display the name of your project, followed by the name of the programming environment you selected.

The main section of the title bar is also used to move, minimize, maximize the top section of the IDE, or to close Visual Studio. On the right section of the title bar, there are three system buttons.

 

The Integrated Development Environment: The Main Menu

Under the title bar, there is the main menu. In our lessons, the expression “Main Menu” refers to the menu on top of the IDE. To use a menu, you click one of its words and the menu expands. If an item is missing from the main menu, you can customize it. You can also use an access key, such as the letter F for the File menu or the letter E for the Edit menu. The menu is given focus by pressing either the Alt or the F10 keys.

To perform some operations, you can use a shortcut. From now on, in our lessons,

Press Means
T Press the T key
Alt, G Press and release Alt. Then press G
Ctrl + H Press and hold Ctrl. While you are still holding Ctrl, press H once. Then release Ctrl
Ctrl + Shift + E Press and hold Ctrl. Then press and hold Shift. Then press E once. Release Ctrl and Shift
 

Projects Fundamentals

 

Creating a Project

These lessons assume that you already know the C++/CLI language, but only the language. We assume that you have no prior knowledge of graphical application programming. In our lessons, we will create Windows applications, the type referred to as graphical user interface (GUI).

To create a Visual C++ 2005 project:

  • On the Start Page, on the right side of Create, you can click Project
  • On the main menu, you can click File -> New -> Project...
  • You can press Ctrl + Shift + N

Any of these actions opens the New Project dialog box. You can then select Visual C++, select the type of project, give it a name, specify its directory, and click OK.

Practical LearningPractical Learning: Using the Main Menu

  1. On the main menu, click File -> New -> Project...
  2. In the Project Types list of the New Project dialog box, click the Visual C++ node 
  3. In the Templates list, click Windows Forms Application
  4. Replace the content of the Name box with Exercise1
     
    New Project
  5. Click OK
    This creates a new project

Compiling and Executing a Project

After (eventually) creating a program, you must prepare it to show you a result. This is done by compiling the project. To compile and execute a Microsoft Visual C++ project in one step, on the main menu, you can click Debug -> Start Without Debugging. Although there are other techniques or details in compiling (or debugging) and executing a project, for now, this is the only technique we will use until further notice.

Practical LearningPractical Learning: Executing a Project

  1. To execute the application, on the main menu, click Build -> Build Exercise1

  2. To execute the application, on the Standard toolbar, click the Start Debugging button

  3. After viewing the result, close it

Opening a Project

As opposed to creating a new project, you can open a project that either you or someone else created. To open an existing project:

  • On the Start Page, on the right side of Open, you can click Project
  • On the main menu, you can click File -> Open -> Project...
  • You can press Ctrl + Shift + O
  • On the main menu, you can click File -> Recent Projects. If the desired project is in the list, you can then click it

Any of these actions would display the Open Project dialog box. You can then select a project and open it.

Design and Run Times

Application programming primarily consists of populating it with objects called Windows controls. These controls are what the users of your application use to interact with the computer. As the application developer, one of your jobs will consist of selecting the necessary objects, adding them to your application, and then configuring their behavior.

There are various ways you can get an object into your application. One of the techniques consists of programmatically adding a control by writing code. Another technique consists of visually selecting a control.

To create your applications, there are two settings you will be using. If a control is displaying on the screen and you are designing it, this is referred to as Design Time. This means that you have the ability to manipulate the control. You can visually set the control’s appearance, its location, its size, and other necessary or available characteristics. The design view is usually the most used and the easiest because you can glance at a control, have a realistic display of the control and configure its properties. The visual design is the technique that allows you to visually add a control and manipulate its display. This is the most common, the most regularly used, and the easiest technique.

The other technique you will be using to control a window is with code, writing the program. This is done by typing commands or instructions using the keyboard. This is considered, or referred to, as Run Time. This is the way you can control an object’s behavior while the user is interacting with the computer and your program.

The Studio Windows

 

The Toolbars

A toolbar is an object made of buttons. These buttons provide the same features you would get from the (main) menu, only faster. Under the main menu, the IDE is equipped with an object called the Standard toolbar. For example, to create a new project, on the main menu, you could click File -> New -> Project… On the other hand, the Standard toolbar is equipped with the New Project button New Project to perform the same action a little faster.

By default, the Standard toolbar is positioned under the main menu but you can position it anywhere else on the IDE. To move a toolbar, position the mouse on the dotted line on its left section. The mouse pointer will change into a cross:

Toolbar

Then click and drag away from that position:

Moving a Toolbar

In the same way, you can position the toolbar anywhere on the screen. You can also attach or "dock" it to one of the four sides of the IDE. When a toolbar is not docked to one side of the IDE, it is said to float. When a toolbar is floating, you can resize it by dragging one of its borders. If a toolbar is floating, to put it back to its previous position, you can double-click its title bar.

By default, when you start Microsoft Visual Studio, it is equipped with one toolbar: Standard. To get more toolbars, on the main menu, you can click View -> Toolbars and click the toolbar of your choice. You can also right-click any available toolbar or the main menu. This displays a list of all the available toolbars. Those that are currently opened have a check mark next to them.

A toolbar is equipped with buttons that could be unfamiliar. Just looking at one is not obvious. To know what a button is used for, you can position the mouse on top of it. A tool tip will come up and display for a few seconds.

In our lessons, each button on any toolbar will be named after its tool tip. This means that, if a tool tip displays "Hungry", its button will be called the Hungry button. If a tool tip displays "Exercises and Assignments", its button will be called the Exercises and Assignments button. If you are asked to click a button, position your mouse on different buttons until one displays the referred to name.

Some buttons present an arrow on their right side. This arrow represents a menu.

Like the menu, the toolbars can be customized.

Practical LearningPractical Learning: Customizing a Toolbar

  1. To customize the Standard toolbar by adding buttons to it, right-click anything on the main menu or the toolbar and click Customize...
  2. On the Customize dialog box, click the Commands tab
  3. In the Categories list, click Debug
  4. In the Commands list, click and drag Start Without Debugging
  5. Position it somewhere in the Standard toolbar:
     
    Customizing the Standard Toolbox
  6. Release the mouse
  7. Click the Close button on the Customize dialog box

Dockable Windows

When creating your applications, you will use a set of windows that each accomplishes a specific purpose. Some windows are represented with an icon but hide the rest of the body. To display such a window, you can position the mouse on it. This would expand the window:

Dockable Windows

If you expand a window, it would display a title bar with two buttons. One is called Auto Hide and the other is the classic Close button:

Auto Hide

If you expand a window but find out you don't need it any more, you can just move the mouse away from it. The window would return to its previous state. Based on this functionality, if you are working with a window and move the mouse away from it, it would retract. If you need it again, you would have to reopen it using the same technique. If you are going to work with a certain window for a while, you can keep it open even if you move the mouse away. To do this, click the Auto Hide button. If clicked, the Auto Hide button changes from pointing left to pointing down.

When Visual Studio 2005 opens, it makes some windows necessary. These are the most regularly used windows. If you think that one of them is not regularly used in your types of assignments, you can remove it from the screen by clicking its Close button. All of the windows you can use are listed in the View menu. Therefore, if a window is not displaying, you can click View on the main menu and click a window of your choice.

By default, Visual Studio 2005 installs some windows to the left and some others to the right of the screen. You can change this arrangement if you want. To do this, expand a window, then drag its title bar to another location on the screen. Windows can then be "coupled", that is docked together to one side of the screen. When windows are grouped, they automatically create tabs, allowing you to select the desired one by clicking its tab.

The options available in windows display differently depending on the window and the items in it. Some items are organized in a tree list equipped with + or – buttons. To expand a list, you can click its + button. To collapse a list, click its – sign. Some other items appear as buttons.

The Server Explorer

The Server Explorer is an accessory that allows you to access SQL Server databases without using the physical server and without opening Microsoft SQL Server.

The items of this window display in a tree. To expand a node, you can click its + button. To collapse it, click its - button. in Lesson 23, we will see that we can drag some items to add to an application.

 

The Start Page

The Start Page is the first wide area that appears when Microsoft Visual Studio comes up. The section displays a title as Recent Projects. At any time, to display the Start Page:

  • You can click its tab on the left side just under the Standard toolbar
  • On the main menu, you can click View -> Other Windows -> Start Page
  • On the main menu, you can click Windows -> Start Page

If you have just installed Microsoft Visual Studio or have not previously opened a project, the Recent Projects section would be empty. Once you start creating and using projects, they display in the Recent Projects section by their names. Here is an example:

The Projects section of the Start Page

The middle section allows you to check new articles from Microsoft and partners directly from Visual Studio 2005 through an Internet connection. 

The Code Editor

 

Description

There are two main ways you will manipulate an object of your application, visually or using code. In the next lessons, we will explore details of visually designing a control. As you should have found out from learning C++/CLI, code of an application is ASCII text-based, written in plain English and readable to human eyes. For a Visual C++ application, you can use any text editor to write your code but one of Visual Studio's main strengths is the code editor that it has always brought. It is very intuitive.

The Code Editor is a window specially designed for code writing.

Author Note Although all languages of the Visual Studio programming environment share the Code Editor, once you have started a type of application, the Code Editor is adapted to the language you are using. Its parser (a program used internally to analyze your code) behaves according to the language of your choice. The features and behaviors of the Code Editor are also different, depending on your language.

To display the code editor, if you created an empty project, you can click Project on the main menu and select one of the options that would lead either to a blank text file or a a file that contains primary code.

The Code Editor is divided in 4 sections:

The Code Editor
 

Practical LearningPractical Learning: Introducing the Code Editor

  1. To display the Start Page, on the main menu, click View -> Other Windows -> Start Page
  2. In the Start Page, on the right side of Create, click Projects
  3. In the Project Types list of the New Project dialog box, click Visual C++ if necessary.
    In the Templates section, click CLR Empty Project
  4. Replace the content of the Name box with Geometry1 and press Enter

The Tabs Bar

The top section of the Code Editor displays tabs of property pages. Each tab represents a file. To add a new file to the project, on the main menu, you can click

  • File -> New -> File...
  • Project -> Add New Item...

Once in the Add New Item dialog box, in the Templates section, click the type of file you want to create, type a name in the Name text box, and press Enter. After the file has been created, it is represented by a tab in the top section of the Code Editor. In the same way, you can add as many files as you judge them necessary. Because there can be different types of files, each has an extension that allows you to know the type of file the tab is holding.

To access one of these files, you can click its corresponding tab. By default, the tabs display in the order their files were created or added to the project, from left to right. If you don't like that arrangement, click and drag its tab either left or right beyond the next tab:

Practical LearningPractical Learning: Creating Files

  1. On the main menu, click Project -> Add New Item...
  2. In the Categories list, click Code.
    In the Templates list, click Header File (.h)
  3. In the Name text box, replace the name with Square
     
    Add New Item
  4. Click Add
  5. Type the following in the empty file:
     
    #pragma once
    
    using namespace System;
    
    public ref class CSquare
    {
    public:
        CSquare();
        CSquare(double s);
    
    private:
        double _side;
    
    public:
        property double Side
        {
    	double get() {return _side; }
            void   set(double value) { _side = value; }
        }
    
        property double Perimeter
        {
            double get() {	return _side * 4; }
        }
    
        property double Area
        {
            double get() { return 0.00; }
        }
    };
  6. To add a source file, on the main menu, click Project -> Add New Item...
  7. In the Templates list, click C++ File (.cpp)
  8. Set the Name to Square and click Add
  9. In the empty file, type:
     
    #include "Square.h"
    
    CSquare::CSquare()
    	: _side(0.00)
    {
    }
    
    CSquare::CSquare(double s)
    	:_side(s)
    {
    }
  10. To create another source file, 
  11. on the main menu, click Project -> Add New Item...
  12. In the Templates list, click C++ File (.cpp)
  13. Set the Name to Exercise and click OK
  14. In the empty file, type:
     
    #include "Square.h"
    
    using namespace System;
    
    public ref class CExercise
    {
    public:
        static void ShowProperties()
        {
    	Console::WriteLine(L"Geometric Calculations\n");
        }
    };
    
    int main()
    {
        CExercise::Welcome();
    
        return 0;
    }
  15. To execute the application, press Ctrl + F5
  16. When asked whether you want to build the project, click Yes

 

  1.  

The Scopes Combo Box

The top-left section of the Code Editor displays a combo box named Scopes. The content of the Scopes combo box depends on the type of file specified in the tab. If the select tab represents a header file, the Scopes combo box has an item titled (Global Scope). The Scopes combo box also holds a list of the names of the classes and the properties, if any, defined in the file. You can display the list if you click the arrow of the combo box:

Scopes

If the selected tab represents a source file, the Scopes combo box has an item labeled (Global Scope). The combo box also holds a list of the classes and structures that are used in the current project.

The Functions Combo Box

The top-right section of the Code Editor displays a combo box named Functions. The content of the Functions combo box depends on the item that is currently selected in the Scopes combo box

  • If the current tab represents a header file and if you select the name of a class in the Scopes combo box, when you click the arrow of the Functions combo box, it would display a list of the methods of that class:

  • If the tab represents a header file and if you select the class::property combincation in the Scopes combo box, if you click the arrow of the Functions combo box, it would display a list of the accessors of the property. Here is an example:

  • If the tab represents a source file and if you select (Global Scope) in the Scopes combo box, when you click the arrow of the Functions combo box, it would display a list of the global functions (functions that don't belong to a class) defined in the file.
  • If the tab represents a source file and if you select the name of a class in the Scopes combo box, when you click the arrow of the Functions combo box, it would display a list of the methods (the function members that belong to a class) defined in the file. Here is an example:

Once you see an item in the Functions combo box, if you click it, the Code Editor jumps to that member in the file and selects that member in the Code Editor.

Practical LearningPractical Learning: Using the Members Combo Box

  1. Click the Square.cpp tab to display the source file. In the Scopes combo box, select CSquare::Area
  2. In the Functions combo box, select get
  3. Change its implementation as follows:
     
    #pragma once
    
    using namespace System;
    
    public ref class CSquare
    {
    public:
        . . . No Change
    
        property double Area
        {
            double get() { return _side * _side; }
        }
    };
  4. Click the Exercise.cpp tab and change the content of the file as follows:
     
    #include "Square.h"
    
    using namespace System;
    
    public ref class CExercise
    {
    public:
        static void ShowProperties(CSquare ^ sqr)
        {
            Console::WriteLine(L"Square Characteristics");
    	Console::WriteLine(L"Side:      {0}", sqr->Side);
            Console::WriteLine(L"Perimeter: {0}", sqr->Perimeter);
            Console::WriteLine(L"Area:      {0}", sqr->Area);
        }
    };
    
    int main()
    {
        CSquare ^ s = gcnew CSquare(38.62);
        CExercise::ShowProperties(s);
    
        return 0;
    }
  5. Execute the application to test it

Code Colors

Code is written in a wide area with a white background. This is the area you use the keyboard to insert code with common readable characters. The Code Editor uses some colors to differentiate categories of words or lines of text. The colors used are highly customizable. To change the colors, on the main menu, you can click Tools -> Options... In the Options dialog box, in the Environment section, click Fonts and Colors. To set the color of a category, in the Display Items section, click the category. In the Item Foreground combo box, select the desired color. If you want the words of the category to have a colored background, click the arrow of the Item Background combo box and select one:

The Options Dialog Box

In both cases, the combo boxes display a fixed list of colors. If you want more colors, you can click a Custom button to display the Color dialog box that allows you to "create" a color.

The Solution Explorer

 

Introduction

The Solution Explorer is a window that displays the file names and other items used in your project:

The items of this window display in a tree. To expand a node, you can click its + button. To collapse it, click its - button. To explore an item, you can double-click it. The result depends on the item you double-clicked.

Using the Solution Explorer

The Solution Explorer can be used to create a new class, a new resource, or a reference to the current project. To perform any of these operations, you can right-click a folder node such as the name of the project, the Source Files, the Header Files, or the Resource Files nodes, position the mouse on Add and select the desired operation. You can also perform any of these operations from the Project category of the main menu.

Besides adding new items to the project, you can also use the Solution Explorer to build the project or change its properties. If you add one or more other project(s) to the current one, one of the projects must be set as the default. That project would be the first to come up when the user opens the application. By default, the first project created is set as the default. If you have more than one project, to set the default, right-click the name of the desired project in Solution Explorer and click Set As StartUp Project.

The Solution Explorer also you to rename or delete some of the items that belong to your project.

Practical LearningPractical Learning: Using Solution Explorer

  1. To add a new project to the current project, on the main menu, click File -> Add -> New Project...
  2. In the Project Types list of the Add New Project dialog box, click Visual C++. In the Templates list, click Windows Forms Application
  3. Replace the content of the Name box with RoundShapes and press Enter
  4. In the Solution Explorer, right-click RoundShapes and click Set As StartUp Project. Notice that RoundShapes is in bold characters
  5. Still in the Solution Explorer, right-click RoundShapes and click Build
  6. When the Build process is over, on the Standard toolbar, click the Start button Start
  7. After using the form, to close it, click its system close button

The Class View

 

Introduction

The Class View displays the various classes used by your project, including their ancestry. The items of the Class View an organized as a tree list with the name of the project on top:

Class View

 

Using the Class View

The Class View shares some of its functionality with the Solution Explorer. This means that you can use it to build a project, to add new class or a new resource. You can also use it to change the default project if you have more than one.

While the Solution Explorer displays the items that are currently being used by your project, the Class View allows you to explore the classes used in your applications, including their dependencies. For example, sometimes you will be using a control of the of the .NET Framework and you may wonder from what class that control is derived. The Class View, rather than the Solution Explorer, can quickly provide this information. To find it out, expand the class by clicking its + button.

Practical LearningPractical Learning: Using the Class View

  1. Click the Class View tab
  2. To add a new class, right-click Geometry1, position the mouse on Add and click Add Class...
  3. In the Templates list, click C++ Class and click Add
  4. Change the name of the class to CRectangle and click Finish
  5. In the Class View, right-click CRectangle, position the mouse on Add and click Add Variable...
  6. Set the Access level to private
  7. Set the Variable Type to double
  8. Set the Variable Name to _length
     
  9. Click Finish
  10. In the same way, add a private member variable to the CRectangle class as double and named _height
  11. In Class View, right-click CRectangle, position the mouse on Add and click Add Function...
  12. Set the Return Type to double and the Function Name to Perimeter then click Finish
  13. In the same way, add a public method that returns a double and is named Area to the CRectangle class
  14. Complete the Rectangle.h header file as follows:
     
    #pragma once
    
    ref class CRectangle
    {
    private:
    	double _length;
    	double _height;
    
    public:
    	CRectangle(void);
    
    	property double Length
    	{
    		double get() { return _length; }
    		void set(double len) { _length = len; }
    	}
    
    	property double Height
    	{
    		double get() { return _height; }
    		void set(double hgt) { _height = hgt; }
    	}
    
    public:
    	double Perimeter(void);
    	double Area(void);
    };
  15. Complete the Rectangle.cpp source file as follows:
     
    #include "Rectangle.h"
    
    CRectangle::CRectangle(void)
    	: _length(0),
    	  _height(0)
    {
    }
    
    double CRectangle::Perimeter(void)
    {
    	return (_length + _height) * 2;
    }
    
    double CRectangle::Area(void)
    {
    	return _length * _height;
    }
  16. Change the Exercise.cpp source file as follows:
     
    #include "Square.h"
    #include "Rectangle.h"
    
    using namespace System;
    
    void ShowRectangle(CRectangle ^ rect);
    
    public ref class CExercise
    {
    public:
        static void ShowSquare(CSquare ^ sqr)
        {
            Console::WriteLine(L"Square Characteristics");
    	Console::WriteLine(L"Side:      {0}", sqr->Side);
            Console::WriteLine(L"Perimeter: {0}", sqr->Perimeter);
            Console::WriteLine(L"Area:      {0}\n", sqr->Area);
        }
    };
    
    int main()
    {
        CSquare ^ s = gcnew CSquare(38.62);
        CExercise::ShowSquare(s);
    
        CRectangle ^ recto = gcnew CRectangle;
        recto->Length = 44.12;
        recto->Height = 26.94;
        ShowRectangle(recto);
    
        return 0;
    }
    
    void ShowRectangle(CRectangle ^ rect)
    {
        Console::WriteLine(L"Rectangle Characteristics");
        Console::WriteLine(L"Length:    {0}", rect->Length);
        Console::WriteLine(L"Height:    {0}", rect->Height);
        Console::WriteLine(L"Perimeter: {0}", rect->Perimeter());
        Console::WriteLine(L"Area:      {0}\n", rect->Area());
    }
  17. In Class View, to change the current default project, right-click Geometry1 and click Set As StartUp Project
  18. On the main menu, click Debug -> Start Without Debugging
     
    Square Characteristics
    Side:      38.62
    Perimeter: 154.48
    Area:      1491.5044
    
    Rectangle Characteristics
    Length:    44.12
    Height:    26.94
    Perimeter: 142.12
    Area:      1188.5928
    
    Press any key to continue . . .
  19. Close the DOS window
 
 

Home Copyright © 2007, yevol.com Next