How to store the item in a dynamically allocated object from a standard input

I need help on how to store the item in a dynamically allocated object from standard input.

I am pretty lost because my professor from my last class never taught us this and this professor expects us to know this already.

GroceryItem.hpp

#include <iostream>
#include <string>

class GroceryItem
{
private:
    std::string _upc;
    std::string _brandName;
    std::string _productName;
    double _price;
public:
    GroceryItem();
    std::string upc();
    void upc(std::string number);
    std::string brandName();
    void brandName(std::string name);
    std::string productName();
    void productName(std::string name);
    double price();
    void price(double amount);
};

GroceryItem.cpp

#include "GroceryItem.hpp"
#include <iostream>
#include <string>

GroceryItem::GroceryItem() :
        _upc("NULL"), _brandName("NULL"), _productName("NULL"), _price(0.00)
{
}

std::string GroceryItem::upc()
{
    return _upc;
}
void GroceryItem::upc(std::string number)
{
    number = _upc;
}
std::string GroceryItem::brandName()
{
    return _brandName;
}
void GroceryItem::brandName(std::string name)
{
    name = _brandName;
}
std::string GroceryItem::productName()
{
    return _productName;
}
void GroceryItem::productName(std::string name)
{
    name = _productName;
}
double GroceryItem::price()
{
    return _price;
}
void GroceryItem::price(double amount)
{
    amount = _price;
}

I need to store this into a dynamic object then store the pointer to the object into a vector

2 answers

  • answered 2019-09-10 02:51 seccpur

    Try this(commented), prefer to use smart pointers to raw pointer:

    class GroceryItem {
    private:
       /* … */
    public:
       /* … */    
       // operator overloadings
       friend istream& operator>> (istream& is, GroceryItem* item);
       friend ostream& operator<< (ostream& os, GroceryItem* item);
    };   
    istream& operator>> (istream& is, GroceryItem* item) {
        is >> item->_brandName >>item->_productName;
        return is;
    }    
    ostream& operator<< (ostream& os, GroceryItem* item) {
        os << "\nBrand "<< item->_brandName << "  product " << item->_productName;
        return os;
    }    
    int main()
    {
        // declare vector of items
        std::vector<GroceryItem*> items;
    
        //GroceryItem1
        GroceryItem* item1 = new GroceryItem();
        std::cin >> item1;
        items.push_back(item1);
    
        //GroceryItem2
        GroceryItem* item2 = new GroceryItem();
        std::cin >> item2;
        items.push_back(item2);
    
        //print and delete all pointers to avoid memory leak
        for (auto p : items) {
            std::cout << p;
            delete p;
        }
    
        return 0;
    }
    

  • answered 2019-09-10 03:01 Rizwan Amjad

    Your first need is to create a dynamic object.

    GroceryItem *item= new GroceryItem();
    

    This will create a pointer of Type GroceryItem and will dynamically store object in it.

    Your next need was to store this dynamic object into a vector.

    create a vector of type GroceryItem

    vector<GroceryItem> vec;
    

    Now you can add your object using push_back() function.

    vec.push_back(*item);