写出来作为指针创建阵列中的子类(C ++)

问题描述 投票:-1回答:1

在主,我想写出被添加到购物车中的所有项目。我贴我的整个程序是什么发生在整个上下文。目前,该方案输出指针的存储位置,但我如何去引用它非常困惑,因为没有什么我已经尝试似乎工作。现在,我想我要对此错误的,但我不知道如何简单地实现打印出购物车中的物品。非常感谢你的任何帮助。

#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;
}
c++ subclass
1个回答
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

© www.soinside.com 2019 - 2024. All rights reserved.