C++ ...方形系列(每次输出相同)?

Yas*_* Ym 0 c++ class function

每当我改变s1.sqseries输出中的值时永远不会改变.它始终是687194768.为什么会这样?

 #include<iostream>
 #include<cmath>
 #include<string>
 #include<iomanip>
 using namespace std;
 class square
 {
int a,b;
 public:
void sqseries(int x,int y)
{
    x=a;
    y=b;

    int c;
   for(c=a;c<=b;c++)
  {

   cout<<c*c<<endl;
  }
}
};
 int main ()
 {
  square s1;
  s1.sqseries(5,8);            /////////right here///////
  return 0;
  }
Run Code Online (Sandbox Code Playgroud)

Alo*_*ave 5

你需要:

void sqseries(int x,int y)
{
    a=x;
    b=y;
    ...
}
Run Code Online (Sandbox Code Playgroud)

需要注意的是在当前的代码,你只需重新分配x和y使用a,并b里面你因此被值作为函数的参数传递功能从未使用过,但他们只是用的值覆盖a和b.