I'm trying to code a class to handle joystick input (irrelevant), and being rusty on inheritance and new to c++ I'm having some confusion while trying to create a subclass of my joystick class. Here's my code
//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
And here's its definition
//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() {
}
Now I'm trying to extend this with a JoystickOne class, as its behavior is slightly different. To this end I created a JoystickOne.h and a 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
And the .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)) {
}
}
My problem is that I'm not quite sure what's extraneous in the JoystickOne class. I come from Java, so I'm used to being able to just extend a superclass and automatically use all of its methods and members. I'm confused because of C++'s seperation into .h and .cpp files; from what I've learned by messing around I have to declare all variables and methods I wish to use, even if they're members of the superclass. I don't think I have to define method buttonClicked() twice, although I don't have a robot so I can't actually test that now.
Basically, I'm asking what I can cut out from the definition of the JoystickOne class, and how to do it. If any of you have advice on some good OOP practices in C++ feel free to share, or maybe even clear up some java-isms that I have.
Thanks!