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

Int Type;

Int Color;

__wchar_t Arrangement;

double UnitPrice;

};

Int main()

{

CFlower ^ flr = gcnew CFlower;

Console::WriteLine(L"Flower Type: {0}", flr->Type);

Console::WriteLine(L"Flower Color: {0}", flr->Color);

Console::WriteLine(L"Arrangement: {0}", flr->Arrangement);

Console::WriteLine(L"Price: {0:C}", flr->UnitPrice);

Console::WriteLine(L"");

return 0;

}

This would produce:

Flower Type: 0

Flower Color: 0

Arrangement:

Price: $0.00

If you declare a variable of a class in your program, when the program comes up, the compiler reserves enough memory space for each member of the class. The memory space reserved for each member variable is filled with an initial value based on its type. For a String object, the space would be left empty. For an integer type, the space would be filled with 0. A better way to take care of this type is to provide a value whose role would be to initialize the member variables with the values of your choice. A method that initializes an object can return any value but it is preferable to be of type void because its primary purpose is to reset the values. Since this method would give a starting value to all member variables that need to be initialized, it should have an equivalent argument for each of the member variables that it would initialize. Here is an example:

public value class CFlower

{

public:

Int Type;

Int Color;

__wchar_t Arrangement;

double UnitPrice;

Void Initializer(int type, int color, __wchar_t arrange,

double price);

};

The method initializer doesn't have to initialize all members of the class. For example, the previous execution of the program shows that the member variables that are of type String are initialized with empty strings. In such a case, you may not have to initialize such variables. To implement a method initializer, simply assign its argument to the corresponding member variable of the class. Here are examples:

Void cFlower::Initializer(int tp, int clr,

__wchar_t arrange, double price)

{

Type = tp;

Color = clr;

Arrangement = arrange;

UnitPrice = price;

}

You can call a method initializer after declaring the instance of the class to give it initial values. Here is an example:

using namespace System;

public value class CFlower

{

public:

int Type;

int Color;

__wchar_t Arrangement;

double UnitPrice;

void Initializer(int type, int color,

__wchar_t arrange, double price);

};

void CFlower::Initializer(int tp, int clr,

__wchar_t arrange, double price)

{

Type = tp;

Color = clr;

Arrangement = arrange;

UnitPrice = price;

}

int main()

{

CFlower ^ flr = gcnew CFlower;

flr->Initializer(3, 7, L'U', 35.85);

Console::WriteLine(L"Flower Type: {0}", flr->Type);

Console::WriteLine(L"Flower Color: {0}", flr->Color);

Console::WriteLine(L"Arrangement: {0}", flr->Arrangement);

Console::WriteLine(L"Price: {0:C}", flr->UnitPrice);

Console::WriteLine(L"");

return 0;

}

This would produce:

Flower Type: 3

Flower Color: 7

Arrangement: U

Price: $35.85

Using a method initializer, after initializing the object, you can use the values it holds as you see fit.