use c++ Write a class called VendSlot. It should have two data members - a Snack
ID: 3835763 • Letter: U
Question
use c++
Write a class called VendSlot. It should have two data members - a Snack, and the amount of that Snack that is currently in the slot. It should also have a default constructor that initializes the data members to a default Snack object and 5. The class should have a constructor that takes two parameters and uses them to initialize the data members. It should have get methods for both data members. It should have a method called decrementAmount that decreases the amount by 1.
For the VendSlot class:
getSnack
getAmount
decrementAmount
For example, these classes could be used as follows:
The files must be named VendSlot.hpp, VendSlot.cpp, VendSlotMain.cpp
Please use comments to explain
Explanation / Answer
// VendSlot.hpp
#include <string>
#include "Snack.hpp"
using namespace std;
class VendSlot{
private:
Snack snack;
int amount;
public:
VendSlot();
VendSlot(Snack s, int p);
Snack getSnack();
int getAmount();
void decrementAmount();
};
// VendSlot.cpp
#include <string>
#include <iostream>
#include "VendSlot.hpp"
using namespace std;
VendSlot::VendSlot(){
amount = 5;
}
VendSlot::VendSlot(Snack s, int p){
snack = s;
amount = p;
}
Snack VendSlot::getSnack(){
return snack;
}
int VendSlot::getAmount(){
return amount;
}
void VendSlot::decrementAmount(){
amount--;
}
// VendSlotMain.cpp
#include <string>
#include <iostream>
#include "VendSlot.hpp"
using namespace std;
int main(){
Snack s1("corn chips", 0.75, 200);
Snack s2("candy bar", 1.25, 300);
Snack s3("root beer", 2.00, 450);
VendSlot vs1(s1, 2);
VendSlot vs2(s2, 1);
VendSlot vs3(s3, 0);
VendSlot vs4; // five bottles of water
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.