C++ 无参构造函数

设计表示平面坐标位置的点类,可以修改和获取点的x、y坐标值,设置构造函数对点的数据成员进行初始化,并且能够用数组保存一系列的点。

#include
using namespace std;
class Point {
private:
	int x, y;
public:
	Point(int a, int b) { setPoint(a, b); }
	int getx() { return x; }
	int gety() { return y; }
	Point() { x = 0; y = 0; }//显式定义无参构造函数
	void setPoint(int a, int b) { x = a; y = b; }
};
Point p0;
Point p1(1, 1);//调用构造函数Point(int,int)
void main() {
	static Point p2;
	Point p3;
	Point a[10];
	Point* p4;
	p4 = new Point;
	p4->setPoint(8, 9);
	cout << "p0:" << p0.getx() << "," << p0.gety() << endl;
	cout << "p1:" << p1.getx() << "," << p1.gety() << endl;
	cout << "p2:" << p2.getx() << "," << p2.gety() << endl;
	cout << "p3:" << p3.getx() << "," << p3.gety() << endl;
	cout << "p4:" << p4->getx() << "," << p4->gety() << endl;
	cout << "a[0]:" << a[0].getx() << "," << a[0].gety() << endl;
}

你可能感兴趣的:(C++)