构造函数不能重载,所以用简单的方法吧。
#include <iostream.h>
class point{
float x,y,z;
public:
point(float x=0,float y=0,float z=0){
this->x=x;
this->y=y;
this->z=z;
}
~point(){};
float getx(){return x;}
float gety(){return y;}
float getz(){return z;}
void
setx(float t){x=t;}
void
sety(float t){y=t;}
void
setz(float t){z=t;}
};
int main(int argc, char *argv[])
{
point A(5,10),B(5,10,12);
cout<<"二维点操作:\nA=("<<A.getx()<<","<<A.gety()<<")\n";
cout<<"二维点操作:\nA=("<<A.getx()<<","<<A.gety()<<","<<A.getz()<<")\n";
cout<<"三维点操作:\nA=("<<B.getx()<<","<<B.gety()<<","<<B.getz()<<")\n";
return 0;
}
执行结果:
E:\C++>project1.exe
二维点操作:
A=(5,10)
二维点操作:
A=(5,10,0)
三维点操作:
A=(5,10,12)