热狗站静态功能问题
Hot Dog Stand static function issue
我需要这个程序来创建一个新的HotDogStand对象,该对象能够跟踪每个摊位单独和所有摊位一起销售了多少热狗,我不知道如何使我的静态方法工作以找到所有摊位之间销售的热狗总数。有人能给我指个正确的方向吗?
#include <iostream>
using namespace std;
class HotDogStand
{
public:
HotDogStand(int id, int hds);
void justSold();
int getNumSold();
int getID();
int getTotalSold();
private:
int idNum;
int hotDogsSold;
static int totalSold;
};
HotDogStand::HotDogStand(int id, int hds)
{
idNum = id;
hotDogsSold = hds;
return;
}
void HotDogStand::justSold()
{
hotDogsSold++;
return;
}
int HotDogStand::getNumSold()
{
return hotDogsSold;
}
int HotDogStand::getID()
{
return idNum;
}
int HotDogStand::getTotalSold()
{
totalSold = 0;
totalSold += hotDogsSold;
}
int main()
{
HotDogStand s1(1, 0), s2(2, 0), s3(3, 0);
s1.justSold();
s2.justSold();
s1.justSold();
cout << "Stand " << s1.getID() << " sold " << s1.getNumSold() << "." << endl;
cout << "Stand " << s2.getID() << " sold " << s2.getNumSold() << "." << endl;
cout << "Stand " << s3.getID() << " sold " << s3.getNumSold() << "." << endl;
cout << "Total sold = " << s1.getTotalSold() << endl;
cout << endl;
s3.justSold();
s1.justSold();
cout << "Stand " << s1.getID() << " sold " << s1.getNumSold() << "." << endl;
cout << "Stand " << s2.getID() << " sold " << s2.getNumSold() << "." << endl;
cout << "Stand " << s3.getID() << " sold " << s3.getNumSold() << "." << endl;
cout << "Total sold = " << s1.getTotalSold() << endl;
}
全局(在类之外)必须定义静态变量:
int HotDogStand::totalSold = 0;
改变void HotDogStand::justSold()
{
hotDogsSold++;
totalSold++; // increment here
return;
}
和
int HotDogStand::getTotalSold()
{
return totalSold; // just return value
}
相关文章:
- 在用于格式4的arm模拟器中实现功能时的一个问题
- 解析模板的好友功能时出现问题
- creat_list2功能有什么问题?
- 此工厂功能有什么问题?
- 具有sleep_for和sleep_until功能的 gcc 可能存在的问题
- Sinlge 链表,C++,删除所有和搜索功能的问题
- VS2019中内联功能的一些问题
- 我的显示功能有问题
- MPI 二进制文件 I/O 基本功能和性能问题
- C++ 初级面试问题:仅使用字符指针压缩字符序列的功能
- C 功能性能问题
- 此读取功能有什么问题
- in_degree功能出现问题
- 我的选择排序功能C 有什么问题
- BOOST-ASIO ASYNC_RECEIVE_FROM功能超载问题( 动态指针)
- 朋友功能可访问性问题
- 我的C++作业中的"solve"功能有什么问题?
- 面临CPP中排序功能的问题
- 反向指针功能问题
- C++ lower_bound比较功能问题