Chief Delphi

Chief Delphi (http://www.chiefdelphi.com/forums/index.php)
-   C/C++ (http://www.chiefdelphi.com/forums/forumdisplay.php?f=183)
-   -   Custom Classes? (http://www.chiefdelphi.com/forums/showthread.php?t=109471)

JPruim 10-11-2012 19:42

Custom Classes?
 
How would I go about creating a custom class? For example, I want a class that has a digitalInput encapsulated within it. In simple terms, How would I convert my existing LimitSwitch class (https://github.com/itguy51/FRC4014-2...mitSwitch.java) into C++? I have never actually used C++ in Object-Oriented programming other than simple things like in Visual Studio for demonstrations. I guessed how, and got this:
Code:

class limitSwitch
{
        DigitalInput di;
        limitSwitch::limitSwitch(UINT32 port){
                di(port);
        }
        bool limitSwitch::getState(){
                return di.Get() == 1;
        }
};

which is FULL of errors, there are more lines in the error log than there are in the cpp file. I get "Error: Expected ')' before "port"" and am confused because that makes no sense. I want to pass an argument to the constructor so that it assigns the DigitalInput to the initialized port.
getState is supposed to return true/false based on the state of the switch.

virtuald 14-11-2012 10:04

Re: Custom Classes?
 
Two ways of doing it. One is totally inline as you're doing it (which isn't really the C++ way of doing it).

Code:

#include <wpilib.h>

class LimitSwitch
{
public:

    LimitSwitch(UINT32 port) :
        di(port)
    {}
   
    bool getState()
    {
        return di.Get() != 0;
    }

private:
    DigitalInput di;
};

Or split into cpp/h files. Here's LimitSwitch.h:

Code:

#ifndef __LIMITSWITCH_H
#define __LIMITSWITCH_H

class LimitSwitch
{
public:

    LimitSwitch(UINT32 port);
   
    bool getState();

private:
    DigitalInput di;
};
#endif

And LimitSwitch.cpp:

Code:

#include <wpilib.h>
#include "LimitSwitch.h"

LimitSwitch::LimitSwitch(UINT32 port) :
    di(port)
{}

bool LimitSwitch::getState()
{
    return di.Get() != 0;
}



All times are GMT -5. The time now is 18:18.

Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi