#include
using namespace std;
class Point
{
public :
Point()
{
X=0;Y=0;
}
Point(int x,int y)
{
X=x;Y=y;
}
void DisPoint()
{
cout<<X<<","<<Y<<endl;
}
private : int X,Y;
};
int main(int argc, const char * argv[]) {
Point p1,p2(2,3),p3(5,5),p4(9,8); //定义对象
Point *parray[4]={&p1,&p2}; //定义对象指针数组,并且初始化parray[0]和parray[1]
parray[2]=&p3; //给parray[2]赋值
parray[3]=&p4; //给parray[3]赋值
for (int i=0; i<4; i++) {
parray[i]->DisPoint(); //调用parray[i]所指向对象的成员函数DisPoint()
}
return 0;
}
输出结果:
0,0
2,3
5,5
9,8
Program ended with exit code: 0
c++对象指针数组的应用
最新推荐文章于 2024-06-16 17:08:41 发布