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

Int main()

{

CHouse ^condo = gcnew CHouse;

Classification ^ type = gcnew Classification;

type->PropertyNumber = 697234; // Unique Number

type->Condition = 2; // Good, may need minor repair

type->Style = L'H'; // High-rise

return 0;

}

Once the object is ready, you can assign it to the member variable that needs the values:

Int main()

{

CHouse ^condo = gcnew CHouse;

Classification ^ type = gcnew Classification;

type->PropertyNumber = 697234; // Unique Number

type->Condition = 2; // Good, may need minor repair

type->Style = L'H'; // High-rise

condo->Class = type;

return 0;

}

When accessing the member variables of that object, remember to use the -> operator. Here are examples:

using namespace System;

public value class Classification

{

public:

long PropertyNumber;

int Condition;

__wchar_t Style;

};

public value class CHouse

{

public:

Classification ^ Class;

__wchar_t TypeOfHome;

int Bedrooms;

double Bathrooms;

Byte Stories;

int YearBuilt;

double Value;

};

int main()

{

CHouse ^condo = gcnew CHouse;

Classification ^ type = gcnew Classification;

type->PropertyNumber = 697234; // Unique Number

type->Condition = 2; // Good, may need minor repair

type->Style = L'H'; // Highrise

condo->Class = type;

condo->YearBuilt = 2002;

condo->Bathrooms = 1;

condo->Stories = 18;

condo->Value = 155825;

condo->Bedrooms = 2;

condo->TypeOfHome = L'C';

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

Console::WriteLine(condo->Class->PropertyNumber);

Console::Write("Condition: ");

Console::WriteLine(condo->Class->Condition);

Console::Write("Style: ");

Console::WriteLine(condo->Class->Style);

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

Console::WriteLine(condo->TypeOfHome);

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

Console::WriteLine(condo->Bedrooms);

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

Console::WriteLine(condo->Bathrooms);

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;

}

When we study methods and constructor, we will see that there are other ways to initi alize a member variable declared as a handle.

7.1.2Practical Learning: Using an Object as a Field

  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 RealEstate7 and click OK

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

  6. In the Templates list, click File (.cpp)

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

  8. In the empty file, type:  

using namespace System;

namespace RealEstate

{

public value class CClassification

{

public:

long PropertyNumber;

int Condition;

__wchar_t Style;

};

public value class CProperty

{

public:

CClassification ^ Class;

__wchar_t TypeOfHome;

int Bedrooms;

float Bathrooms;

Byte Stories;

int YearBuilt;

double MarketValue;

};

}

int main()

{

RealEstate::CProperty ^ condo = gcnew RealEstate::CProperty;

RealEstate::CClassification ^ type = gcnew

RealEstate::CClassification;

type->PropertyNumber = 697234; // Unique Number

type->Condition = 2; // Good, may need minor repair

type->Style = L'H'; // Highrise

condo->Class = type;

condo->YearBuilt = 2002;

condo->Bathrooms = 1;

condo->Stories = 18;

condo->MarketValue = 155825;

condo->Bedrooms = 2;

condo->TypeOfHome = L'C';

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

Console::WriteLine(condo->Class->PropertyNumber);

Console::Write("Condition: ");

Console::WriteLine(condo->Class->Condition);

Console::Write("Style: ");

Console::WriteLine(condo->Class->Style);

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

Console::WriteLine(condo->TypeOfHome);

Console::Write("Bedrooms: ");

Console::WriteLine(condo->Bedrooms);

Console::Write("Bathrooms: ");

Console::WriteLine(condo->Bathrooms);

Console::Write("Stories: ");

Console::WriteLine(condo->Stories);

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

Console::WriteLine(condo->YearBuilt);

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

Console::WriteLine(condo->MarketValue);

Console::WriteLine();

return 0;

}

  1. To execute the application, on the main menu, click Debug -> Start Without Debugging

  2. Click Yes  

Property #: 697234

Condition: 2

Style: H

Type of Home: C

Bedrooms: 2

Bathrooms: 1

Stories: 18

Year Built: 2002

Market Value: 155825

  1. Close the DOS window