派生类错误

Derived Classes Error

本文关键字:错误 派生      更新时间:2023-10-16

我使用派生类完成了一个小程序,它编译正确,但输出错误。

该程序使用有序对(x,y)作为圆心。然后,它使用给定的圆心和半径来确定圆的面积和周长。

使用pointType类输出有序对可以很好地工作,但当我测试circleType类时,我希望得到(0,0)作为默认值。相反,我得到了(2293512293700)

任何帮助都将不胜感激!

这是点类代码:

#ifndef POINTTYPE_H_INCLUDED
#define POINTTYPE_H_INCLUDED
#include <iostream>
class pointType{
 public:
  pointType (int=0, int=0);
  int getX() const;
  int getY() const;
  void setX(int);
  void setY(int);
  void setValues(int, int);
  friend pointType operator + (pointType, pointType);
  friend pointType operator - (pointType, pointType);
  friend std::ostream& operator << (std::ostream&, pointType);
 private:
  int x;
  int y;
};
#endif // POINTTYPE_H_INCLUDED

以下是点类实现:

#include "pointType.h"
pointType::pointType (int X, int Y) : x(X), y(Y) {}
int pointType::getX () const {
 return x;
}
int pointType::getY () const {
 return y;
}
void pointType::setX (int new_x) {
 x = new_x;
}
void pointType::setY (int new_y) {
 y = new_y;
}
void pointType::setValues (int new_x, int new_y) {
 x = new_x;
 y = new_y;
}
pointType operator + (pointType lh, pointType rh){
 pointType answer;
 answer.x = lh.x + rh.x;
 answer.y = lh.y + rh.y;
 return answer;
}
pointType operator - (pointType lh, pointType rh){
 pointType answer;
 answer.x = lh.x - rh.x;
 answer.y = lh.y - rh.y;
 return answer;
}
std::ostream& operator << (std::ostream& out, pointType c){
 out << "(" << c.x << ", " << c.y << ")";
 return out;
}

这是圆圈类:

#ifndef CIRCLETYPE_H_INCLUDED
#define CIRCLETYPE_H_INCLUDED
#include "pointType.h"
#include <iostream>
class circleType: protected pointType {
 public:
  circleType (float=0);
  circleType (int=0, int=0);
  void setRadius (float);
  float calculateArea (float);
  float calculateCircumference (float);
  friend std::ostream& operator << (std::ostream&, circleType);
 private:
  float radius;
  int center_x;
  int center_y;
};
#endif // CIRCLETYPE_H_INCLUDED

这是circle类的实现:

#include "pointType.h"
#include "circleType.h"
#include <math.h>
const float PI = 3.14;
circleType::circleType(float R): radius(R) {}
circleType::circleType(int center_X, int center_Y):
 pointType(center_x, center_y) {}
void circleType::setRadius(float new_radius) {
 radius = new_radius;
}
float circleType::calculateArea(float radius) {
 float area;
 area = PI * pow(radius, 2);
 return area;
}
float circleType::calculateCircumference(float radius) {
 float circumference;
 circumference = PI * (radius * 2);
 return circumference;
}
std::ostream& operator << (std::ostream& odata, circleType f) {
 odata << "(" << f.center_x << ", " << f.center_y << ")";
 return odata;
}

这是测试代码:

#include "pointType.h"
#include "circleType.h"
#include <iostream>
#include <math.h>
using namespace std;
int main() {
  pointType c, d(8, 9);
  circleType f(4, 5);
  cout << c << endl;
  cout << d << endl;
  c.setValues(12, 3);
  cout << c << endl;
  cout << c + d << endl;
  cout << c - d << endl;
  cout << f << endl;
  return 0;
}

据我所见,circleType int成员center_x和center_y从未设置。构造函数CircleType::CircleType(int=0,int=0)调用基构造函数,而基构造函数又设置了pointType.x和pointType.y,这两个成员都是基类的私有成员(因此CircleType不可用)。

我的建议(或者我认为你想要的):让int x和int y受到保护,而不是私有的,删除center_x和center_y,然后打印odata<lt;("<<f.x<","<<f.y<)";

这是一个拼写错误。

circleType::circleType(int center_X, int center_Y):
pointType(center_x, center_y) {}

请注意center_X(大写)和center_X之间的区别。您将未初始化的成员传递给父构造函数,该构造函数可以在堆栈中具有任何延迟值。

因此,经常用m_(例如:m_centerX)指导您的成员是一种很好的做法。(在这种特殊情况下,甚至没有必要在子类中再次使用它们,冗余是不好的!)

编辑:

很抱歉造成混淆,但下次请说得更准确一点,本可以为我们节省很多时间。我假设你的问题发生在用圆圈添加点时,在我自己运行代码后,我注意到它发生在输出f时。原因与上面解释的相同:你没有初始化你的成员!它们只是保存当前位于对象构建的内存位置的任何值。

在circleType类中,center_x和center_y没有初始化。参数被直接传递给pointType构造函数并设置私有成员。您可以通过更改运算符<lt;接受circleType参数的函数:

std::ostream& operator << (std::ostream& odata, circleType f) {
    odata << "(" << f.center_x << ", " << f.center_y << ")" << std::endl;
    odata << "(" << f.getX() << ", " << f.getY() << ")"; //access superclass
    return odata;
}

对于受保护的继承,您也不能使用为基类定义的+和-重载,因为它不会编译。