Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Toy Class Consider the definition of class Toy below, representing a type of toy that is being sold at the toy store. The comments in

Toy Class

Consider the definition of class Toy below, representing a type of toy that is being sold at the toy store. The comments in the code define the purpose of each variable, constant and method (it is also found in Toy.h).

class Toy { private: std::string name; // the toy name, e.g. firetruck int numInStock; // how many of this toy is in stock static const int MAX_IN_STOCK = 20; // maximum number of a toy that can be kept in stock public: // Accessors std::string GetName() { return name; } int GetNumInStock() { return numInStock; } }; 

Do not create any new member variables. You will need to create the following 4 member functions---also know as methods---all of which must be public.

Constructor

1) The constructor, which takes a string and an integer parameter---in that order. The string is the Toy name, and the integer represents how many of this Toy you would like to stock. The constructor should check that:

  • the value of the string parameter is longer than zero. If it is not, set name to "unknown".
  • that the int parameter matches the same constraints as defined in the SetNumInStock method (If the parameter is greater than MAX_IN_STOCK, this constructor must set numInStock to MAX_IN_STOCK).

Note: you must create the above constructor with parameter types exactly as specified above. Otherwise, all the unit tests will fail.

SetNumInStock method

2) Public mutator method SetNumInStock that takes an integer as a parameter and sets the member variable numInStock to that number only if the parameter is not negative. If the parameter is greater than MAX_IN_STOCK, this method must set numInStock to MAX_IN_STOCK. The method does not return any value.

Note: you must create the above method with the return type and parameter type exactly as specified above. Otherwise, the unit tests will fail.

SetName method

3) Public mutator method SetName that takes a string as a parameter and sets the Toy's name to that value, but only if the string parameter has length longer than zero. Otherwise the method leaves name unchanged. The method does not return any value.

Note: you must create the above method with the return type and parameter type exactly as specified above. Otherwise, the unit tests will fail.

ToString method

4) Public ToString method---no parameters---that should return a string with the name of the toy and how many are in stock using the format in the following example: Assuming there is a toy named "Legos" and there are 15 in stock, then ToString returns the string "15 Legos in stock"

Hint: if you include you can use the function to_string which takes an integer as a parameter and returns the string representation of that integer.

Note: you must create the above method with the return type and no parameters exactly as specified above. Otherwise, the unit tests will fail.

Integration and main

Finally, add code in your main function to:

  1. Create one Toy named Truck with 8 in stock, and another Toy named Blocks with 19 in stock.
  2. Print out the two toys: the Truck first, then the Blocks---use the ToString method.
  3. Change the name of the Blocks toy to "Blocks Plus" and use ToString to print out the information for this toy.
  4. Ask the user how many more Trucks to stock (use the following string "How many more trucks would you like to stock?"), then use the user input to try to stock this many more. You must use the accessor and mutator methods (GetNumInStock and SetNumInStock) to do this. Use ToString to print out the information for this toy.

Sample Input

4 

Sample Output

8 Truck in stock 19 Blocks in stock 19 Blocks Plus in stock How many more trucks would you like to stock? 12 Truck in stock

main.cpp

#include #include "Toy.h"

using namespace std;

int main() { // TO DO return 0; }

Toy.h

#ifndef TOY_H #define TOY_H

#include

using namespace std;

class Toy {

private: string name; // the toy name, e.g. firetruck int numInStock; // how many of this toy is in stock const int MAX_IN_STOCK = 20; // maximum number of a toy that can be kept in stock public: // Accessors string GetName() const; // Do NOT change int GetNumInStock() const;// Do NOT change };

#endif // TOY_H

Toy.cpp

#include "Toy.h"

using namespace std;

string Toy::GetName() const { return name; } // Do NOT change int Toy::GetNumInStock() const { return numInStock; } // Do NOT change // TO DO: add the needed implementations of the required methods

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Financial management theory and practice

Authors: Eugene F. Brigham and Michael C. Ehrhardt

12th Edition

978-0030243998, 30243998, 324422695, 978-0324422696

Students also viewed these Programming questions