-2

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

user4581301
  • 33,082
  • 7
  • 33
  • 54
  • 1
    What does "store the item in a dynamically allocated object from standard input" mean to you? You need to explain what this means. The individual words are understandable, but their combination, in totality, does not add up for me. – Sam Varshavchik Sep 10 '19 at 02:43
  • One of the best things about `vector` is there's very rarely a need to dynamically allocate an object. Let `vector` store and manage the sucker and your life will be orders of magnitude easier. – user4581301 Sep 10 '19 at 02:51

2 Answers2

1

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;
}
seccpur
  • 4,996
  • 2
  • 13
  • 21
  • That's a good point. Salam Abdelhalim, consider giving building a `operator >>` overload for `GroceryItem`. This may be what your instructor is trying to teach. [Here is some excellent reading on operator overloading](https://stackoverflow.com/questions/4421706/what-are-the-basic-rules-and-idioms-for-operator-overloading) to help you out. – user4581301 Sep 10 '19 at 02:57
  • @user4581301: operator overloading implemented. Thanks. – seccpur Sep 10 '19 at 03:17
  • I really appreciate it. I've been trying to learn how to use the friend function. I'm also learning to use std. I'll give this a try. – Salam Abdelhalim Sep 10 '19 at 07:10
0

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);
Rizwan Amjad
  • 329
  • 4
  • 11