gpt4 book ai didi

c++ - 如何在 C++ 中扩展和使用父类(super class)的成员?

转载 作者:太空宇宙 更新时间:2023-11-04 15:22:08 24 4
gpt4 key购买 nike

我正在尝试编写一个类来处理操纵杆输入(无关紧要),并且对继承生疏并且是 c++ 的新手我在尝试创建操纵杆类的子类时遇到了一些困惑。这是我的代码

//superclass's .h
#ifndef JOYSTICKINPUT_H
#define JOYSTICKINPUT_H

#include "WPILib.h"

class JoystickInput {
public:
JoystickInput(Joystick*);
Joystick * joystick;
Victor * myVictor [3];
bool buttons [10];
bool buttonClicked(int id);
void testForActions();
};
#endif

这是它的定义

//superclass's .cpp
#include "JoystickInput.h"

JoystickInput::JoystickInput(Joystick * joy) {
joystick = joy;
for (int x = 0; x < 10; x++) {
buttons[x] = false;
}
}

bool JoystickInput::buttonClicked(int id) {
if (buttons[id] == false and joystick->GetRawButton(id) == true) {
buttons[id] = true;
return true;
} else if (buttons[id] == true and joystick->GetRawButton(id) == false) {
buttons[id] = false;
return false;
} else {
return false;
}
}

void JoystickInput::testForActions() {
}

现在我试图用 JoystickOne 类扩展它,因为它的行为略有不同。为此我创建了一个 JoystickOne.h 和一个 JoystickOne.cpp

//Joystickone.h
#ifndef JOYSTICKONE_H
#define JOYSTICKONE_H
#include "WPILib.h"
#include "JoystickInput.h"

class JoystickOne : public JoystickInput {
public:
JoystickOne(Joystick*);
Joystick * joystick;
Victor * myVictor;
bool buttons [10];
bool buttonClicked(int id);
void testForActions();
};
#endif

还有.cpp

    #include "JoystickOne.h"
#include "WPILib.h"

JoystickOne::JoystickOne(Joystick * joy) : JoystickInput(joy) {
//joystick = joy;
//myVictor = new Victor(1);
/*for (int x = 0; x < 10; x++) {
buttons[x] = false;
}*/
}

bool JoystickOne::buttonClicked(int id) {
if (buttons[id] == false and joystick->GetRawButton(id) == true) {
buttons[id] = true;
return true;
} else if (buttons[id] == true and joystick->GetRawButton(id) == false) {
buttons[id] = false;
return false;
} else {
return false;
}
}

void JoystickOne::testForActions() {
if (buttonClicked(1)) {
}
if (buttonClicked(2)) {
}
if (buttonClicked(3)) {
//myVictor->Set(.3);
}
if (buttonClicked(4)) {
}
}

我的问题是我不太确定 JoystickOne 类中有什么无关的东西。我来自 Java,所以我习惯于扩展一个父类(super class)并自动使用它的所有方法和成员。我很困惑,因为 C++ 分为 .h 和 .cpp 文件;根据我通过乱搞所学到的知识,我必须声明我希望使用的所有变量和方法,即使它们是父类(super class)的成员。我不认为我必须定义方法 buttonClicked() 两次,虽然我没有机器人所以我现在不能实际测试它。

基本上,我是在问我可以从 JoystickOne 类的定义中删除什么,以及如何删除。如果你们中的任何人对 C++ 中的一些良好 OOP 实践有建议,请随时分享,或者甚至可以清除我的一些 java 主义。

谢谢!

最佳答案

您应该在基类中将可以重写的方法标记为 virtual。要在派生类中覆盖它,只需在派生类中重新定义它即可。

例子:

class Base{
public:
virtual void overrideThis() { std::cout << "Base" << std::end; }
void test() { std::cout << "Base::test()" << std::endl; }
};

class Derived : public Base{
public:
void overrideThis() { std::cout << "Derived" << std::endl; }
};

现在如果你实例化:

Derived d;
d.overrideThis(); // Will print 'Derived'
d.test(); // Will print 'Base::test()'

至于成员变量。在您的基类中定义的私有(private)成员将在您的派生类中不可用。另一方面,protected 和 public 成员变量将是可访问的。

关于c++ - 如何在 C++ 中扩展和使用父类(super class)的成员?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16326144/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com