发新话题
打印

为什么和我想要的结果不一样啊??????

为什么和我想要的结果不一样啊??????

#include <iostream.h>
class point
{
  public:
  int x;
  int y;
  point(int a,int b)
  {
   x=a;
   y=b;
  }
  void output()
  {
    cout<<x<<endl<<y<<endl;
  }
  void output(int x,int y)
  {
    x=x;
    y=y;
  }
};
void main()
{
   point pt(3,3);
   pt.output(5,5);
   pt.output();
}


我想得到

5  5

结果是
3  3

为什么???

TOP

void output(int x,int y)
这一行有问题,也就是说这个函数有问题,因为参数都一样了。
x=x;
y=y;
这样过后,x还是x,y还是y,没有变化。
你应当写成这样
[code:2fc656d74a]void output(int x1,int y1)
{
    x=x1;
    y=y1;
} [/code:2fc656d74a]
或者这样也行
point pt(3,3);
pt.x=5;
pt.y=5;
pt.output();

TOP

我认为是这样的

#include <iostream.h>
class point
{
  public:
  int x;
  int y;
  point(int a,int b)
  {
   x=a;
   y=b;
  }

  void output(int x1,int y1)
  {
    x=x1;
    y=y1;
cout<<x<<","<<y;
  }
};
void main()
{
   point pt(3,3);
   pt.output(5,5);
   
}你的上面的参数赋值不正确
或者这样也可:
#include <iostream.h>
class point
{
  public:
  int x;
  int y;
  point(int a,int b)
  {
   x=a;
   y=b;
  }
  void output()
  {
          cout<<x<<","<<y;
  }
  void output(int x1,int y1)
  {
    x=x1;
    y=y1;
  }
};
void main()
{
   point pt(3,3);
   pt.output(5,5);
   pt.output();
}但是这样效率不高,而且多余

TOP

看看這樣行不行

#include <iostream.h>
class point
{
  public:
  int x;
  int y;
  point(int *a,int *b)
  {
   x=&a;
   y=&b;
  }
  void output()
  {
          cout<<x<<","<<y;
  }
  void output(int &x,int &y)
  {
    x=a;
    y=b;
  }
};
void main()
{
   point pt(3,3);
   pt.output(5,5);
   pt.output();
}
yangfeng

TOP

发新话题