list — C ++ Использование одной структуры ADT для нескольких классов

Итак, например, я должен создать простую систему очередей в больнице с несколькими классами, используя List ADT. Так что моя проблема с typedef. Как мне это сделать, так как тип def может иметь только один тип данных.

  #include <string>
#include "Patients.h"#include "Records.h"#include "Services.h"
const int MAX_SIZE = 10000;
typedef Patients ItemType;
typedef Records ItemType;         //Error Here
typedef Services ItemType;        //Error Here

class List
{
private:
ItemType items[MAX_SIZE];
int      size;

public:

List::List();

void List::display();

void List::replace(int index, ItemType item);

bool List::add(ItemType newItem);

bool List::add(int index, ItemType newItem);

void List::remove(int index);

ItemType List::get(int index);

bool List::isEmpty();

int List::getLength();

};#include <iostream>
#include "List.h"  // header file
using namespace std;
// constructor
List::List()
{
size = 0;
}

// add a new item to the back of the list (append)
bool List::add(ItemType newItem)
{
bool success = size < MAX_SIZE;
if (success)
{
items[size] = newItem; // add to the end of the list
size++;                // increase the size of the list by one
}
return success;
}

// add a new item at a specified position in the list (insert)
bool List::add(int index, ItemType newItem)
{
bool success = (index >= 1) && (index <= size + 1) && (size < MAX_SIZE);
if (success)
{
for (int pos = size; pos >= index; pos--)
items[pos] = items[pos-1];

items[index-1] = newItem;
size++;  // increase the size of the list by one
}
return success;
}

// remove an item at a specified position in the list
void List::remove(int index)
{
bool success = (index >= 1) && (index <= size);
if (success)
{
for (int fromPosition = index + 1; fromPosition <= size; fromPosition++)
items[fromPosition - 2] = items[fromPosition - 1];

size--;
}

}

// get an item at a specified position of the list (retrieve)
ItemType List::get(int index)
{
ItemType dataItem;// = 0;
bool success = (index >= 1) && (index <= size);
if (success)
dataItem = items[index - 1];

return dataItem;
}

// check if the list is empty
bool List::isEmpty()
{
return size == 0;
}

// check the size of the list
int List::getLength()
{
return size;
}void List::replace(int index, ItemType item)
{
bool success = index >= 1 && index <= getLength();
if (success)
items[index] = item;
}

1

Решение

Я предлагаю вам изменить List из класса в шаблон класса. Смотри как std::list<> работает в стандартной библиотеке для большего количества идей.

Итак, вы можете иметь:

template<class ItemType>
class List
{
private:
ItemType items[MAX_SIZE];
int      size;
public:
ItemType List::get(int index);
...
};

Затем вы можете указать тип базовой точки списка при объявлении списка:

List<Patients> allThePeople;
List<Records>  allThePapers;
List<Services> allTheWork;

Конечно, если создаем List код по любой причине, кроме назначения класса, вы должны действительно использовать std::list вместо.

0

Другие решения

Вы должны использовать шаблоны:

#include <list>

typedef std::list<Patient> Patients;
typedef std::list<Record> Records;
typedef std::list<Service> Services;
1

По вопросам рекламы ammmcru@yandex.ru
Adblock
detector