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

Int main()

{

double bRooms = *GetBathrooms();

Console::WriteLine(L"Number of Bathrooms: {0:F}\n", bRooms);

return 0;

}

This would produce:

How many bathrooms in the house? 2.5

Number of Bathrooms: 2.50

9.2.6Returning a Handle

If you want to process a function in the managed heap, you can make it return a handle. When declaring or defining the function, precede it with a handle type. When you finish using the function, make sure it returns a value that is a handle. The rules are the same for pointers.

9.2.7Practical Learning: Returning a Handle

1. To show different ways of returning a handle, change the file as follows:

#include <iostream>

using namespace std;

using namespace System;

long ^ GetPropertyNumber()

{

long ^ propertyNumber = gcnew long;

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

propertyNumber = long::Parse(Console::ReadLine());

return propertyNumber;

}

int ^ GetPropertyCondition()

{

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: ");

int ^ condition = int::Parse(Console::ReadLine());

return condition;

}

Byte ^ GetNumberOfStories()

{

Byte ^ stories = gcnew Byte;

Console::Write("Stories: ");

*stories = int::Parse(Console::ReadLine());

return stories;

}

unsigned int ^ GetBedrooms()

{

unsigned int ^ bedrooms = gcnew unsigned int;

Console::Write("Bedrooms: ");

bedrooms = UInt32::Parse(Console::ReadLine());

return bedrooms;

}

__wchar_t GetPropertyStype()

{

__wchar_t style;

Console::WriteLine("Style");

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

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

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

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

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

style = __wchar_t::Parse(Console::ReadLine());

return style;

}

double GetPropertyValue()

{

double marketValue;

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

marketValue = double::Parse(Console::ReadLine());

return marketValue;

}

float GetBathrooms()

{

float bathrooms;

Console::Write("Bathrooms: ");

bathrooms = float::Parse(Console::ReadLine());

return bathrooms;

}

unsigned int GetYearBuilt()

{

unsigned int yearBuilt;

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

yearBuilt = int::Parse(Console::ReadLine());

return yearBuilt;

}

Void CreateAndShowProperty()

{

long^ propertyNumber = gcnew long;

int ^ condition = gcnew int;

Byte ^stories = gcnew Byte;

unsigned int ^ bedrooms = gcnew unsigned int;

float bathrooms;

unsigned int yearBuilt;

__wchar_t style;

double marketValue;

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

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

Console::WriteLine(strTitle1);

Console::WriteLine(strTitle2);

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

following information");

propertyNumber = GetPropertyNumber();

condition = GetPropertyCondition();

stories = GetNumberOfStories();

bedrooms = GetBedrooms();

bathrooms = GetBathrooms();

yearBuilt = GetYearBuilt();

style = GetPropertyStype();

marketValue = GetPropertyValue();

system("cls");

Console::WriteLine(strTitle1);

Console::WriteLine(strTitle2);

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

Console::WriteLine("Property #: {0}", propertyNumber);

Console::WriteLine("Condition: {0}", condition);

Console::WriteLine("Style: {0}", style);

Console::WriteLine("Bedrooms: {0}", bedrooms);

Console::WriteLine("Bathrooms: {0}", bathrooms);

Console::WriteLine("Stories: {0}", stories);

Console::WriteLine("Year Built: {0}", yearBuilt);

Console::WriteLine("Market Value: {0:C}", marketValue);

}