Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Руководство_по_C++_CLI.doc
Скачиваний:
4
Добавлен:
01.07.2025
Размер:
8 Мб
Скачать
☆

11.1.2Practical Learning: Introducing Data Reading

  1. To start a new program, launch Microsoft Visual C++ 2005

  2. On the main menu, click File -> New -> Project...

  3. On the left side, make sure that Visual C++ is selected. In the Templates list, click CLR Empty Project

  4. In the Name box, replace the string with RealEstate10 and click OK

  5. On the main menu, click Project -> Add New Item...

  6. To create a header file, in the Templates list, click Header File (.h)

  7. Set the Name to Property and click Add

  8. Complete the file as follows:  

#pragma once

using namespace System;

namespace RealEstate

{

public ref class CProperty

{

public:

long PropertyNumber;

String ^ Address;

String ^ City;

String ^ State;

String ^ ZIPCode;

Int Bedrooms;

float Bathrooms;

Int YearBuilt;

double MarketValue;

};

}

  1. To create another header file, on the main menu, click Project -> Add New Item...

  2. In the Templates list, make sure Header File (.h) is selected. Set the Name to House and press Enter

  3. Complete the file as follows:  

#pragma once

#include "Property.h"

using namespace System;

namespace RealEstate

{

public ref class CHouse : public CProperty

{

public:

Byte Stories;

Int Condition;

unsigned NumberOfGarageSpaces;

};

}

  1. To create one more header file, on the main menu, click Project -> Add New Item...

  2. In the Templates list, make sure Header File (.h) is selected. Set the Name to SingleFamily and click Add

  3. Complete the file as follows:  

#pragma once

#include "House.h"

using namespace System;

namespace RealEstate

{

public ref class CSingleFamily : public CHouse

{

public:

__wchar_t Style;

double LotSizeInSqFt;

};

}

  1. To create a source file, on the main menu, click Project -> Add New Item...

  2. In the Templates list, click C++ File (.cpp)

  3. In the New box, type Exercise and click Add

  4. In the empty file, type:  

#include <iostream>

#include "SingleFamily.h"

using namespace std;

using namespace RealEstate;

Int main()

{

String ^ strTitle1 = L"=//= Altair Realty =//=";

String ^ strTitle2 = L"-=- Properties Inventory -=-";

CSingleFamily ^ home = gcnew CSingleFamily;

Console::WriteLine(strTitle1);

Console::WriteLine(strTitle2);

Console::WriteLine();

return 0;

}

  1. Execute the application to test it

  2. Close the DOS window

11.1.3Returning a Handle

If you are defining a function and you want it to return a handle to a class, you can. To indicate this, between the name of the class and the name of the function, type the ^ operator.

11.1.4Practical Learning: Returning a Handle

  1. To return a handle, change the Exercise.cpp file as follows:  

#include <iostream>

#include "SingleFamily.h"

using namespace std;

using namespace RealEstate;

CSingleFamily ^ CreateListing()

{

CSingleFamily ^ home = gcnew CSingleFamily;

Console::WriteLine("To create a listing, enter

the following information");

Console::Write("Property #: ");

home->PropertyNumber = long::Parse(Console::ReadLine());

Console::WriteLine("Property Condition");

Console::WriteLine("0. Unknown");

Console::WriteLine("1. Excellent");

Console::WriteLine("2. Good (may need minor repair");

Console::WriteLine("3. Acceptable (needs major repair)");

Console::WriteLine("4. Even (land is more important)");

Console::Write("Your Choice: ");

home->Condition = int::Parse(Console::ReadLine());

Console::Write("Street Address: ");

home->Address = Console::ReadLine();

Console::Write("City: ");

home->City = Console::ReadLine();

Console::Write("State: ");

home->State = Console::ReadLine();

Console::Write("ZIP Code: ");

home->ZIPCode = Console::ReadLine();

Console::Write("Bedrooms: ");

home->Bedrooms = int::Parse(Console::ReadLine());

Console::Write("Bathrooms: ");

home->Bathrooms = float::Parse(Console::ReadLine());

Console::Write("Stories: ");

home->Stories = int::Parse(Console::ReadLine());

Console::Write("Year Built: ");

home->YearBuilt = int::Parse(Console::ReadLine());

Console::WriteLine("Style");

Console::WriteLine("U. Unknown");

Console::WriteLine("S. Split Level");

Console::WriteLine("C. Colonial");

Console::WriteLine("G. Georgial");

Console::Write("Your Choice: ");

home->Style = __wchar_t::Parse(Console::ReadLine());

Console::Write("Garage Spaces: ");

home->NumberOfGarageSpaces = int::Parse(Console::ReadLine());

Console::Write("Total Lot Size: ");

home->LotSizeInSqFt = double::Parse(Console::ReadLine());

Console::Write("Market Value: ");

home->MarketValue = double::Parse(Console::ReadLine());

return home;

}