Practical Learning Logo

Managed Classes

Introduction

A class is referred to as managed if it lets the compiler take care of cleaning the memory space it was using once an object declared from that class is not used anymore. This means that, for a class to be managed, first it must be created as such. Second, when an instance of that class is needed, it must be dynamically created, using a pointer.

  1. Start Microsoft Visual C++ .Net or MS Visual Studio .Net
  2. From the main menu, click File -> New -> Project...
  3. In the New Project dialog box, on the Project Types tree list, click the Visual C++ Projects node
  4. In the Templates list view, click Managed C++ Empty Project
  5. In the Name text box, replace the contents with Introduction to Managed Classes
     
    New Project
  6. In the Location text box, specify your desired project path. Otherwise, type C:\Programs\MSVC.Net and click OK

Creating a Simple Managed Class

To create a managed, you must use the __gc keyword. This lets the compiler know that an instance of the class will be garbage collection (the gc stands for Garbage Collection).

  1. To create a simple managed class, on the main menu, click Project -> Add New Item...
  2. From the Add New Item dialog box, in the Categories tree view, make sure that Visual C++ is selected. In the Templates list, click Header File (.h)
  3. In the Name text box, replace the contents with Recto and click Open
  4. Replace the empty file with the following:
     
    #pragma once
    #using <mscorlib.dll>
    
    __gc class CRectangle
    {
    public:
    	CRectangle();
    	CRectangle(double L, double H);
    	~CRectangle();
    	double getLength();
    	void setLength(double L);
    	double getHeight();
    	void setHeight(double H);
    	void setDimensions(double L, double H);
    	double Perimeter();
    	double Area();
    private:
    	double Length;
    	double Height;
    };
  5. To implement the class, on the main menu, click Project -> Add New Item...
  6. In the Templates list, click C++ File (.cpp)
  7. In the Name text box, replace the contents with Recto and click Open
  8. Replace the contents of the empty file with the following:
     
    #include "recto.h"
    
    CRectangle::CRectangle()
    	: Length(0.00), Height(0.00)
    {
    }
    
    CRectangle::CRectangle(double L, double H)
    	: Length(L), Height(H)
    {
    }
    
    CRectangle::~CRectangle()
    {
    }
    
    double CRectangle::getLength()
    {
    	return Length;
    }
    
    void CRectangle::setLength(double L)
    {
    	Length = L;
    }
    
    double CRectangle::getHeight()
    {
    	return Height;
    }
    
    void CRectangle::setHeight(double H)
    {
    	Height = H;
    }
    
    void CRectangle::setDimensions(double L, double H)
    {
    	setLength(L);
    	setHeight(H);
    }
    
    double CRectangle::Perimeter()
    {
    	return 2 * (Length + Height);
    }
    
    double CRectangle::Area()
    {
    	return Length * Height;
    }
  9. Save the project

 

 

Inheriting From a .Net Class

Just as you would create a managed class simply by using the __gc keyword, to create your own custom class using a .Net existing class, using the __gc keyword and inherit from the desired class, provided the class can be inherited. The simplest class you can inherit from is the form, which allows you to create a fundamental application.

In the previous example, we create a class by defining its header file first, then its source file. From the early uses of MFC, Microsoft Visual C++ allows you to create a class, combining a header and a source files in one step. Unfortunately, probably because MSVC .Net is still in its early stages, the dialog box used to add a class doesn't provide an option to make the class a managed object. You will have to edit the file(s) manually. Otherwise, you can still create your class in two steps.

  1. To create a class inherited from Form, on the main menu, click Project -> Add New Item...
  2. In the Categories tree view, make sure that Visual C++ is selected. In the Templates list view, click Header File (.h)
  3. Replace the contents of the Name text box with SimpleForm and click Open
  4. Define the class as follows:
     
    #pragma once
    #using <mscorlib.dll>
    #using <System.dll>
    #using <System.Drawing.dll>
    #using <System.Windows.Forms.dll>
    
    using namespace System;
    using namespace System::Drawing;
    using namespace System::Windows::Forms;
    
    __gc class CSimpleForm : public Form
    {
    public:
    	CSimpleForm();
    	void InitForm();
    private:
    	GroupBox* grpBox;
    	Label *lblLength;
    	Label *lblHeight;
    	TextBox* txtLength;
    	TextBox* txtHeight;
    	Label* lblPerimeter;
    	Label* lblArea;
    	TextBox* txtPerimeter;
    	TextBox* txtArea;
    	Button* btnCalculate;
    	void CalculateClick(Object *Sender, EventArgs *Args);
    	Button* btnClose;
    	void CloseClick(Object *Sender, EventArgs *Args);
    };
  5. To implement the class, on the main menu, click Project -> Add New Item...
  6. In the Categories tree view, make sure that Visual C++ is selected. In the Templates list view, click C++ File (.cpp)
  7. Replace the contents of the Name text box with SimpleForm and click Open
  8. Implement the class as follows:
     
    #include "simpleform.h"
    #include "Recto.h"
    #using <mscorlib.dll>
    
    CSimpleForm::CSimpleForm()
    {
    	InitForm();
    }
    
    void CSimpleForm::InitForm()
    {
    	this->Text = S"Rectangle Characteristics";
    	this->Size = Drawing::Size(336, 224);
    	this->MaximizeBox = false;
    	this->MinimizeBox = false;
    	this->StartPosition = FormStartPosition::CenterScreen;
    	this->FormBorderStyle = FormBorderStyle::FixedDialog;
    
    	this->grpBox = new GroupBox;
    	this->grpBox->Location = Point(16, 8);
    	this->grpBox->Size = Drawing::Size(210, 170);
    	this->grpBox->Text = S"Rectangle";
    
    	this->lblLength = new Label;
    	this->lblLength->Location = Point(16, 32);
    	this->lblLength->AutoSize = true;
    	this->lblLength->Text = S"&Length:";
    	this->grpBox->Controls->Add(lblLength);
    
    	this->txtLength = new TextBox;
    	this->txtLength->Location = Point(80, 32);
    	this->txtLength->Text = S"0.00";
    	this->grpBox->Controls->Add(txtLength);
    
    
    	this->lblHeight = new Label;
    	this->lblHeight->Location = Point(16, 64);
    	this->lblHeight->AutoSize = true;
    	this->lblHeight->Text = S"&Height:";
    	this->grpBox->Controls->Add(lblHeight);
    	
    	this->txtHeight = new TextBox;
    	this->txtHeight->Location = Point(80, 64);
    	this->txtHeight->Text = S"0.00";
    	this->grpBox->Controls->Add(txtHeight);
    
    	this->lblPerimeter = new Label;
    	this->lblPerimeter->Location = Point(16, 104);
    	this->lblPerimeter->AutoSize = true;
    	this->lblPerimeter->Text = S"&Perimeter:";
    	this->grpBox->Controls->Add(lblPerimeter);
    	
    	this->txtPerimeter = new TextBox;
    	this->txtPerimeter->Location = Point(80, 96);
    	this->txtPerimeter->Text = S"0.00";
    	this->grpBox->Controls->Add(txtPerimeter);
    
    	this->lblArea = new Label;
    	this->lblArea->Location = Point(16, 136);
    	this->lblArea->AutoSize = true;
    	this->lblArea->Text = S"&Area:";
    	this->grpBox->Controls->Add(lblArea);
    
    	this->txtArea = new TextBox;
    	this->txtArea->Location = Point(80, 128);
    	this->txtArea->Text = S"0.00";
    	this->grpBox->Controls->Add(txtArea);
    
    	this->Controls->Add(grpBox);
    
    	this->btnClose = new Button;
    	this->btnClose->Location = Point(240, 12);
    	this->btnClose->Text = S"&Close";
    	this->btnClose->add_Click(new EventHandler(this, CloseClick));
    	this->Controls->Add(btnClose);
    
    	this->btnCalculate = new Button;
    	this->btnCalculate->Location = Point(240, 40);
    	this->btnCalculate->Text = S"C&alculate";
    	this->btnCalculate->add_Click(new EventHandler(this, CalculateClick));
    	this->Controls->Add(btnCalculate);
    }
    
    void CSimpleForm::CloseClick(Object *Sender, EventArgs *Args)
    {
    	Close();
    }
    
    void CSimpleForm::CalculateClick(Object *Sender, EventArgs *Args)
    {
    	double dblLength = txtLength->Text->ToDouble(0);
    	double dblHeight = txtHeight->Text->ToDouble(0);
    
    	CRectangle *Recto = new CRectangle;
    
    	Recto->setLength(dblLength);
    	Recto->setHeight(dblHeight);
    
    	this->txtPerimeter->Text = Recto->Perimeter().ToString();
    	this->txtArea->Text = Recto->Area().ToString();
    }
  9. Save the project.

Using Managed Classes

Because the memory used by a managed in handled by the compiler, all instanced of managed classes must by dynamically created, using the new operator.

  1. On the main menu, click Project -> Add New Item...
  2. In the Categories tree view, make sure that Visual C++ is selected. In the Templates list view, click C++ File (.cpp)
  3. Replace the contents of the Name text box with Main and click Open
  4. Implement the file as follows:
     
    #using <mscorlib.dll>
    #include "SimpleForm.h"
    
    int _stdcall WinMain()
    {
    	CSimpleForm *SF = new CSimpleForm;
    	Application::Run(SF);
    
    	return 0;
    }
  5. To test the application, press Ctrl + F5


Home Copyright © 2002-2015 FunctionX