fryme
fryme

Reputation: 321

Implementation of interface

I have an interface and some implementations. But in one implementation i have a specify functionality using only in that implementation.

class Interface
{
    virtual void foo() = 0;
}

class D1 : public Interface
{
    void foo() {}
}

class D2 : public Interface
{
    void foo() {}
    void bar() {}
}

So i have a D2::bar() function only in D2, and it specified only for D2 implementation. What is the right way using OOP to write that kind of stuff?

In my client code i have a call: Interface* i; i->foo();

But if it is D2 in "i" i need to call bar() function in some cases.

Upvotes: 0

Views: 137

Answers (5)

Lwin Htoo Ko
Lwin Htoo Ko

Reputation: 2406

class _interface
{
    virtual void foo() = 0;
};

class _abstract : public _interface
{
public:
    _abstract(){}
    virtual ~_abstract(){};

    virtual void foo() = 0;
    int get_type()
    {
        return i_type;
    }

protected:
    int i_type;
};

class D1 : public _abstract
{
public:
    D1(){
        i_type = 1;
    }
    ~D1(){}

    void foo() {
        // do something
    }
};

class D2 : public _abstract
{
public:
    D2(){
        i_type = 2;
    }
    ~D2(){}

    void foo() {
        // do something
    }

    void bar() {
        // do something
    }
};
int main()
{
    D1 d_one;
    D2 d_two;

    _abstract* ab = &d_one;
    cout << ab->get_type() << "D1" << endl;

    ab = &d_two;
    cout << ab->get_type() << "D2" << endl;

    return 0;
}

You can identify which child by get_type(). So, you know what time you can use bar(). I don't what is the best approach.

Upvotes: 0

Wes
Wes

Reputation: 296

If you are prepared to have some generic implementation in your interface you put an empty implementation of bar() in your interface:

class Interface
{
    virtual void foo() = 0;
    virtual void bar() {}
}

class D1 : public Interface
{
    void foo() {}
}

class D2 : public Interface
{
    void foo() {}
    void bar() {}
}

Now when you call Interface i* = blah; i->bar(); if i is a D1 it does nothing, if i is a D2 it will do something D2 specific.

Upvotes: 0

Jeeva
Jeeva

Reputation: 4663

Assuming that you inherit D1 and D2 from Interface. you can use cast to convert base pointer to derived object and use it, provided the base pointer points to D2

Upvotes: 0

Frerich Raabe
Frerich Raabe

Reputation: 94289

If you insist on using interfaces, you should move bar into a dedicated interface and then let clients use that:

class FooInterface {
public:
    virtual void foo() = 0;
};

class BarInterface {
public:
    virtual void bar() = 0;
};

class D1 : public FooInterface {
public:
    void foo() {}
};

class D2 : public FooInterface,
           public BarInterface {
public:
    void foo() {}
    void bar() {}
};

Your client code needing a bar implementation could then take a BarInterface.

Upvotes: 0

njzk2
njzk2

Reputation: 39386

If you need to call the bar function, you need to have a reference to an object that knows about bar.

So, either you reference a D2 object or your Interface must include a bar function. In the later case, your D1 must implement it as well, but the implementation can be empty or return an error value.

Upvotes: 1

Related Questions