Question

In: Computer Science

Objective You are given a partial implementation of one header file, GildedRose.h. Item is a class...

Objective

You are given a partial implementation of one header file, GildedRose.h. Item is a class that holds the information for each item for the inn. GildedRose is a class that holds an internal listing of many Item objects. This inventory should hold at least 10 items. For this you can use arrays, the std::array class, or even the vector class.

Complete the implementation of these classes, adding public/private member variables and functions as needed. You should choose an appropriate data structure to maintain this inventory with an unknown size known only at runtime. Your code is tested in the provided main.cpp.

You will need to implement the following functions:

  • Constructors/Destructors - Initialize your data. Allocate memory if using a native array. The destructor should deallocate memory if using a native array.

  • size() - This should return the number of items currently for sale (this is different from the max).

  • get(size_t) - This should return the item with the matching index. For example if given an index of 3, you should return the item at index 3 in the list.

  • add(Item) - This should add another item for sale in the Gilded Rose by adding it to your inventory.

  • operator[](size_t) - This should perform identical to the get(size_t) function.

SKELETAL CODE FOR HEADER FILE:

#pragma once

#include <string>
using std::string;

// This is already done for you...
class Item {
public:
   string name;
   int sellIn;
   int quality;
   Item(string, int, int);
};

Item::Item(string new_name, int new_sellIn, int new_quality)
   : name(new_name), sellIn(new_sellIn), quality(new_quality) {
}


// This class is incomplete...
class GildedRose {
private:
   // Add something to hold at least 10 items

public:
   GildedRose();
   ~GildedRose();

   size_t size() const;
   Item& get(size_t);
   void add(const Item&);

   Item& operator[](size_t);
};

Code should be in C++

Solutions

Expert Solution

Screenshot

--------------------------------------------------------

Program

Main.cpp

#include "stdafx.h"
//Header files
#include<iostream>
#include "GildedRose.h"
#include "item.h"
using namespace std;

int main()
{
   Item item1("Bench", 1, 1);
   Item item2("Chair", 1, 1);
   GildedRose gr;
   gr.add(item1);
   gr.add(item2);
   cout << "Item from index 1(using[]):";
   cout << gr[1].name << " " << gr[1].sellIn << " " << gr[1].quality << endl;
   cout << "Get the number of items in GildedRose:" << gr.size() << endl;
   cout << "Item from index 1(using get):";
   cout << gr.get(0).name<< " " << gr.get(0).sellIn << " " << gr.get(0).quality << endl;
    return 0;
}

Item.h

#pragma once
#include <string>
using std::string;

// This is already done for you...
class Item {
public:
   string name;
   int sellIn;
   int quality;
   Item(string, int, int);
};

Item.cpp

#include "item.h"
Item::Item(string new_name, int new_sellIn, int new_quality)
   : name(new_name), sellIn(new_sellIn), quality(new_quality) {
}

GildedRose.h

#pragma once
#include "item.h"
#include<vector>
using std::vector;
class GildedRose{
private:
   // Add something to hold at least 10 items
   vector<Item> items;

public:
   GildedRose();
   ~GildedRose();

   size_t size() const;
   Item& get(size_t);
   void add(const Item&);

   Item& operator[](size_t);
};

GildedRose.cpp

#include "GildedRose.h"
//Default constructor
GildedRose::GildedRose(){ }
//Parameterized constructor
GildedRose::~GildedRose() {   }
//Get number of items in the vector
size_t GildedRose::size() const {
   return items.size();
}
//Get item details of the given index
Item& GildedRose::get(size_t i) {
   return items.at(i);
}
//Add an item
void GildedRose::add(const Item& item) {
   items.push_back(item);
}
//Using operator[] to access item details of corresponding index
Item& GildedRose::operator[](size_t i) {
   return items.at(i);
}

-----------------------------------------------------------------------

Output

Item from index 1(using[]):Chair 1 1
Get the number of items in GildedRose:2
Item from index 1(using get):Bench 1 1
Press any key to continue . . .


Related Solutions

Problem Statement: Implement the MyString class using a header and implementation file named MyString.h and MyString.cpp...
Problem Statement: Implement the MyString class using a header and implementation file named MyString.h and MyString.cpp respectively. Make sure to properly test your code on your own by creating a test driver that tests every function created in the MyString class. Deliverables: proj3-MyString.h proj3-MyString.cpp proj3-testMain.cpp Memory Requirements: Your MyString should start with 10 bytes of allocated memory and should grow in size by doubling. So, we should be able to predict the capacity of your MyString as acquiring a patten...
A header file contains a class template, and in that class there is a C++ string...
A header file contains a class template, and in that class there is a C++ string object. Group of answer choices(Pick one) 1)There should be a #include for the string library AND a using namespace std; in the header file. 2)There should be a #include for the string library. 3)There should be a #include for the string library AND a using namespace std; in the main program's CPP file, written before the H file's include.
Complete the provided partial C++ Linked List program. Main.cpp is given and Link list header file...
Complete the provided partial C++ Linked List program. Main.cpp is given and Link list header file is also given. The given testfile listmain.cpp is given for demonstration of unsorted list functionality. The functions header file is also given. Complete the functions of the header file linked_list.h below. ========================================================= // listmain.cpp #include "Linked_List.h" int main(int argc, char **argv) {      float           f;      Linked_List *theList;      cout << "Simple List Demonstration\n";      cout << "(List implemented as an Array - Do...
Study the file polygon.h. It contains the header file for a class of regular polygons. Implement...
Study the file polygon.h. It contains the header file for a class of regular polygons. Implement the methods, and provide a driver to test it. It should be in C++ polygon.h file- #ifndef POLY_RVC_H #define POLY_RVC_H #include <iostream> using namespace std; class Polygon { public:    Polygon();    Polygon(int n, double l);    //accessors - all accessors should be declared "const"    // usually, accessors are also inline functions    int getSides() const { return sides; }    double getLength()...
C++ MAIN remains same Given the complete main() function, partial playlist class header playlist.h, and playlist.cpp,...
C++ MAIN remains same Given the complete main() function, partial playlist class header playlist.h, and playlist.cpp, you will complete the class declaration and class implementation. The following member functions are required: constructor copy constructor destructor addSong(song tune) adds a single node to the front of the linked list no return value displayList() displays the linked list as formatted in the example below no return value overloaded assignment operator A description of all of these functions is available in the textbook's...
Write the header and the implementation files (.h and .cpp separatelu) for a class called Course,...
Write the header and the implementation files (.h and .cpp separatelu) for a class called Course, and a simple program to test it, according to the following specifications:                    Your class has 3 member data: an integer pointer that will be used to create a dynamic variable to represent the number of students, an integer pointer that will be used to create a dynamic array representing students’ ids, and a double pointer that will be used to create a dynamic array...
Write a C++ program that design a class definition to be put in a header file...
Write a C++ program that design a class definition to be put in a header file called fizzjazz.h A store sells online FizzJazz which are sound tones made by famous bands. For each FizzJazz, the store wants to keep track of the following attributes: * title - the name of the sound tone * band - Famous band name that created the tone * duration - this is in seconds and may be fractional: examples: 20.0, 34.5 Each attribute will...
Write a C++ program that design a class definition to be put in a header file...
Write a C++ program that design a class definition to be put in a header file called fizzjazz.h A store sells online FizzJazz which are sound tones made by famous bands. For each FizzJazz, the store wants to keep track of the following attributes: * title - the name of the sound tone * band - Famous band name that created the tone * duration - this is in seconds and may be fractional: examples: 20.0, 34.5 Each attribute will...
Write a C++ program that design a class definition to be put in a header file...
Write a C++ program that design a class definition to be put in a header file called fizzjazz.h A store sells online FizzJazz which are sound tones made by famous bands. For each FizzJazz, the store wants to keep track of the following attributes: * title - the name of the sound tone * band - Famous band name that created the tone * duration - this is in seconds and may be fractional: examples: 20.0, 34.5 Each attribute will...
Using the textbook implementation of integer node given below; public class IntegerNode { public int item;...
Using the textbook implementation of integer node given below; public class IntegerNode { public int item; public IntegerNode next; public IntegerNode(int newItem) { item = newItem; next = null; } // end constructor public IntegerNode(int newItem, IntegerNode nextNode) { item = newItem; next = nextNode; } // end constructor } // end class IntegerNode You need to implement add( ), delete( ), traverse( ) methods for an ordered linked list. And after insertion and deletion, your linked list will remain...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT