结果不理想,哪里错误了?

来源:百度知道 编辑:UC知道 时间:2024/05/03 00:33:57
#include <iostream.h>
class exe
{
private:
int x,y;
public:
exe(int a,int b)
{
a=x;
b=y;
}
int showx()
{
return x;
}
int showy()
{
return y;
}
};
void main()
{
int i;
exe sz[4]={exe(1,2),exe(3,4),exe(5,6),exe(7,8)};
exe *p;
p=&sz[3];
for(i=0;i<4;i++)
{
cout<<p->showx();
cout<<p->showy();
p--;
cout<<"\n";
}
}

结果应该是
78
56
34
12

#include <stdafx.h>
#include <iostream.h>

//using namespace std;
class exe
{
private:
int x,y;

public:
exe(int a,int b)
{
x=a;
y=b;
}
int showx()
{
return x;
}
int showy()
{
return y;
}
};

void main(void)
{
int i;
exe sz[4]={exe(1,2),exe(3,4),exe(5,6),exe(7,8)};
exe *p;
p=&sz[3];
for(i=0;i<4;i++)
{
cout<<p->showx();
cout<<p->showy();
p--;
cout<<"\n";
}
}
应该是这样的, 你的那个
exe(int a,int b)
{
a=x;
b=y;
}
应该是
exe(int a,int b)
{
x=a;
y=b;
}

exe(int a,int b)
{
a=x; //这两句话错了 x=a;
b=y; // 同上 y=b;
}
x,y才是exe类的成员变量,a,b只是形参