Question

c++ Create a Grocery list dynamically allocated object. also store pointer in to a standard vector...

c++ Create a Grocery list dynamically allocated object. also store pointer in to a standard vector

//GroceryItem.cpp

#pragma once // include guard

#include <string>
#include <iostream>

class GroceryItem
{
// Insertion and Extraction Operators
friend std::ostream & operator<<( std::ostream & stream, const GroceryItem & groceryItem );
friend std::istream & operator>>( std::istream & stream, GroceryItem & groceryItem );

// Relational Operators
friend bool operator==( const GroceryItem & lhs, const GroceryItem & rhs );
friend bool operator< ( const GroceryItem & lhs, const GroceryItem & rhs );

public:
// Constructors
GroceryItem() = default;
GroceryItem( const std::string & productName,
const std::string & brandName = {},
const std::string & upcCode = {},
double price = 0.0 );

// Queries
std::string upcCode () const;
std::string brandName () const;
std::string productName () const;
double price () const;

// Mutators
void upcCode ( const std::string & upcCode );
void brandName ( const std::string & brandName );
void productName ( const std::string & productName );
void price ( double price );

private:
std::string _upcCode;
std::string _brandName;
std::string _productName;
double _price = 0.0;
};

// Relational Operators
bool operator==( const GroceryItem & lhs, const GroceryItem & rhs );
bool operator!=( const GroceryItem & lhs, const GroceryItem & rhs );
bool operator< ( const GroceryItem & lhs, const GroceryItem & rhs );
bool operator<=( const GroceryItem & lhs, const GroceryItem & rhs );
bool operator> ( const GroceryItem & lhs, const GroceryItem & rhs );
bool operator>=( const GroceryItem & lhs, const GroceryItem & rhs );

output--

Welcome to Kroger. Place grocery items into your shopping basket by entering each product's information.
enclose string entries in quotes, separate fields with comas
CTL-Z (Windows) or CTL-D (Linux) to quit

Enter UPC, Product Brand, Product Name, and Price
Item added to shopping basket: "00072250018548", "Nature's Own", "Nature's Own Butter Buns Hotdog - 8 Ct", 10.79

Enter UPC, Product Brand, Product Name, and Price
Item added to shopping basket: "00028000517205", "Nestle", "Nestle Media Crema Table Cream", 17.97

Enter UPC, Product Brand, Product Name, and Price
Item added to shopping basket: "00034000020706", "York", "York Peppermint Patties Dark Chocolate Covered Snack Size", 12.64

Enter UPC, Product Brand, Product Name, and Price
Item added to shopping basket: "00038000570742", "Kellogg's", "Kellogg's Cereal Krave Chocolate", 18.66

Enter UPC, Product Brand, Product Name, and Price
Item added to shopping basket: "00014100072331", "Pepperidge Farm", "Pepperidge Farm Classic Cookie Favorites", 14.43

Enter UPC, Product Brand, Product Name, and Price


Here is an itemized list of the items in your shopping basket:
"00014100072331", "Pepperidge Farm", "Pepperidge Farm Classic Cookie Favorites", 14.43
"00038000570742", "Kellogg's", "Kellogg's Cereal Krave Chocolate", 18.66
"00034000020706", "York", "York Peppermint Patties Dark Chocolate Covered Snack Size", 12.64
"00028000517205", "Nestle", "Nestle Media Crema Table Cream", 17.97
"00072250018548", "Nature's Own", "Nature's Own Butter Buns Hotdog - 8 Ct", 10.79

0 0
Add a comment Improve this question Transcribed image text
Answer #1
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; }
Add a comment
Know the answer?
Add Answer to:
c++ Create a Grocery list dynamically allocated object. also store pointer in to a standard vector...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • c++ Create a Grocery list dynamically allocated object. also store pointer in to a standard vector...

    c++ Create a Grocery list dynamically allocated object. also store pointer in to a standard vector OUTPUT - Welcome to Kroger. Place grocery items into your shopping basket by entering each product's information. enclose string entries in quotes, separate fields with comas CTL-Z (Windows) or CTL-D (Linux) to quit Enter UPC, Product Brand, Product Name, and Price Item added to shopping basket: "00072250018548", "Nature's Own", "Nature's Own Butter Buns Hotdog - 8 Ct", 10.79 Enter UPC, Product Brand, Product Name,...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT