如何正确访问c++中继承的方法和构造函数
How do I properly access inherited methods and constructors in c++?
我有一个Sphere类,它继承了中心的Point对象。当我通过Sphere构造函数创建Sphere对象时,它总是将中心初始化为0,0,0,但它会正确地设置半径。
访问Sphere的setCenter()方法也没有影响。我能有效地更改球体中心点的X、Y、Z坐标的唯一方法是调用该点的setX()等方法。
如果这是一个显而易见的答案,我很抱歉,但我是C++的新手,正在为过渡而挣扎。如果我遗漏了任何重要信息,请毫不犹豫地告诉我。以下是相关代码:
主要
#include <iostream>
#include <fstream>
#include "MovingSphere.h"
using namespace std;
int main() {
ifstream inFile("sphere.txt");
int X, Y, Z, R, DX, DY, DZ;
if (inFile) {
inFile >> X >> Y >> Z >> R
>> DX >> DY >> DZ;
}
else {
cerr << "neRR: Cannot find input file.nn";
}
// create a sphere
Sphere sphereInstance(X, Y, Z, R);
sphereInstance.setCenter(1, 2, 3);
sphereInstance.setX(3);
cout << endl <<
"X = " << sphereInstance.getX() << endl <<
"Y = " << sphereInstance.getY() << endl <<
"Z = " << sphereInstance.getZ() << endl;
return 0;
}
点.cpp
#include "Point.h"
Point::Point() : x(0), y(0), z(0) { // default constructor (point at 0,0,0)
}
Point::Point(int X, int Y) : x(), y(), z(0) { // constructor for 2d point
}
Point::Point(int X, int Y, int Z) : x(), y(), z() { // constructor for 3d point
}
// set the points X coordinate (mutator)
void Point::setX(int newX) {
x = newX;
}
... etc.
点.h
#ifndef POINT_H
#define POINT_H
class Point {
public:
Point (); // default constructor (0,0,0);
Point(int X, int Y); // constructor for 2d point
Point(int X, int Y, int Z); // constructor for 3d point
void setX(int newX); // declaration
int getX() const; // declaration
etc...
private:
int x, y, z; // coordinates of point
};
#endif /* POINT_H */
Sphere.cpp
#define _USE_MATH_DEFINES
#include <math.h>
#include "Sphere.h"
Sphere::Sphere() :
center(), radius(0) {// default constructor (a point at 0,0,0)
}
Sphere::Sphere(int X, int Y, int Z, int R) { // sphere constructor
center = Point(X, Y, Z);
radius = R;
}
// set the sphere's center (mutator)
void Sphere::setCenter(int X, int Y, int Z) {
center.setX(X);
center.setY(Y);
center.setZ(Z);
}
... etc.
Sphere.h
#ifndef SPHERE_H
#define SPHERE_H
#include "Point.h"
class Sphere: public Point {
public:
Sphere(); // default constructor (a point at 0,0,0)
Sphere (int X, int Y, int Z, int R); // sphere constructor
void setRadius(int newRadius); // declaration
void setCenter(int X, int Y, int Z); // declaration
int getRadius() const; // declaration
private:
Point center; // center of sphere
int radius; // radius of sphere
};
#endif /* SPHERE_H */
输出
X = 3
Y = 0
Z = 0
class Sphere: public Point {
这意味着球体是一个点,并从一个点所拥有的一切开始,包括X、Y和Z坐标。
private:
Point center; // center of sphere
这意味着球体有一个点,称为center
。球体的这个点也有一个X、Y和Z坐标,就像所有的点一样
因此,球体既是一个点,又有一个点。每个点都有一个X、Y和Z坐标。这不可能是你想要的,当你的代码设置了这两点中的一个,然后得到另一个时,它就会失败。。选择一个型号并坚持下去。
如果你需要像对待点一样对待球体,那么去掉center
——在这个模型中,球体是一个也有半径的点。如果你不需要像对待点一样对待球体,那么不要从点继承——在这个模型中,球体不是一个点,而是有一个点和一个半径。
您的Point
2和3参数构造函数不处理输入。将其更改为
Point::Point(int X, int Y) : x(X), y(Y), z(0) { }
Point::Point(int X, int Y, int Z) : x(X), y(X), z(Z) { }
如果center
是Sphere
的Point
数据成员,那么您应该更喜欢构造函数初始化列表中的初始化,而不是构造函数主体中的赋值:
Sphere::Sphere(int X, int Y, int Z, int R) : center(X,Y,Z), radius(R) { }
我在语句中编辑了一个冒号,但至少需要6个字符。
您没有显示Point
和Sphere
是如何关联的,但我猜Sphere
继承自Point
。。。
class Point
{
...
};
class Sphere : public Point
{
...
};
如果你想调用例如基本构造函数,你可以在初始化器列表中调用:
Sphere::Sphere(int X, int Y, int Z, int R)
: Point(X, Y, Z), radius(R)
{
}
至于其他函数,如果它们在基类中,则可以将它们当作子类的成员来使用:
void Sphere::setCenter(int X, int Y, int Z)
{
setX(X);
setY(Y);
setZ(Z);
}
由于Sphere
也是Point
(由于继承),因此不需要center
成员变量。
正如其他人所说,您应该首先明确Sphere与Point的关系(要么Sphere有一个Point,即成员,要么Sphere是一个Point(即继承))。
然而,你的确切问题是:
- Sphere的setCenter()方法更新Sphere::center
- Sphere使用Point的getX/Y/Z(),这些使用Point中的x/Y/Z
也就是说,setCenter()更新与getX/Y/Z()无关的内容。
- 如果C++类在类方法中具有动态分配,但没有构造函数/析构函数或任何非静态成员,那么它仍然是POD类型吗
- 初始化具有非默认构造函数的std::数组项的更好方法
- 获取从C++中同一类中的构造函数调用的方法返回的值
- 有没有一种代码密度较低的方法来使用非默认构造函数初始化数组?
- C++方法是否可以根据传递给构造函数的参数具有不同的返回类型?
- C++:将向量传递到构造函数以创建成员变量的最佳方法?
- 在C++中使用默认构造函数初始化对象的不同方法
- 使用默认构造函数初始化对象的不同方法
- 静态 std::map instatiation 在类的方法中调用构造函数吗?
- 定义类模板构造函数的两种方法之间的区别
- 通过构造函数方法输出的类到类类型转换是 5500 为什么不是 5555
- 用相同的参数声明两个构造函数的最偶像化的方法是什么?
- 构造函数是否有一种现代C++方法来了解其'container'类?
- 使用回调函数从构造函数调用虚拟/派生方法的替代方法?
- 如何构造一个类型特征,可以判断一个类型的私有方法是否可以在另一个类型的构造函数中调用?
- 不为 emplace() 定义构造函数的解决方法
- 有没有一种简单的方法可以在对象向量上调用构造函数?
- 删除复制构造函数的 Intel 13.1.2 中不良C++行为的解决方法
- 方法/构造函数及其返回值
- 目标C类方法==C++构造函数