【发布时间】:2018-12-12 21:12:41
【问题描述】:
类点工作正常,它正在创建 x、y 点。代码:
point.h 文件
#ifndef POINT_H
#define POINT_H
namespace pt
{
class Point
{
int x, y;
public:
Point();
Point(int x, int y);
int getX();
int getY();
};
}
#endif // POINT_H
point.cpp 文件
#include "point.h"
pt::Point::Point()
{
this->x = this->y = 0;
}
pt::Point::Point(int x, int y)
{
this->x=x;
this->y=y;
}
int pt::Point::getX()
{
return this->x;
}
int pt::Point::getY()
{
return this->y;
}
同时,当我尝试在 main 中创建新的 Point3D 类时,它将继承 Point x, y 坐标并添加 z 以创建第三维,新的构造函数无法访问 Point 类的 x, y。错误是: 1. 'int pt::Point::x' 在 Point3D constr 中的第一个和第二个 this-> 是私有的。 2. 两者都“断章取义”
main.cpp
#include <iostream>
#include "point.h"
int main()
{
class Point3D : public pt::Point
{
int z;
public:
getZ()
{
return this->z;
}
Point3D(int x ,int y, int z)
{
this->x=x;
this->y=y;
this->z=z;
}
};
return 0;
}
感谢您的帮助。
【问题讨论】:
-
我把标题弄错了——对不起。我的意思是继承类:(
-
如果您的标题有误,请edit您的帖子进行修正。评论它只是白噪音。
-
派生类无法访问基类的
private成员。他们可以访问protected成员。派生类不是朋友。任何怀有敌意孩子的父母都会明白有必要对某些事情保密,即使是对后代也不例外。从哲学上讲,在软件设计中也是如此 -
@Peter 后裔无法访问您的私人信息,但好朋友可以! -- 已经过时了,但您应得的 :p
-
@Peter 任何有敌对父母的孩子也明白这种需要:)
标签: c++