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

Void Show(const int qty);

};

  1. To implement the method, change the Flower.cpp source file as follows:  

#include "Flower.h"

. . .

Void cFlower::Show(const int qty)

{

double totalPrice = UnitPrice * qty;

Console::WriteLine(L"=======================");

Console::WriteLine(L"==-=-=Flower Shop=-=-==");

Console::WriteLine(L"-----------------------");

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

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

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

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

Console::WriteLine(L"Quantity: {0}", qty);

Console::WriteLine(L"Total Price: {0:C}", totalPrice);

Console::WriteLine(L"=======================");

}

  1. To use a delegate with argument, change the Exercise.cpp source file as follows:  

#include "Flower.h"

using namespace System;

private delegate void Display(const int x);

Int main()

{

CFlower ^ flower = gcnew CFlower;

flower->Type = L"Mixed";

flower->Color = L"Various";

flower->Arrangement = L"Bouquet";

flower->UnitPrice = 34.85;

Display ^ disp = gcnew Display(flower, &CFlower::Show);

disp->Invoke(3);

return 0;

}

  1. Execute the application to see the result:  

=======================

==-=-=Flower Shop=-=-==

-----------------------

Flower Type: Mixed

Flower Color: Various

Arrangement: Bouquet

Price: $34.85

Quantity: 3

Total Price: $104.55

=======================

  1. Close the DOS window

15.3.10A Delegate Passed as Argument

Using delegates, one method can be indirectly passed as argument to another method. To proceed, first declare the necessary delegate. Here is a example of such a delegate:

public delegate double Squared(double x);

A delegate can be passed as argument to a method. Such an argument would be used as if it were a method itself. This means that, when accessed in the body of the method, the name of the delegate must be accompanied by parentheses and if the delegate takes an argument or arguments, the argument(s) must be provided in the parentheses of the called delegate. Here is an example:

public delegate double Squared(double x);

public ref class CCircle

{

private:

double _radius;

public:

double Area(Squared ^ sqd)

{

return sqd(_radius) * Math::PI;

}

public:

property double Radius

{

double get() { return _radius; }

void set(double r) { _radius = r; }

}

};

After declaring a delegate, remember to define a method that implements the needed behavior of that delegate. Here is an example:

public delegate double Squared(double x);

public ref class CCircle

{

private:

double _radius;

public:

double Area(Squared ^ sqd)

{

return sqd(_radius) * Math::PI;

}

static double ValueTimesValue(double Value)

{

return Value * Value;

}

public:

property double Radius

{

double get() { return _radius; }

void set(double r) { _radius = r; }

}

};

You can also define the associated method in another class, not necessarily in the class where the delegate would be needed. Once the method that implements the delegate is known, you can use the delegate as you see fit. To do that, you can declare a pointer of the type of that delegate and pass the implementing method to its constructor. Here is an example:

public delegate double Squared(double x);

public ref class CCircle

{

private:

double _radius;

public:

double Area(Squared ^ sqd)

{

return sqd(_radius) * Math::PI;

}

static double ValueTimesValue(double Value)

{

return Value * Value;

}

public:

property double Radius

{

double get() { return _radius; }

void set(double r) { _radius = r; }

}