2012-11-15 46 views
0

如何访问存储在列表中的对象。我不想使用载体或#include <list>。任何见解都会被赞赏。谢谢!以下是我的班级定义:如何访问存储在列表中的对象

class UnsortedType // UnsortedType.h 
{ 
    private:     
     int length, currentPos; 
     book info[MAX_ITEMS]; 


    public:    
     UnsortedType(); 
     bool IsFull() const ; 
     int LengthIs() const ; // returns length of list 
     void RetrieveItem(book& item, bool& found); 
     void InsertItem(book item); // adds objects to the list 
     void DeleteItem(book item); // deletes objects in the list 
     void ResetList(); // resets list to default length -1 
     void GetNextItem(book& item); // gets the next object in the list 

}; 

存储在此列表中的是书籍对象。包含在这些对象中的标题,作者,价格等...我的问题是如何访问每个对象,一旦它们存储在列表中。我想能够比较存储在列表中的对象的属性。例如每本书的价格。

//main 
#include <iostream> 
#include "Book.h" 
#include "textbook.h" 
#include "Name.h" 
#include "unsorted.h" 

using namespace std; 


int main() 
{ 


    book b1("The Exception to the Rulers", "Amy", "Goodman", "Hyperion", 342, "1-4013-0131", 21.95,'N'); 
    book b2("Who moved my cheese", "Spencer", "Johnson", "Red Tree", 95, "0-399-14446-3", 19.99, 'H'); 
    book b3("Hellbound Hearts", "Neil", "Gaiman", "Dark Harvest", 326, "978-1-4391-4090-1", 16.00, 'F'); 

    UnsortedType L1; // creating a list "L1" 

    L1.InsertItem(b1); // populating the list with the first book 
    L1.InsertItem(b2); // populating the list with the second book 
    L1.InsertItem(b3); // populating the list with the third book 


    return 0; 
} 

这些是在book.h

// book.h 
enum RelationType 
{ 
    LESS, EQUAL, GREATER 
}; 

class book 
{ 
private: 
    string title; 
    Name aurthor; 
    string publisher; 
    string ISBN; 
    int pages; 
    float price; 
    char code; 


public: 
    RelationType ComparedTo(book) const; 
    class negativeNumber{}; 
    void setTitle(string); 
    void setAurthor(string f, string l); 
    void setPublisher(string); 
    void setISBN(string); 
    void setPages(int); 
    void setPrice(float); 
    void setCode(char); 
    string getTitle(); 
    Name getAurthor(); 
    string getPublisher(); 
    string getISBN(); 
    int getPages(); 
    float getPrice(); 
    char getCode(); 
    void PrintBook(); 
    book(); //default constructor 
    book(string, string, string, string, int, string, float, char); //constructor with args 
    ~book(); //Destructor 
}; 
+2

你写的'UnsortedType'?因为它看起来不可能使用。看起来有一个功能可以让书出来,但是你不能指定哪一个。 DeleteItem apppears需要搜索。它使左右书籍的额外副本......看起来你可以遍历列表,但只能有一次。 –

+0

基本上你有一个数组的包装,所以你的问题是'我如何使用一个对象数组' - 回答'使用正常数组的东西'。例如,您可以添加一个GetItem(int idx)方法来获取数组中的元素 – pm100

+0

如果想让列表正常工作,为什么要使用如此复杂的'book'类? – Beta

回答

1
Book& UnsortedType::operator[](const int i)) 
{ 
    //check if array is in bounds 
    return info[i]; 
} 

的功能,这将允许您使用所述未分类的类L1访问书的对象[3];

或在您的情况下,继承人访问每个对象,并确定本本的最低价格的例子:

double UnsortedType::FindLow() 
{ 
    double low = 9999;  //used to store lowest value 
    for(int i = 0; i < length; i++) //loop through array 
    { 
     if(info[i].price < low) //if the price of this book is lower than the current lowest 
      low = info[i].price; 
       //set low to new low 
    } 
    return low; 
} 
+0

所有书籍都存储在L1中。但是,当我尝试像L1 [i]那样访问列表(L1)中的第一个元素(第一本书)时,出现错误。我需要知道如何首先访问列表中的对象。然后我可以开始操纵对象中的数据。 –

+0

你需要重载你的[]操作符或使用某个函数,否则请重新编辑我的帖子 –

+0

感谢!即时通讯不知道我真的明白为什么你必须超载运营商,但它的作品。 –

相关问题