Click here to Skip to main content
15,305,480 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
▪ Youoperateseveralhotdogstandsdistributedthroughouttown.Definea 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 so that you can track the total number of hot dogs sold by the stand.

▪ Addanotherfunctionthatreturnsthenumberofhotdogssold.

▪ 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.


I Want to make hotdog stand without #include <cstdlib>
Please help me...



The result:
ex)

Stand count(3~10 input) : input number -> 5
ID:0
ID:0
ID:3
ID:2
ID:0
ID:3
ID:4
ID:3
ID:2
ID:2
Stand 0 sold 3
Stand 1 sold 0
Stand 2 sold 3
Stand 3 sold 3
Stand 4 sold 1
Total sold = 10

What I have tried:

C++
<main.cpp>

#include <iostream>
#include "Hotdog.h"
using namespace std;

int main()
{
	int count;
	HotDogStand* sList;
	cout << "Stand count(3~10개) : ";
	cin >> count;
	sList = new HotDogStand[count];
	
	for (int i = 0; i < count; i++) {
		sList[i].SetID(i);
	}


	char input;
	while (true)
	{
		cout << "ID : ";
		cin >> input;
		
		if (input == 'q' || input == 'Q') {
			break;
		}

		for (int i = 1; i < count; i++) {
			HotDogStand(i, 0);
		}

	}


	for (int i; i < count; i++) {
		sList[i].JustSold();
		cout << "stand" << sList[i].GetID() << "sold" << sList[i].GetNumSold() << endl;
	}


	cout << "Total sold = " << sList[].GetTotalSold() << endl;


	return 0;
}


<hotdog.h>

#ifndef HOTDOG.H
#define HOTDOG.H


class HotDogStand
{
public:
	HotDogStand();
	HotDogStand(int newID, int newNnumSold);
	int GetID();
	void SetID(int newID);
	void JustSold();
	int GetNumSold();
	static int GetTotalSold();

private:
	static int totalSold;
	int numSold;
	int ID;
};


#endif // !HOTDOG.H


<hotdog.cpp>


#include <iostream>
#include "Hotdog.h"
using namespace std;


int HotDogStand::totalSold = 0;

HotDogStand::HotDogStand()
{
    numSold = 0;
    ID = 0;
    totalSold++;
}

HotDogStand::HotDogStand(int newID, int newNnumSold)
{
    numSold = newNnumSold;
    ID = newID;
    totalSold++;
}

int HotDogStand::GetID()
{
    return ID;
}

void HotDogStand::SetID(int newID)
{
    ID = newID;
}

void HotDogStand::JustSold()
{
    numSold++;
}

int HotDogStand::GetNumSold()
{
    return numSold;
}

int HotDogStand::GetTotalSold()
{
    return totalSold;
}
Posted
Updated 29-Apr-21 6:44am
v2

1 solution

One problem you have is in both constructors of HotDogStand you increment totalSold and both cases are wrong. The first one should not increment it all since numSold for that instance is zero. The second one that is passed newNnumSold should increment it by that value instead of by just one. Additionally, the method JustSold should increment totalSold also.

Personally, I would not do it that way. To obtain the total I would iterate through all of the stands and sum the number each one sold. That's my personal preference. As you can see, doing it your way can be prone to error. The main reason is I write lots of multithreaded code and static variables can be problematic. Critical sections are usually required to access them safely and there are usually better ways to deal with that kind of data.
   
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900