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

7.Classes Combinations and Inheritance

7.1Class Combinations (агрегирование классов)

7.1.1 A Class as a Member Variable

Like a regular data type, you can use a class to create a member variable of another class. The primary rules to follow are the same as those of a primitive type. Here is an example:

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;

};

After declaring the variable, to access its members, if you had declared it like a regular type, you can access a member using the period operator. This allows you either retrieve the value of the member or to modify it. 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;

condo->Class.PropertyNumber = 697234;

condo->Class.Condition = 2;

condo->Class.Style = L'H';

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;

}

This would produce:

Property #: 697234

Condition: 2

Style: H

Type of Home: C

Number of Bedrooms: 2

Number of Bathrooms: 1

Number of Stories: 18

Year Built: 2002

Monetary Value: 155825

Most of the time, you will declare the member variable as a handle type. To do this, type the ^ operator between the class type and the name of the member variable. Here is an example:

public value class CHouse

{

public:

Classification ^ Class;

__wchar_t TypeOfHome;

Int Bedrooms;

double Bathrooms;

Byte Stories;

Int YearBuilt;

double Value;

};

This time, to access a member of that class, you can use the -> operator. Because the member variable is a handle, you must appropriately initialize it on the managed heap. Otherwise the program will not compile. There are various ways you can initialize the variable. From what we have learned so far, you can first create an object of that type, allocated it on the managed heap, and initialize it as you see fit. Here is an example: