c++的继承问题

来源:百度知道 编辑:UC知道 时间:2024/05/21 01:29:27
//构造一个动态点的数组
#include<iostream>
using namespace std;
class Point//点类
{
protected:
double x;
double y;
public:
Point();
Point(double ,double);
Point(Point &);
double getx()
{return x;
}
double gety()
{
return y;
}
~Point();
};
Point::Point(void)
{
x=0;
y=0;
}
Point::Point(double x1,double y1)
{
x=x1;
y=y1;
}
Point::Point(Point &p)
{
x=p.x;
y=p.y;
}

Point::~Point(void)
{
}
class Array ://动态数组的类 公有继承了点类
public Point
{
private:
int num;
Point *points;
public:
Array(void){num=0;points=NULL;}
Array(int);
void show_array();
public:
~Array(void){};
};
Array::Array(int n)
{
points=new Point[n];
num=n;
}
void Array::show_array()
{
for(int i=0;i<num;i++)
{

x,y 是Point类的Protected成员变量。

虽然Array继承自Point类。

但是,你的Array类中的points还是Point*类型,
points[i]是Point*类型的实例。

而对于Point类来说,外部不能直接访问Protected成员变量。

Array类自己的x,y变量(从Point继承)才可以在Array类中直接访问。
例如:this->x

Array类不能访问基类的私有成员,在类外基类的对象也不能访问私有成员...

你的程序有错。。执行不出来。。。自己再出查查书吧