C++语言程序设计

下面是一个可以编译和运行的完整的程序：

| 例11-1

// 文件名：Points.cpp
// 功能：演示继承的思想
//
#include <iostream.h>
class twoD
{
protected:
double 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;}
};

class threeD:public twoD
{
private:
double z;
public:
threeD(double i, double j, double k):twoD(i,j){z = k;}
void setZ(double NewZ){z = NewZ;}
double getZ() {return z;}
};
void main()
{
twoD Obj2(1, 1);
threeD Obj3(1, 2, 3);
cout << "Coordinates for 3d object is: " <<endl;
cout << Obj3.getX() << ", " << Obj3.getY() <<", " <<
Obj3.getZ()<< endl;
}

运行结果如下：
Coordinates for 3d object is:
1, 2, 3