一个关于派生类的问题,大家看一下
写了个程序,派生类对象不能访问派生类数据成员????const double Pi=3.141593;
#include<iostream.h>
#include<afx.h>
#include<math.h>
class Shape
{public:
int X_axis;
int Y_axis;
Shape(int axis1,int axis2)
{
X_axis=axis1;
Y_axis=axis2;
}
};
class Circle:public Shape
{//public:
int m_nRadii;
public:
Circle::Circle(int axis,int axis1,int axis2):Shape(axis1,axis2)
{
m_nRadii=axis;
}
double Area(int abscissa,int ordinate,int radii);
};
double Circle::Area(int abscissa,int ordinate,int radii)
{
double area=Pi*radii*radii;
return area;
}
class Square:public Shape
{//public:
int m_sXaxis;
int m_sYaxis;
public:
Square::Square(int axis1,int axis2,int axis3,int axis4):Shape(axis3,axis4)
{
m_sXaxis=axis1;
m_sYaxis=axis2;
}
double Area(int axis1,int axis2,int axis3,int axis4);
};
double Square::Area(int axis1,int axis2,int axis3,int axis4)
{
int length,width;
length=2*(axis3-axis1);
width=2*(axis4-axis2);
double area=fabs(length)*fabs(width);
return area;
}
void main()
{
Circle circle(2,3,5);
Square square(2,3,1,5);
double area1=circle.Area(circle.X_axis,circle.Y_axis,circle.m_nRadii);
cout<<"圆面积AREA="<<area1<<endl;
double area2=square.Area(square.m_sXaxis,square.m_sYaxis,square.X_axis,square.Y_axis);
cout<<"长方形面积AREA="<<area2<<endl;
}
其中,square,circle分别为基类Shape的派生类Square,Circle的对象,但调用函数circle.Area 和square.Area 时,错误提示为
cannot access private member declared in class 'Circle';
cannot access private member declared in class 'Square';
cannot access private member declared in class 'Square'
把m_nRadii,m_sXaxis,m_sYaxis三个改为"protected"型的也是同样的问题,只有是public型的才可以...
难道派生类对象不能访问其自身派生类的私有或保护型数据成员???
有谁帮我解释一下!!!