Besides, or instead of, a for loop, the .NET Framework
provides another and better support for enumeration. In the C++/CLI language, you can
enumerate a collection using the for each operator, but the collection
must be prepared for it: you cannot just use for each for any collection.
This support is provided through two main interfaces: public IEnumerator and IEnumerable.
These two interfaces are defined in the System::Collections namespace.
Therefore, if you intend to use them, you can include this namespace in your
source file.
Practical Learning: Introducing Built-In Collections |
|
- Start Microsoft Visual C++ and create a new CLR Empty Project named BethesdaCarRental1
- To create a new class, in the Solution Explorer, right-click BethesdaCarRental1
->
Add -> Class...
- In the Templates list, click C++ Class and click Add
- Set the Name to CCar and press Enter
- Change the Car.h header file as follows:
#pragma once
using namespace System;
public ref class CCar
{
public:
String ^ TagNumber;
String ^ Make;
String ^ Model;
short CarYear;
int Mileage;
String ^ Category;
bool HasK7Player;
bool HasCDPlayer;
bool HasDVDPlayer;
bool Available;
CCar ^ Next;
CCar(void);
};
|
- Access the Car.cpp source file and implement the constructor as follows:
#include "Car.h"
CCar::CCar()
{
TagNumber = L"000 000";
Make = L"Unknown";
Model = L"Unknown";
CarYear = 1960;
Mileage = 0;
Category = L"Unknown";
HasK7Player = false;
HasCDPlayer = false;
HasDVDPlayer = false;
Available = false;
}
|
- To create a new class, in the Solution Explorer, right-click BethesdaCarRental1 ->
Add -> Class...
- In the Templates list, click C++ Class and click Add
- Set the Name to CInventory and click Finish
- Change the Inventory.h header file as follows:
#pragma once
#include "Car.h"
using namespace System;
public ref class CInventory abstract
{
protected:
int items;
public:
CInventory(void);
property int Count
{
int get() { return items; }
}
virtual int Add(CCar ^ obj) = 0;
virtual CCar ^ Get(int index) = 0;
virtual bool Delete() = 0;
};
|
- Access the Inventory.cpp source file and change it as follows:
#include "Inventory.h"
CInventory::CInventory(void)
{
items = 0;
}
|
- To create a new class, in the Class View, right-click BethesdaCarRental1 ->
Add -> Class...
- In the Templates list, click C++ Class and click Add
- Set the Name to CCarInventory and click Finish
- Change the CarInventory.h header file as follows:
#pragma once
#include "Inventory.h"
#include "Car.h"
using namespace System;
public ref class CCarInventory : public CInventory
{
public:
CCar ^ Head;
CCar ^ Current;
CCar ^ Inventory;
virtual int Add(CCar ^ NewCar) override;
virtual CCar ^ Get(int index) override;
virtual bool Delete() override;
CCarInventory(void);
};
|
- Access the CarInventory.cpp source file and change it as follows:
#include "CarInventory.h"
CCarInventory::CCarInventory(void)
{
Head = nullptr;
}
int CCarInventory::Add(CCar ^ NewCar)
{
CCar ^ Sample = gcnew CCar;
Sample = NewCar;
Sample->Next = Head;
Head = Sample;
return items++;
}
CCar ^ CCarInventory::Get(int index)
{
CCar ^ Current = Head;
for(int i = Count - 1;
i > index && Current != nullptr;
i--)
Current = Current->Next;
return Current;
}
bool CCarInventory::Delete()
{
if (Head == nullptr)
{
Console::WriteLine(L"The inventory is empty");
return false;
}
CCar ^ Current;
Current = Head->Next;
Head->Next = Current->Next;
items--;
return true;
}
|
- Save all
|
|