How to extend and use members of a superclass in c++?

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!

Answers


You should mark methods that can be overridden as virtual in your base class. To override it in a derived class, simply re-define it in your derived class.

Example:

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; }
};

Now if you instantiate:

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

As for member variables. Private members defined in your base will class will NOT be available in your derived class. On the other hand, protected and public member variables will be accessible.


Need Your Help

Animating text-shadow to fade in and out

javascript jquery animation jquery-animate

I'm trying to animate text-shadow to fade in and out using the snippet shared here:

Instanciate a class inside a class NullPointerException

java class methods nullpointerexception

I'm getting a nullPointerException error in the line playlist1.firstSong = song; below (9th line). Any ideas?

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.