C++ You operate several hot dog stands distributed throughout town and need to t
ID: 3719642 • Letter: C
Question
C++
You operate several hot dog stands distributed throughout town and need to track sales.
Define a class named HotDogStand
has a member variable for the hot dog stand's ID number and a
has a member variable for how many hot dogs the stand has sold that day.
has a constructor that allows a user of the class to initialize both values.
has a function named "JustSold"
increments the number of hot dogs the stand has sold by one.
(this method will be invoked each time the stand sells a hot dog)
has a function that returns the number of hot dogs sold.
has a static variable called totalDogs that tracks the total number of hotdogs sold by all hot dog stands (should be updated as part of each sale)
has a static function that returns the value of totalDogs
Write a main function that will:
Welcomes
create 3 hotdog stands
initialize the stands to the ids 1,2,3
initialize sales to 0
display the current sales for each stand
display totalDogs
invoke the JustSold function once for each stand
display the current sales for each stand
display totalDogs
Use 3 loops to:
invoke JustSold 123 times for stand 1
invoke JustSold 101 times for stand 2
invoke JustSold 134 times for stand 3
display the current sales for each stand
display totalDogs
Goodbye
Explanation / Answer
Hi,
Please find my working code.
#include <iostream>
using namespace std;
//Class definition
class HotDogStand
{
//Private variables
private:
static int tSold;
int idNum;
int soldNum;
//Public members
public:
//Constructor
HotDogStand(int id, int sold)
{
idNum = id;
soldNum = sold;
}
//Function that increments sold number
void justSold()
{
soldNum += 1;
tSold += 1;
}
//Function that returns id
int getID()
{
return idNum;
}
//Function that returns sold number
int getNumSold()
{
return soldNum;
}
//Function that returns total number sold
static int getTotalSold()
{
return tSold;
}
};
//Initializing static variable
int HotDogStand::tSold = 0;
//Main function
int main()
{
//Creating objects
HotDogStand s1(1,0), s2(2,0), s3(3,0);
//Updating items sold
s1.justSold();
s2.justSold();
s1.justSold();
//Printing current status
cout << "Stand " << s1.getID() << " sold " << s1.getNumSold() << endl;
cout << "Stand " << s2.getID() << " sold " << s2.getNumSold() << endl;
cout << "Stand " << s3.getID() << " sold " << s3.getNumSold() << endl;
//Printing total sold
cout << endl << "Total sold = " << s1.getTotalSold() << endl;
cout << endl;
//Updating items sold
s3.justSold();
s1.justSold();
//Printing current status
cout << "Stand " << s1.getID() << " sold " << s1.getNumSold() << endl;
cout << "Stand " << s2.getID() << " sold " << s2.getNumSold() << endl;
cout << "Stand " << s3.getID() << " sold " << s3.getNumSold() << endl;
//Printing total sold
cout << endl << "Total sold = " << s1.getTotalSold() << endl;
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.