#include<iostream>
using namespace std;
class Point
{
private:
int x;
int y;
public:
Point(int a, int b)
{
x = a;
y = b;
}
Point(Point &a_point)
{
x = a_point.x;
y = a_point.y;
}
~Point()
{
cout << "Deconstructed Point";
print();
}
void print()
{
cout << "(" << x << "," << y << ")" << endl;
}
};
int main()
{
Point b_point(0,0);
b_point.print();
int a, b;
cin >> a >> b;
Point c_point(a, b);
c_point.print();
return 0;
}
//运行结果如下
(0,0)
5 6 //注意此处为输入
(5,6)
Deconstructed Point(5,6)
Deconstructed Point(0,0)
-
Point(Point &a_point)
:定义了一个拷贝构造函数,接受一个Point
类型的引用参数a_point
,用于将一个已有的Point
对象的数据拷贝到新创建的对象中。 -
在程序运行结束时,会先销毁
c_point
对象,然后销毁b_point
对象,因此先输出Deconstructed Point(5,6)
,再输出Deconstructed Point(0,0)
。