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

Int NumberOfBedrooms;

double NumberOfBathrooms;

Byte Stories;

Int YearBuilt;

double Value;

};

Int main()

{

CProperty victorian;

victorian.Value = 485565;

CProperty % single = victorian;

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

Console::WriteLine(victorian.Value);

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

Console::WriteLine(single.Value);

Console::WriteLine();

return 0;

}

Remember that you can also create a handle to use it.

6.6.1Practical Learning: Creating a Reference Class

  1. Access the Property.h file

  2. To make the class a reference type, change it as follows:

#pragma once

namespace RealEstate

{

public ref class CProperty

{

public:

long PropertyNumber;

__wchar_t PropertyType;

unsigned Stories;

unsigned Bedrooms;

float Bathrooms;

unsigned YearBuilt;

double PropertyValue;

};

}

  1. Save the Property.h file

6.6.2Accessing the Members of a Handle

After creating a handle, you can access its members. As done for the pointer, you have two alternatives. You can use the asterisk "*" on the left side of a handle. After including the asterisk and the handle between parentheses, you can use the period operator to access each member. This allows you either to initialize the member or to retrieve its current value. Here are examples:

using namespace System;

public value class CProperty

{

public:

__wchar_t TypeOfHome;

Int NumberOfBedrooms;

double NumberOfBathrooms;

Byte Stories;

Int YearBuilt;

double Value;

};

Int main()

{

CProperty apartment;

apartment.YearBuilt = 2002;

apartment.NumberOfBathrooms = 1;

apartment.Stories = 18;

apartment.Value = 155825;

apartment.NumberOfBedrooms = 2;

apartment.TypeOfHome = L'C';

CProperty ^ condo = %apartment;

Console::Write("Type of Home: ");

Console::WriteLine((*condo).TypeOfHome);

Console::Write("Number of Bedrooms: ");

Console::WriteLine((*condo).NumberOfBedrooms);

Console::Write("Number of Bathrooms: ");

Console::WriteLine((*condo).NumberOfBathrooms);

Console::Write("Number of Stories: ");

Console::WriteLine((*condo).Stories);

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

Console::WriteLine((*condo).YearBuilt);

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

Console::WriteLine((*condo).Value);

Console::WriteLine();

return 0;

}

Instead of the asterisk, you can use the arrow, as done for the pointer, to access the members of a handle. The above program can produce the same results with:

using namespace System;

public value class CProperty

{

public:

__wchar_t TypeOfHome;

Int NumberOfBedrooms;

double NumberOfBathrooms;

Byte Stories;

Int YearBuilt;

double Value;

};

Int main()

{

CProperty apartment;

apartment.YearBuilt = 2002;

apartment.NumberOfBathrooms = 1;

apartment.Stories = 18;

apartment.Value = 155825;

apartment.NumberOfBedrooms = 2;

apartment.TypeOfHome = L'C';

CProperty ^ condo = %apartment;

Console::Write("Type of Home: ");

Console::WriteLine(condo->TypeOfHome);

Console::Write("Number of Bedrooms: ");

Console::WriteLine(condo->NumberOfBedrooms);

Console::Write("Number of Bathrooms: ");

Console::WriteLine(condo->NumberOfBathrooms);

Console::Write("Number of Stories: ");

Console::WriteLine(condo->Stories);

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

Console::WriteLine(condo->YearBuilt);

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

Console::WriteLine(condo->Value);

Console::WriteLine();

return 0;

}

Both notations produce the same result:

Type of Home: C

Number of Bedrooms: 2

Number of Bathrooms: 1

Number of Stories: 18

Year Built: 2002

Monetary Value: 155825