6_18.cpp

来自「C++多个例题的源代码及分析.有兴趣的可以」· C++ 代码 · 共 44 行

CPP
44
字号
//6_18.cpp
#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 x,int y)
		{  X=x;  Y=y;   }
  private:
       int  X,Y;
};
class ArrayOfPoints
{
   public:
     ArrayOfPoints(int n)
     {   numberOfPoints=n;  points=new Point[n];  }
     ~ArrayOfPoints()
     {   cout<<"Deleting..."<<endl;
         numberOfPoints=0;  delete[] points;     
       }
     Point& Element(int n)
     {  return points[n];  }
   private:
     Point *points;
     int numberOfPoints;
};

void main()
{
	int number;
	cout<<"Please enter the number of points:";
	cin>>number;
     ArrayOfPoints points(number);    //创建对象数组
     points.Element(0).Move(5,10);     //通过指针访问数组元素的成员
     points.Element(1).Move(15,20);   //通过指针访问数组元素的成员
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?