Question: in C++ please solve it step by step. thanks So far in class weve implemented a SkittlesDish and weve begun to implement a Bag. For

in C++ please solve it step by step. thanks
So far in class weve implemented a SkittlesDish and weve begun to implement a Bag. For this second programming assignment, you will design and implement a SkittlesBag, which is a special-purpose bag designed precisely for holding skittles. You will design the interface for your SkittlesBag in a header file called skittlesbag.h and you will implement it in skittlesbag.cpp.
Functionality will be graded based on whether your class is implemented such that running hw2test.cpp
produces console output that matches the sample run in this handout. However, your overall assignment
grade will also consider your object-oriented design decisions and general programming style. Good style
essentially means easily readable code, including documentation, indentation, descriptive variable names, and procedural intuitive solutions. Additional guidance is given at the end of this handout.
Do not change filenames for the files you must submit (skittlesbag.h and skittlesbag.cpp).
Your only functionality requirement is to implement public member functions consistent with the following descriptions that can be used as invoked by the starter test code. In particular, you get
to decide what private member variables will be useful to you when defining the following member functions for your SkittlesBag class.
Your class should define two constructors consistent with the constructor invocation in the test code. This
requires a 0-argument constructor and a 5-argument constructor; the 5-argument constructor should respect the color ordering on the back of my actual bag of skittles: red, yellow, green, orange, purple.
Three member functions serve as accessors: any good container has a size function, and yours should
return the total number of skittles in the bag; a function count should return the count of the color designated by some argument r, y, g, o, or p (it can return zero if an illegal argument is provided); and
printHist should display the contents of the bag to the console as a color-indexed histogram.
There are several member functions that serve indirectly as mutators. The simplest is addOne, which
should be invoked on a bag object by passing in a character to indicate specify the color of a single skittle to be added to the bag, and the next simplest is eat, which should be invoked on a bag object by passing in an int and a designated character to specify how many of the stated color to remove from the bag. This time, we should not be so punishing toward greedy eaters requesting more of a particular color than is present results in eating the entire supply of that color. For either eating or adding, if a character other than the designated 5 is passed in, the function can simply do nothing. The most algorithmically interesting mutator is evenOut,
which has no parameters but instead involves eating the minimum amount of each color to result in the a
perfectly color-balanced bag. This function reports how many of each type was eaten and the number of each that remain. You will practice using classes inside classes by defining pourInDish, which empties the contents of a (monochromatic) SkittlesDish object into the calling SkittlesBag object. To do this,
you should #include the skittlesdish.h file from Week 2, and you will also need to have Week 2s
skittlesdish.cpp in your directory for compilation.
In addition to these four member functions, you should overload two operators. Overload the += operator as a void member function so that b1+=b2 results in emptying b2 into b1; this is similar to what we did in class for SkittlesDish, but this time you should implement it as a member function and make it void (so you wont have to return dereferenced this). Finally, overload == as a non-member non-friend function so that it returns true only if the color counts of one bag match the color counts of the other.
Additional guidance.
As soon as you look at this assignment, download all the files into a directory (without other copies of
files with the same name), replace the test(); invocation in the main() function of hw2test.cpp
with a command like cout
environment is set up properly. My compilation and running commands are as follows:
g++ hw2test.cpp skittlesbag.cpp skittlesdish.cpp -o test -std=c++11
./test
Alternatively:
g++ hw2test.cpp skittlesbag.cpp skittlesdish.cpp -std=c++11
./a.out
Since skittlesdish.cpp shouldnt change, you can omit that after the first successful compilation.
Note that your bag and dish .h files also need to be in the same directory as the three .cpp files.
You may choose to follow the private member variable naming convention we used in our Bag code,
but you dont have to.
You will not lose functionality points for making all member fields public, but this is bad OO so you
will certainly lose design points!
Similarly but more subtly, you are not required to use const anywhere in your code, but you will
only receive full credit if you modify parameters and member functions with const when the relevant
objects should not be modified by the function.
A class constant specifying that there are 5 skittles colors is not necessary, but it might be a nice touch.
Your .h file may include inline function definitions, but any function whose definition does not fit on a
single line with its header should instead be defined in the .cpp file.
You may assume all values are small enough that you can always use int instead of std::size t.
You should not require any additional #include or using directives in either skittlesbag file.
hw2test.cpp
#include
#include "skittlesbag.h"
#include "skittlesdish.h"
using namespace std;
void test();
int main() {
test();
return 0;
}
void test() {
cout
SkittlesBag smallBag;
cout
cout
SkittlesBag bigBag(4,3,5,9,7);
cout
bigBag.printHistogram();
cout
smallBag.addOne('g');
cout
cout
bigBag.evenOut();
cout
bigBag.eat(5,'y');
bigBag.printHistogram();
cout
SkittlesDish dish(10, "red");
cout
cout
bigBag.pourInDish(dish);
cout
bigBag.printHistogram();
cout
cout
SkittlesBag newBag(0,0,1,0,0);
cout
cout
cout
bigBag+=newBag;
cout
cout
bigBag.printHistogram();
}
skittlesdish.cpp
#include "skittlesdish.h"
SkittlesDish::SkittlesDish() : count(0), color("red") { }
SkittlesDish::SkittlesDish(int initialCount, string color) :
count(initialCount), color(color) {}
int SkittlesDish::getCount() const{
return count;
}
string SkittlesDish::getColor() const {
return color;
}
void SkittlesDish::addSkittles(int numToAdd) {
count += numToAdd;
}
void SkittlesDish::eatSkittles(int numToEat) {
if (count
cout
} else {
count -= numToEat;
}
}
// == as member function
bool SkittlesDish::operator ==(const SkittlesDish& rhs) const {
return (count==rhs.count && color==rhs.color);
}
//
bool operator
return (lhs.count
}
// += as a nonmember nonfriend function
SkittlesDish operator +=(SkittlesDish& lhs, SkittlesDish& rhs) {
if (lhs.getColor()!=rhs.getColor()) { // check for illegal combine attempt
cout
} else {
int n = rhs.getCount(); // local copy of starting value in rhs
rhs.eatSkittles(n); // "eat" as public way of emptying rhs
lhs.addSkittles(n); // add the same number of skittles to lhs
}
return lhs; // convention for +=
}
// += as a member function (declaration in .h file would also change)
// note both private fields of "this" (previously lhs) and rhs are accessible
/*void SkittlesDish::operator +=(SkittlesDish& rhs) {
if (color!=rhs.color) {
cout
} else {
count += rhs.count;
rhs.count = 0;
}
// if we're fine with breaking convention, we can make this function void;
// otherwise, as the SkittlesDish return val we'd have to return *this;
}*/
in C++ please solve it step by step. thanks So far in
class weve implemented a SkittlesDish and weve begun to implement a Bag.
#ifndef SKITTLESDISH_H #define SKITTLESDISH_H #include using namespace std; 1/ keeps track of count of a monochromatic dish of skittles class SkittlesDish { public: // constructors Skittles Dish(); // 0 red skittles Skittles Dish(int initialCount, string color); // accessors and mutators int getCount( const; string getColor const; void addSkittles (int numToAdd); // increase the count by numToAdd void eatSkittles (int numToEat); // decrement count, or do nothing if count

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!