CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Jan 2016
    Posts
    1

    hierarchy of abstract interfaces implementation

    Hello,

    I have a hierarchy of abstract interfaces to be used by an other api. For exemple:
    //*******************
    #include <iostream>
    using namespace std;

    class AnimalInterface
    {
    public:
    virtual ~AnimalInterface() = 0;
    virtual void eat () = 0;
    };

    AnimalInterface::~AnimalInterface()
    {}

    class BirdInterface : public AnimalInterface
    {
    public:
    virtual ~BirdInterface() = 0;
    virtual void fly() = 0;
    };

    BirdInterface::~BirdInterface()
    {
    }

    class Animal : public AnimalInterface
    {
    public:
    void eat () { cout << "miam miam..." << endl;};
    };

    class Bird : public BirdInterface, public Animal
    {
    public:
    void fly() { cout << "flap flap flap ..." << endl;};
    void eat () { Animal::eat(); } ;
    };

    int main(int argc, char **argv) {
    Bird bird;
    bird.eat();
    bird.fly();
    }
    //*******************

    I have a problem with the method Bird::eat, it requires to explicitly calls the ancestor Animal::eat method. It becomes complicated to recall the ancestor methods for descendent classes when the hierarchy increases in size.
    Do you know a way to avoid this kind of repetitions or something that would mean the class Bird implements the AnimalInterface through the Animal class.

  2. #2
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: hierarchy of abstract interfaces implementation

    Instead of trying to cram many methods into a single interface, consider separating them according, as per the interface segregation principle. Maybe you could have something like this:
    Code:
    #include <iostream>
    
    using namespace std;
    
    class EaterInterface
    {
    public:
        virtual void eat() = 0;
        virtual ~EaterInterface() = default;
    };
    
    class FlyerInterface
    {
    public:
        virtual void fly() = 0;
        virtual ~FlyerInterface() = default;
    };
    
    class Animal : public EaterInterface
    {
    public:
        void eat() override {
            cout << "miam miam..." << endl;
        }
    
        virtual ~Animal() = 0;
    };
    
    Animal::~Animal() {}
    
    class Bird : public Animal, public FlyerInterface
    {
    public:
        void fly() override {
            cout << "flap flap flap ..." << endl;
        }
    };
    
    int main() {
        auto bird = Bird{};
        bird.eat();
        bird.fly();
    }
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  3. #3
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: hierarchy of abstract interfaces implementation

    When posting code, please use code tags. Go Advanced, select the formatted code and click '#'.

    Your design also has an issue arising from the so-called 'diamond problem' where bird is derived from birdinterface and animal, and both birdinterface and animal are derived from the same class animalinterface. In this case bird contains 2 sets of data members inherited from class animalinterface.
    See https://en.wikipedia.org/wiki/Virtual_inheritance for more info.

    The solution is to make the inherited base class virtual. Note that laserlight's solution doesn't have this issue as bird is derived from animal and flyerinterface and animal is derived from eaterinterface.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured