write out array created as pointer in subclass (C++)

时间:2019-02-05 15:22:26

标签: c++ subclass

In the main, I'm trying to write out all items that are added to the shopping cart. I posted my whole program for context of what is happening throughout. Currently, the program outputs the memory location of the pointer, but I'm terribly confused on how to de-reference it as nothing I've tried seems to work. Now, I think I'm going about this wrong, but I'm not sure how to simply implement printing out the items in the cart. Thank you so much for any help.

#include <iostream>
#include <string>
using namespace std;

class item{
private:
    string title;
    string description;
    float price;
public:
    item(string t, string d, float p){
    this -> title = t;
    this -> description = d;
    this -> price = p;
    }
    //setters
    void setTitle(string title){}
    void setDescription(string description){}
    void setPrice(float price){}
    //getters
    string getTitle(){
        return title;
    }
    string getDescription(){
        return description;
    }
    float getPrice(){
        return price;
    }
    //virtual print function
    virtual void print(){
        cout << "Description: " << description << endl;
    }
};//end class item____________________________________________________
class book : public item{
private:
    int pageCount;
public :
    //book();
    book(string t, string d, float p, int pageCount) : item(t, d, p){
    }
    void setPageCount(int pageCount){}
    int getPageCount(){
        return pageCount;
    }
    void print(){
        cout << "Type: Book \n";
        //cout << "Description: " << description << endl;
    }
};//end subclass book_________________________________________________________
class movie : public item{
private:
    float length;
public:
    movie(string t, string d, float p, float length) : item(t, d, p){
        }
    void setLength(float length){}
    float getLength(){
        return length;
    }
    void print(){
        cout << "Type: Movie \n";
        //cout << "Description: " << d << endl;
    }
};//_______________________________________________________________________
class CD : public item{
private:
    int trackCount;
public:
    CD(string t, string d, float p, int trackCount) : item(t, d, p){
    }
    void setTrackCount(int trackCount){}
    int getTrackCount(){
        return trackCount;
    }
    void print(){
        cout << "Type: CD \n";
        //cout << "Description: " << description << endl;
    }
};//___________________________________________________________________________
class ShoppingCart{
private:
    int maxItems;
    item** items;
public:
    ShoppingCart(int maxItems){
        this-> maxItems = maxItems;
        this-> items = new item*[maxItems];
        for (int i = 0; i < maxItems; i++){
            //fills array with nothing, will store all items here
            items[i] = nullptr;
        }
    }

    void addItem(item* item){
        for (int i = 1; i <= maxItems; i++){
            if (this->items[i] == nullptr) {//check for empty spot in array
                this->items[i] = item; //adds the item here
                break; // exits if
            }
        }
    }

    void setMaxItems(int maxItems){}
    int getMaxItems(){
        return maxItems;
    }
    int getItemCount(){
        int count = 0;
        for (int i = 1; i <= maxItems; i++){
            if (this->items[i] != nullptr){
                count++;
            }
        }
        return count;
    }

    void listItems(){
        for (int i = 1; i < maxItems ; i ++){
            if (this->items[i] != nullptr){
                cout << "Item " << i <<
                ": "<< (items[i]) << endl;
            }
        }
    }
    item* getItemNum(int num){
        return items[num];
    }
};//____________________________________________________________
int main(){

    book book_dawkins("The Selfish Gene", "Genetics", 8.75, 344);

    Jacob.addItem(&book_dawkins);
    Jacob.listItems(); //this outputs "Item 1: 0x61fd40"

    return 0;
}

1 个答案:

答案 0 :(得分:0)

为打印取消引用指针只是做cout << *items[i]的问题。但是,只有在定义了用于打印item的适当重载之后,这才起作用。

首先,为virtual实现一个item函数(以及book中的替代),该函数将this打印到ostream&上:

virtual ostream& print(ostream& os) const;

接下来,在operator <<的重载中使用它:

ostream& operator<<(ostream& os, const item& i) {
    return i.print(os);
}

在这里引用至关重要,因为它允许在运行时查找正确的print方法。