SHARE
SPREAD
HELP

The Tradition of Sharing

Help your friends and juniors by posting answers to the questions that you know. Also post questions that are not available.


To start with, Sr2Jr’s first step is to reduce the expenses related to education. To achieve this goal Sr2Jr organized the textbook’s question and answers. Sr2Jr is community based and need your support to fill the question and answers. The question and answers posted will be available free of cost to all.

 

#
Authors:
Walter Savitch ,kenrick Mock
Chapter:
Constructors And Other Tools
Exercise:
Programming Projects
Question:4 | ISBN:9780132846813 | Edition: 5

Question

You operate several hot dog stands distributed throughout town. Define a class named HotDogStand that has a member variable for the hot dog stand’s ID number and a member variable for how many hot dogs the stand sold that day. Create a constructor that allows a user of the class to initialize both values.

Also create a function named JustSold that increments the number of hot dogs the stand has sold by one. This function will be invoked each time the stand sells a hot dog so that you can track the total number of hot dogs sold by the stand. Add another function that returns the number of hot dogs sold.

Finally, add a static variable that tracks the total number of hot dogs sold by all hot dog stands and a static function that returns the value in this variable. Write a main function to test your class with at least three hot dog stands that each sell a variety of hot dogs.

TextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbook

Answer

#include <iostream>
#include<cstdlib>
using namespace std;

class HotDogStand 
{            
private:
	int IDnumber;
	int HotDogsSold;
	static int totalSold;

public:
	HotDogStand();
	HotDogStand(int IDnumber, int hotDogsSold); 
	int getID();
	void setID(int ID);
	void JustSold();
	int getNumSold();
	static int getTotalSold();
};
int HotDogStand::totalSold=0;

int main() 
{
	HotDogStand a(1,0);
	HotDogStand b(2,0);
	HotDogStand c(3,0);
	HotDogStand d;
	a.setID(1);
	b.setID(2);
	c.setID(3);
	a.JustSold();
	b.JustSold();
	a.JustSold();
	cout<<"Id "<<a.getID()<<" sold "<<a.getNumSold()<<endl;
	cout<<"Id "<<b.getID()<<" sold "<<b.getNumSold()<<endl;
	cout<<"Id "<<c.getID()<<" sold "<<c.getNumSold()<<endl;
	cout<<"Total sold="<<a.getTotalSold()<<endl;
	system("PAUSE");
	return 0; 
}
HotDogStand::HotDogStand()
{
	IDnumber=0;
	HotDogsSold=0;

}
HotDogStand::HotDogStand(int IDnumber, int hotDogsSold)
{
	IDnumber=IDnumber;
	HotDogsSold=hotDogsSold;
}
int HotDogStand::getID()
{
	return IDnumber;
}
void HotDogStand::setID(int newID)
{
	IDnumber=newID;
}
void HotDogStand::JustSold()
{
	HotDogsSold++;
	totalSold++;

}
int HotDogStand::getNumSold()
{
 return HotDogsSold;
}
int HotDogStand::getTotalSold()
{
	return totalSold;
}

Output:

0 0

Discussions

Post the discussion to improve the above solution.