首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

小疑点请问下!

2012-02-17 
小问题请教下!!#include iostreamusing namespace stdclass Point{public:Point(){xy0coutdefaul

小问题请教下!!
#include <iostream>
using namespace std;
class Point
{
public:
Point()
{
x=y=0;cout<<"default constructor called!"<<endl;
}
Point(int xx,int yy)
{
x=xx;y=yy;cout<<"constructor called!"<<endl;
}
~Point()
{
cout<<"destructor called!"<<endl;
}
int getx(){return x;}
int gety(){return y;}
void move(int a,int b)
{
x=a;y=b;
}
 

private:
int x;
int y;
};

class ArrayOfPoints
{
public:
ArrayOfPoints(int n)
{
numberOfPoint=n;
points=new Point[n];
}
~ArrayOfPoints()
{
cout<<"deleting.."<<endl;
numberOfPoint=0;
delete[]points;
}
Point &Element(int n) //请教下这个这个函数为什么要应用Point类的引用呢
{
return points[n];
}

private:
Point *points;
int numberOfPoint;
};

void main()
{
int number;
cout<<"please enter the neumber of points:";
cin>>number;
ArrayOfPoints pointsArray1(number);
pointsArray1.Element(0).move(5,10);//对于这地方pointsArray1对象调用成员函数Element我明白,但是后面用
pointsArray1.Element(0).move(15,20);//Element调用move不太懂,这个Element是Point类的对象么?
}

[解决办法]
Point *points;//这是你自己定义的。。。一个point类对象数组。。也就是points
Point &Element(int n) //这个引用就是如2楼所说。。。会改变你points的值。。。而points的值又是什么呢。。。就是Point类的对象。。。所以你理解差不多。。。。不过不是一个对象。。而是对象数组。。

热点排行