Question

struct Item { string name; int quantity; float cost; }; const int MAX_SIZE = 50; class...

struct Item {

string name; int quantity; float cost;

};
const int MAX_SIZE = 50;

class ManageInventory {
public:

ManageInventory() : count{0}, p_pInventoryItems {new Item*[size]} { }

ManageInventory(int size) : size{size}, count{0}, p_pInventoryItems {new Item*[size]} { }

~ManageInventory();
void addItem(string name, int quantity, float cost);

private:
int size {MAX_SIZE};

int count;

Item ** p_pInventoryItems; };

  1. Write the definition for addItem. Use the new operator to dynamically create instances of type Item. Store pointers to inventory items in the inventoryItems array.

  2. Write the ManageInventory class destructor definition. Use the delete operator to dynamically destroy all Item objs stored in the inventoryItems array.

0 0
Add a comment Improve this question Transcribed image text
Answer #1
#include<string>
#include<iostream>
using namespace std;
struct Item {

  string name;
  int quantity;
  float cost;

};
const int MAX_SIZE = 50;

class ManageInventory {
  public:

    ManageInventory(){
      count = 0;
      // creating inventory of max size
      p_pInventoryItems = new Item [MAX_SIZE];
    }

  ManageInventory(int size) {
      count = 0;
      // creating inventory of given size
      p_pInventoryItems = new Item [size];
  }

    ~ManageInventory(){
        cout<<"destructor called\n";
        // deleting dynamically array
        delete [] p_pInventoryItems;
    }
  void addItem(string name, int quantity, float cost){
      // creating struct of inventory
      
      Item item;
      item.name = name;
      item.quantity = quantity;
      item.cost = cost;
      // adding to array and incrementing counter
      p_pInventoryItems[count++] = item;
  }

// added function for priting inventory
  void printItems(){
      for(int i=0;i<count;i++){
          cout<<p_pInventoryItems[i].name<<"\t"<<p_pInventoryItems[i].quantity<<"\t"<<p_pInventoryItems[i].cost<<endl;
      }
  }

  private:
        int count;
        Item * p_pInventoryItems;

 
};

// below function for are testing purpose only
int main(){
    // created object
    ManageInventory inv;
    inv.addItem("milk", 4, 45.67);
    inv.addItem("water", 2, 10.5);
    inv.printItems();
    return 0;
}

//OUT

Please do comment if u have any concern...

Add a comment
Know the answer?
Add Answer to:
struct Item { string name; int quantity; float cost; }; const int MAX_SIZE = 50; class...
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++ assignment help! The instructions are below, i included the main driver, i just need help...

    C++ assignment help! The instructions are below, i included the main driver, i just need help with calling the functions in the main function This assignment will access your skills using C++ strings and dynamic arrays. After completing this assignment you will be able to do the following: (1) allocate memory dynamically, (2) implement a default constructor, (3) insert and remove an item from an unsorted dynamic array of strings, (4) use the string class member functions, (5) implement a...

  • Data Structures and Algorithm Analysis – Cop 3530 Module 3 – Programming Assignment This assignment will...

    Data Structures and Algorithm Analysis – Cop 3530 Module 3 – Programming Assignment This assignment will access your skills using C++ strings and dynamic arrays. After completing this assignment you will be able to do the following: (1) allocate memory dynamically, (2) implement a default constructor, (3) insert and remove an item from an unsorted dynamic array of strings, (4) use the string class member functions, (5) implement a copy constructor, (6) overload the assignment operator, (7) overload the insertion...

  • Consider the Tollowing class declaration: class student record public: int age; string name; double gpa; }:...

    Consider the Tollowing class declaration: class student record public: int age; string name; double gpa; }: Implement a void function that initiatizes a dynamic array of student records with the data stored in the file "university body.txt". The function has three formal parameters: the dynamic array "S db, the count, and the capacity. Open and close an ifstream inside the function. Remember, you must allocate memory for the initial size of the dynamic array using "new" inside this function. If...

  • #include <iostream> using namespace std; template <typename Item> class MyArray{ private:    Item *myarray;    int...

    #include <iostream> using namespace std; template <typename Item> class MyArray{ private:    Item *myarray;    int size;    int used;    void doubleSize(); public:    MyArray();    ~MyArray();    int length();    void insertHead(Item i);    void insertTail(Item i);    void deleteHead();    void deleteTail();    void sortAscending();    void sortDescending();    Item operator [](int i){        return myarray[i];    } }; template <typename Item> MyArray<Item>::MyArray(){    size = 5;    used = 0;    myarray = new Item[size];...

  • please write the code in C++ 2 Base class File 3 Derived class PDF 3.1 Class...

    please write the code in C++ 2 Base class File 3 Derived class PDF 3.1 Class declaration • The class PDF inherits from File and is a non-abstract class 1. Hence objects of the class PDF can be instantiated. 2. To do so, we must override the pure virtual function clone) in the base class • The class declaration is given below. • The complete class declaration is given below, copy and paste it into your file. . It is...

  • struct nameType { string first; string last; }; struct studentType { nameType name; double gpa; };...

    struct nameType { string first; string last; }; struct studentType { nameType name; double gpa; }; a. Write a program that declares an array of the studentType structure, size 10, name of your choice, and includes the following: • Write a void function to request user input into each of the members of the studentType structure array. Be sure to include proper user prompts. Assume the entire structure array is filled. Write the prototype and the function definition. • Call...

  • please write in c++. 4 Derived class JPG 4.1 Class declaration • The class JPG inherits...

    please write in c++. 4 Derived class JPG 4.1 Class declaration • The class JPG inherits from File and is a non-abstract class. 1. Hence objects of the class JPG can be instantiated. 2. To do so, we must override the pure virtual function clone() in the base class. • The class declaration is given below. class JPG : public File { public: JPG(const std::string& n, int n, int w, const double rgb()) : File(...) { // ... } *JPG()...

  • #ifndef STUDENTTESTSCORES_H #define STUDENTTESTSCORES_H #include <string> using namespace std; const double DEFAULT_SCORE = 0.0; class StudentTestScores...

    #ifndef STUDENTTESTSCORES_H #define STUDENTTESTSCORES_H #include <string> using namespace std; const double DEFAULT_SCORE = 0.0; class StudentTestScores { private: string studentName; // The student's name double *testScores; // Points to array of test scores int numTestScores; // Number of test scores // Private member function to create an // array of test scores. void createTestScoresArray(int size) { numTestScores = size; testScores = new double[size]; for (int i = 0; i < size; i++) testScores[i] = DEFAULT_SCORE; } public: // Constructor StudentTestScores(string...

  • Redesign your Array class from lab6 as a class template to work with the application below....

    Redesign your Array class from lab6 as a class template to work with the application below. write your overloaded output stream operator as an inline friend method in the class declaration. Include the class template header file in your application as below. #include "Array.h" main() {   Array<char> c(3);   c.setValue(0,'c');   c.setValue(1,'s');   c.setValue(2,'c');   cout << c;   Array<int> i(3);   i.setValue(0,1);   i.setValue(1,2);   i.setValue(2,5);   cout << i;   Array<int> j(3);   j.setValue(0,10);   j.setValue(1,20);   j.setValue(2,50);   cout << j;   Array<int> ij;   ij = i + j;   cout << ij;...

  • C Programming Language 2(a) Define a struct with 1 int array named i, 1 float array...

    C Programming Language 2(a) Define a struct with 1 int array named i, 1 float array named f, and one double array named d, each of size M. (b)Declare array x with N of those structs. (c)Write a void function to traverse array x (using a pointer) assigning to each element in each array d (in each struct in array x) the sum of the corresponding elements in arrays i and f (in the same struct). Use 3 pointers (of...

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