C++语言程序设计

| 程序段11-1

class twoD
{
protected:
double x, y; // x和y坐标
public:
twoD(double i, double j):x(i), y(j){}
//内联构造函数
//内联成员函数
void setX(double NewX){x = NewX;}
void setY(double NewY){y = NewY;}
double getX() const {return x;}
double getY() const {return y;}
};

假定后来又要处理3维空间点的情形，一个直接的方法是再定义一个3维空间点类threeD：

| 程序段11-2

class threeD
{
protected:
double x, y, z; // x 、y和z坐标
public:
twoD(double i, double j, double k):x(i), y(j) , z(k){}
//内联构造函数
//内联成员函数
void setX(double NewX){x = NewX;}
void setY(double NewY){y = NewY;}
void setZ(double NewZ){z = NewZ;}
double getX() const {return x;}
double getY() const {return y;}
double getZ() const {return z;}
};