二维数组作为对象的应用举例:
#include<iostream.h>
class Location
{
int x,y;
public:
Location();
Location(int xx,int yy);
~Location();
void Move(int m,int n);
int GetX(){return x;}
int GetY(){return y;}
};
Location::Location()
{
x=y=0;
cout<<"Default Constructor called."<<endl;
}
Location::Location(int xx,int yy):x(xx),y(yy)
{cout<<"Construcator called."<<endl;}
Location::~Location()
{
cout<<"Destructor called."<<endl;
}
void Location::Move(int m,int n)
{x=m;y=n;}
void main()
{
Location A[2];
for(int i=0;i<2;i++)
cout<<A[i].GetX()<<","<<A[i].GetY()<<endl; //0,0\n0,0
Location B[2]={Location(1,2),Location(3,4)};
for(i=0;i<2;i++)
cout<<B[i].GetX()<<","<<B[i].GetY()<<endl; //1,2\n3,4
for(i=0;i<2;i++)
A[i].Move(i+10,i+20);
for(i=0;i<2;i++)
cout<<A[i].GetX()<<","<<A[i].GetY()<<endl;//A[2][2]={{10,20},{11,21}}
}