程序没有在一个总计变量中加起来来自所有不同办事处的股票
program not adding up the shares from all different offices in one TotalAmount variable
我在C 中学习OOP,该程序旨在从不同的办公室收集股票,并将每个办公室的股份存储在名为SharePeroffice的变量中,并将所有办公室的这些股份添加到一个单一可变中(总计((请参阅班级国家佛教徒的私人成员(..但它没有在TotalAmount变量中添加它们。
#include<iostream>
#include<cstdlib>
using namespace std;
class nationalOffices
{
private:
int sharePerOffice;
int totalShare;
public:
nationalOffices();
nationalOffices(int x);
void setsharePerOffice(int value);
void setTotalShare();
int getTotalShare();
};
nationalOffices::nationalOffices()
{
sharePerOffice = 0;
}
nationalOffices::nationalOffices(int x)
{
totalShare = x;
sharePerOffice = 0;
}
void nationalOffices::setsharePerOffice(int value)
{
sharePerOffice = value;
}
void nationalOffices::setTotalShare()
{
totalShare = totalShare + sharePerOffice;
}
int nationalOffices::getTotalShare()
{
return totalShare;
}
int main ()
{
int shares = 0;
nationalOffices offices[5] = {0};
for (int i = 0; i < 5; i++)
{
cout <<"enter share for office number "<<i+1<<": ";
cin >> shares;
offices[i].setsharePerOffice(shares);
offices[i].setTotalShare();
system("cls");
}
cout <<endl;
cout <<offices[0].getTotalShare();
}
变量offices
是五个不同且独特且唯一的nationalOffices
对象的数组。每个对象都有其自己的成员变量集。
totalShare
成员对于每个对象都是唯一的,并且与其他每个对象的成员变量无关。
而不是将其作为成员变量,而是将总计添加到main
函数内的本地变量。
另一个可能的解决方案是使 totalShare
成为 static
成员变量,在这种情况下,它变成了 class 变量,由类的所有对象共享。
相关文章:
- std::vector的包装器,使数组的结构看起来像结构的数组
- 我可以将一个用clang c++11编译的对象与另一个用c++17编译的对象链接起来吗
- 看起来is_nothrow_constructible_v()在MSVC中被破坏了,我错了吗
- 将大括号括起来的初始值设定项列表作为结构返回
- 学习多线程C++:添加线程不会使执行速度更快,即使它看起来应该
- 将 SDL2 与 CMake 链接起来
- 调用重载的"<大括号括起来的初始值设定项列表>"对于对来说就足够了是模棱两可的
- 如何使用connect将qml按钮与同一类的cpp函数连接起来
- 尽管一切看起来都很好,但值不会交换
- 尝试将 c 字符串数组与分隔符连接起来
- 为什么我会收到此错误?无法将 {lb, ub} 从<大括号括起来的初始值设定项列表>转换为 float(**)(float*, int)
- 有没有办法生成一个包含平方的序列,这些平方加起来就是一个整数平方?
- 计算数组重复次数的组合的有效算法,加起来达到给定的总和
- 自制的上衣:看起来一样,但不完全相同
- 为什么这个复合语句作为用大括号和括号括起来的语句序列似乎不是有效的语句表达式
- 从双引号括起来的子字符串中删除特定字符
- 大括号括起来的初始值设定项列表中的短 c++ 类构造函数
- 嵌套的大括号括起来的初始值设定项列表
- 如何使它看起来像正在下的雪
- 程序没有在一个总计变量中加起来来自所有不同办事处的股票