可以定义无参的构造函数。
#include<iostream>
using namespace std;
class Point{
private:
double x;
double y;
public:
Point(){
x=0;
y=0;
}
Point(double a,double b){set(a,b);}
void set(double a,double b){
x=a;
y=b;
}
double getx(){return x;}
double gety(){return y;}
};
int main(){
Point p;
Point p1(1.9,4.0);
Point *p2;
p2=new Point;
p2->set(1,2);
cout<<p.getx()<<"\t"<<p.gety()<<endl;
cout<<p1.getx()<<"\t"<<p1.gety()<<endl;
cout<<p2->getx()<<"\t"<<p2->gety()<<endl;
}
自我反思:指针需要先用new分配一定的内存空间,才能继续进行运行,否则的话就会运行出错,这个需要注意。