mardi 28 mars 2017

Hiding concrete nested types

I am trying to hide the following types from the end user.

//concrete.hpp
#include <iostream>
#include <vector>
using namespace std;

class Input
{
    public:
        int id;
        string note;
        Input():id(0),note("")  {}

        void printInput()
        {
            cout<<"Input id : "  << id <<endl;
            cout<<"Note :" << note <<endl;
        }

        void set(std::string &update,int id)
        {
            note = update;
            this->id = id;
        }

};

class Event
{
    public:
        int event;
        vector<Input> ins;

        Event():event(0)
        {

        }

        void printEvent()
        {
            cout<< "Event ID " << event <<endl;;
        }

        void addInput(Input &newInput)
        {
            ins.push_back(newInput);
        }

        Input& getInputAt(int index)
        {
            //ignore range check
            return ins[index];
        }
};

So my interface which certainly falls short is :

#include "concrete.hpp"

class InputI
{
public:
    virtual void printInput() =0;
    virtual void set(std::string &update,int id)=0;
};


class EventI
{
public:    
    virtual void printEvent() =0;
    virtual void addInput(InputI &newInput) =0;
    virtual InputI& getInputAt(int index)=0;
};


class InputImpl: public InputI , private Input
{   
    public:
    void printInput()
    {
        Input::printInput();
    }

    void set(std::string &update,int id)
    {
        Input::set(update,id);
    }
};

/*
class EventImpl: public EventI
{
public:
    void printEvent()
    {
        e.printEvent();
    }

    void addInput(InputI * newInput)
    {


    }

   InputI& getInputAt(int index)=0;    
private:
    Event e;    
};
*/




int main()
{
    std::string val("IN1");    
    InputI *inp = new InputImpl();
    inp->set(val,10);
    inp->printInput();
    inp->set(val,20);
    inp->printInput();
    return 0;
}

The problem here is i am unable to figure out how the EventImpl should be designed. Can any one point me in the right direction please.

Aucun commentaire:

Enregistrer un commentaire