问题遇到的现象和发生背景
问题相关代码,请勿粘贴截图
#include
#include
#include
using namespace std;
static int orangenum=0, applenum=0, peachnum=0;
class fruit
{
private:
string m_Name;
string m_origin;
int m_weith;
int m_level;
public:
string Name(string name)
{
m_Name = name;
return m_Name;
}
string Origin(string origin)
{
m_origin = origin;
return m_origin;
}
int weith(int weith)
{
m_weith = weith;
return m_weith;
}
int level(int level)
{
m_level = level;
return m_level;
}
void output()
{
if (m_weith > 100 && m_level == 1 && m_Name == "orange")
{
orangenum++;
}
if (m_weith > 100 && m_level == 1 && m_Name == "apple")
{
applenum++;
}
if (m_weith > 100 && m_level == 1 && m_Name == "peach")
{
peachnum++;
}
cout << "orange: " << orangenum << " ";
cout << "apple: " << applenum << " ";
cout << "peach: " << peachnum << " ";
}
};
int main()
{
int n, i;
cin >> n;
string Name;
string origin;
int weith;
int level;
fruit A;
for (i = 0; i < n; i++)
{
cin >> Name >> origin >> weith >> level;
A.Name(Name);
A.Origin(origin);
A.weith(weith);
A.level(level);
}
A.output();
system("pause");
return 0;
}
运行结果及报错内容
我的解答思路和尝试过的方法
我想要达到的结果
怎么可以防止变量重复初始化