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

Void Show();

};

  1. Complete the Flower.cpp source file as follows:  

#include "Flower.h"

CFlower::CFlower(void)

: _tp(L"Roses"), _clr(L"Red"),

_arg(L"Basket"), _price(45.95)

{

}

CFlower::CFlower(String ^ type, String ^ color,

String ^ argn, double price)

{

_tp = type;

_clr = color;

_arg = argn;

_price = price;

}

CFlower::~CFlower()

{

}

Void cFlower::Show()

{

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"=======================");

}

  1. To create one more source file, on the main menu, click Project -> Add New Item...

  2. In the Templates list, make sure C++ File (.cpp) is selected. Set the Name to Exercise and click Add

  3. Complete the file as follows:  

#include "Flower.h"

using namespace System;

Int main()

{

CFlower ^ flower = gcnew CFlower(L"Lilies", L"Pink",

L"Bouquet", 45.65);

flower->Show();

Console::WriteLine();

return 0;

}

  1. Execute the application to make sure it can compile:  

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

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

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

Flower Type: Lilies

Flower Color: Pink

Arrangement: Bouquet

Price: $45.65

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

Press any key to continue . . .

  1. Close the DOS window

15.3.3Delegate Declaration

To declare a delegate, you use the delegate keyword. The basic formula used to create a delegate is:

Access Level delegate Function-Signature;

The Function-Signature factor is created as you would declare a normal C++ function. This means that the Function-Signature must have

  1. A return type: The return type can be void, one of the primitive data types (int, long, double, short, etc), a .NET Framework type (Double, Decimal, etc), a .NET Framework class, or a class you created

  2. A name: Like every variable, function, or class, a delegate must have a name

  3. Parentheses: Like every function, a delegate must have parentheses

  4. Optional arguments: If the method(s) that this delegate will reference has/have argument(s), this delegate must also have argument(s) of exactly the same kind

After defining the Function-Signature, you must end the delegate declaration with a semi-colon. Here is an example:

delegate double Addition();

A declaration of a delegate can also use an access level as public or private. If you plan to use the delegate only locally, you can omit the access level or declare it as private, which is the default. If you plan to use the delegate outside of the project in which it is created, you should declare it as public.

After declaring a delegate, remember that it only provides a skeleton for a method, not an actual method. In order to use it, you must define a method that would carry an assignment the method is supposed to perform. That method must have the same return type and the same (number of) argument(s), if any. Here is an example:

private delegate double Addition();

public ref class CMathOperations

{

public:

double Plus()

{

double a = 248.66, b = 50.28;

return a + b;

}

};

After implementing the method, you can associate it to the name of the delegate. To do that, where you want to use the method, first declare a pointer of the type of the delegate using the gcnew operator and calling its default constructor.