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

11.2.2Passing a Handle

Another technique you can use to pass a class by reference is to pass it as a handle. To start with the formula we reviewed, use the ^ operator instead of %. In the body of the function, you can access each member variable using the -> operator.

As mentioned for a tracking reference, if the function doesn't modify the argument, the handle can be passed as a constant. 

Using handles as arguments, you can overload a function by creating functions with the same name with either different types of argument(s) or different numbers of arguments.

11.2.3Practical Learning: Passing a Handle

  1. To pass a handle as argument, 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;

}

void ShowProperty(const CSingleFamily ^ home)

{

Console::WriteLine("=//= Property Listing =//=");

Console::WriteLine("Property #: {0}", home->PropertyNumber);

Console::WriteLine("Address: {0}\n {1},

{2} {3}", home->Address, home->City,

home->State, home->ZIPCode);

Console::WriteLine("Condition: {0}", home->Condition);

Console::WriteLine("Style: {0}", home->Style);

Console::WriteLine("Bedrooms: {0}", home->Bedrooms);

Console::WriteLine("Bathrooms: {0}", home->Bathrooms);

Console::WriteLine("Stories: {0}", home->Stories);

Console::WriteLine("Year Built: {0}", home->YearBuilt);

Console::WriteLine("Garage Spaces: {0} SqFt",

home->NumberOfGarageSpaces);

Console::WriteLine("Lot Size: {0:F}", home->LotSizeInSqFt);

Console::WriteLine("Market Value: {0:C}", home->MarketValue);

}