0

Coming from a background of PHP and Java I want to do things in C++ which are not as easy as I had hoped.

Let's say I have a header file (which I see as an Interface) containing the class Sensor

I have multiple types of sensors but they all have the basic functionality such as

  • initialize
  • read
  • write

However, they are all implemented in a different way for each sensor.

In PHP I would be able to use IoC and dependency injection to bind a specific implementation to an interface.

How can I extend the "interface"/header with the specific implementation for each sensor?

I imagine having a file structure like so: sensor.h sensor1.cpp sensor2.cpp

Where the .cpp files are all different implementations for the Sensor interface.

My compiler is compatible with C++11 and I prefer to do things the "new" way.

Ortix92
  • 2,648
  • 1
  • 27
  • 57
  • Sorry, but I have to vote to close as being too broad--there are just too many possibilities here, and choosing the right one for a particular situation depends on lots of factors you haven't discussed in the question (and by the time you discussed them, and gave answers for each, you'd have a decent-sized book). A couple obvious possibilities would be inheritance and strategy pattern, but there are many others. – Jerry Coffin Apr 19 '16 at 16:36

1 Answers1

1

Typically, in C++, declaration of a class and its implementation go in two different files: header files (.h) typically contains only declarations while .cpp files contains implementation. For more details on this separation see this post: Separating class code into a header and cpp file.

In addition, in order to have different implementations of the same interface the simplest approach is using inheritance. Below a simple example using it.

#include <vector>
#include <iostream>

// This could be in SensorInterface.h
class SensorInterface
{
    public:

        virtual void initialize() = 0;
        virtual void read() = 0;
        virtual void write() = 0;
};

// This could be in SensorImplementation1.h (for definitions) and     SensorImplementation1.cpp (for implementation)
class SensorImplementation1 : public SensorInterface
{
    public:
            virtual void initialize()
    {
        std::cout << "SensorImplementation1::initialize" << std::endl;
        // Here implementation
    }
            virtual void read()
    {
        std::cout << "SensorImplementation1::read" << std::endl;
        // Here implementation
    }
            virtual void write()
    {
        std::cout << "SensorImplementation1::write" << std::endl;
        // Here implementation
    }
};

// This could be in SensorImplementation1.h (for definitions) and SensorImplementation1.cpp (for implementation)
class SensorImplementation2 : public SensorInterface
{
    public:
            virtual void initialize() 
            {
        std::cout << "SensorImplementation2::initialize" << std::endl;
                    // Here implementation
            }
            virtual void read() 
            {
        std::cout << "SensorImplementation2::read" << std::endl;
                    // Here implementation
            }
            virtual void write() 
            {
        std::cout << "SensorImplementation2::write" << std::endl;
                    // Here implementation
            }
};

int main(int argc, char **argv)
{
    std::vector<SensorInterface *> sensors;

    SensorImplementation1 sensor1;
    SensorImplementation2 sensor2;

    sensors.push_back(&sensor1);
    sensors.push_back(&sensor2);

    for(unsigned int i = 0; i < sensors.size(); i++)
    {
        sensors.at(i)->initialize();
        sensors.at(i)->read();
        sensors.at(i)->write();
    }

    return 0;

}
Community
  • 1
  • 1
Francesco Argese
  • 568
  • 3
  • 11