This example illustrates a simple list of items such as a list of integers, characters, etc.
Header File: List.h
//---------------------------------------------------------------------------
#ifndef ListH
#define ListH
//---------------------------------------------------------------------------
const int MaxItems = 100;
typedef double ItemType;
class FList
{
public:
FList();
virtual ~FList();
int Count() const;
bool Add(const ItemType NewItem);
ItemType Retrieve(const int Pos);
bool Delete(const int Pos);
private:
int Counter;
ItemType Item[MaxItems];
const Index(const int Pos) const;
};
//---------------------------------------------------------------------------
#endif
Source File: List.cpp
//---------------------------------------------------------------------------
#pragma hdrstop
#include "List.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
FList::FList()
: Counter(0)
{
}
//---------------------------------------------------------------------------
FList::~FList()
{
}//---------------------------------------------------------------------------
int FList::Count() const
{
return Counter;
}
//---------------------------------------------------------------------------
const FList::Index(const int Pos) const
{
return Pos-1;
}
//---------------------------------------------------------------------------
bool FList::Add(const ItemType NewItem)
{
if( (Counter < 0) || (Counter > MaxItems) )
return false;
else
{
Item[Counter] = NewItem;
Counter++;
return true;
}
}
//---------------------------------------------------------------------------
ItemType FList::Retrieve(const int Pos)
{
if( (Pos < 0) || (Pos > Counter) )
return 0;
else
return Item[Pos];
}
//---------------------------------------------------------------------------
bool FList::Delete(const int Pos)
{
if( (Pos < 0) || (Pos > Counter) )
return false;
else
{
for(int i = Pos+1; i <= Counter; i++)
Item[Index(i-1)] = Item[Index(i)];
Counter--;
return true;
}
}
//---------------------------------------------------------------------------
Source File: Main.cpp
//---------------------------------------------------------------------------
#include
#include
using namespace std;
#pragma hdrstop
#include "List.h"
//---------------------------------------------------------------------------
#pragma argsused
int main(int argc, char* argv[])
{
FList List;
List.Add(1240.42);
List.Add(283.15);
List.Add(42.48);
List.Add(582.12);
List.Add(60.14);
List.Add(8022.28);
cout << "List of items";
for(int i = 0; i < List.Count(); i++)
cout << "\nItem No." << i + 1 << ": " << List.Retrieve(i);
cout << "\nNumber of items: " << List.Count() << " items\n\n";
List.Delete(5);
List.Delete(2);
cout << "List of items";
for(int i = 0; i < List.Count(); i++)
cout << "\nItem No." << i + 1 << ": " << List.Retrieve(i);
cout << "\nNumber of items: " << List.Count() << " items\n\n";
cout << "Press any key to continue...";
getch();
return 0;
}
//---------------------------------------------------------------------------
No comments:
Post a Comment