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

34.3.3 Implementing a Collection

After deciding what each item of the list would be made of, you can create a class that would manage the list. This class would be responsible for all operations that can be performed on the list. Here is an example:

using namespace System;

public ref class CNumbers

{

};

int main()

{

CNumbers ^ nbrs = gcnew CNumbers;

return 0;

}

If the list will be made of objects, you can first create a class that specifies those types of items and then declare its variable in the list class. Here is an example of a simple class that holds a double-precision value:

using namespace System;

public ref class CNumber

{

public:

double Item;

};

public ref class CNumbers

{

CNumber ^ Sample;

};

int main()

{

CNumbers ^ nbrs = gcnew CNumbers;

return 0;

}

When creating a list, one of the aspects you should pay attention to is to keep track of the number of items in the list. To do this, you can create a property that holds the number. The value of this property would increase as the items are added to the list and the value would decrease as the items are removed from the list. Here is how this can be done:

using namespace System;

public ref class CNumber

{

public:

double Item;

};

public ref class CNumbers

{

private:

int size;

CNumber ^ Sample;

public:

CNumbers()

{

size = 0;

}

property int Count

{

int get() { return size; }

}

};

int main()

{

CNumbers ^ nbrs = gcnew CNumbers;

Console::WriteLine(L"Number of Items: {0}", nbrs->Count);

return 0;

}

This would produce:

Number of Items: 0

Press any key to continue . . .

34.3.4 Practical Learning: Creating a cCollection Class

  1. Open the Flower.h header file and change it as follows:  

    #pragma once

    using namespace System;

    . . . No Change

    public ref class CFlower

    {

    private:

    double prc;

    public:

    FlowerType Type;

    FlowerColor Color;

    FlowerArrangement Arrangement;

    property double UnitPrice

    {

    double get() { return prc; }

    void set(double value) { prc = value; }

    }

    CFlower(void);

    protected:

    int items;

    public:

    property int Count

    {

    int get() { return items; }

    }

    };

  2. Open the Flower.cpp source file and change it as follows:  

    #include "Flower.h"

    CFlower::CFlower(void)

    {

    Type = Mixed;

    Color = Various;

    Arrangement = Vase;

    prc = 0.00;

    items = 0;

    }

  3. To create a new class, in the Class View, right-click FlowerShop4 -> Add -> Class...

  4. In the Templates list, click C++ Class and click Add

  5. Set the Name to CFlowerInventory and click Finish

  6. Change the FlowerInventory.h header file as follows:  

    #pragma once

    #include "Flower.h"

    public ref class CFlowerInventory : CFlower

    {

    public:

    CFlower ^ Inventory;

    CFlowerInventory(void);

    };

  7. Access the FlowerInventory.cpp source file and change it as follows:  

#include "FlowerInventory.h"

CFlowerInventory::CFlowerInventory(void)

{

Inventory = gcnew CFlower;

}