爲什麼方法payment1
給出了不同的輸出,然後payment2
? payment2
具有相同的參數名稱,然後方法名稱(我希望我說的是正確的?)。我的意思是代碼this->moneysamename+=moneysamename;
。爲什麼編譯器不能區分這兩個變量?與參數同名的方法導致不同的輸出
下面是完整的代碼:
#include <iostream>
using namespace std;
class Person
{
protected:
static int money;
static int moneysamename;
public:
virtual void payment1(float money1) = 0;
virtual void payment2(float moneysamename) = 0;
};
class Worker : public Person
{
public:
virtual void payment1(float moneyanothername)
{
this->money+= moneyanothername;
cout << "Worker: " << money << endl;
};
virtual void payment2(float moneysamename)
{
this->moneysamename+= moneysamename;
cout << "Worker: " << moneysamename << endl;
};
};
void pay1(Person &m, float sum)
{
m.payment1(sum);
}
void pay2(Person &m, float sum)
{
m.payment2(sum);
}
int Person::money = 0;
int Person::moneysamename = 0;
int main()
{
Worker Gaston;
pay1(Gaston, 200);
pay1(Gaston, 300);
pay2(Gaston, 200);
pay2(Gaston, 300);
}
輸出:
Worker: 200
Worker: 500
Worker: 200
Worker: 300
無法理解問題。請說明確切的問題 - 你的期望輸出是什麼(你爲什麼期望它是這樣的)。 – SergeyA
他們爲什麼是「靜態」? –