2013-01-11 67 views
1

好的,例如,我必須使用List ADT創建一個包含多個類的簡單醫院隊列系統。所以我的問題是typedef。我怎麼去做這件事,因爲類型def只能有一種數據類型。C++對多個類使用相同的ADT結構

#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

歡迎堆棧溢出! –

+0

你打算將'Patients','Records'和'Services'存儲在同一個列表中,還是將它包含三個不同的列表? – Lol4t0

回答

0

我建議您將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

我必須對List.cpp文件所做的更改如何? –

+0

您應該閱讀模板。簡而言之,您需要將List.cpp中的所有代碼移動到List.h中。 –

+0

通過將所有的代碼轉移到List.h中,我會在第二個添加函數 –

1

你應該使用模板:

#include <list> 

typedef std::list<Patient> Patients; 
typedef std::list<Record> Records; 
typedef std::list<Service> Services; 
相關問題