Abstract inheritence

This is a discussion on Abstract inheritence within the C++ Programming forums, part of the General Programming Boards category; I understand that a class containing abstract methods (virtual <type> <method name>(<params>) = 0; ) can only be used to ...

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    71

    Abstract inheritence

    I understand that a class containing abstract methods (virtual <type> <method name>(<params>) = 0; ) can only be used to derive from and that the deriving class must implement the abstracted methods, but I'm wondering if it's possible for abstract classes to inherit from other abstract classes. See, in other languages (ones that utilize interfaces rather than multiple inheritence) the interface can inherit another interface(s). I'm wondering if the same can be said for abstract classes in C++.

  2. #2
    "Why use dynamic memory?"
    Join Date
    Aug 2006
    Posts
    186
    why would you do that ??
    abstract class is already an abstract class, one I think should be enough because after all it's generic and should contain all functions

    multiple inheritance can cause confusion
    "C makes it easy to shoot yourself in the foot; C++ makes it harder, but when you do, it blows away your whole leg."-Bjarne Stroustrup
    Nearing the end of finishing my 2D card game! I have to work on its 'manifesto' though <_<

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    890
    Sure it's possible. You might want an abstract subclass to provide a partial default implementation. Even if you don't provide a default impl, you may want to extend the interface.

    Code:
    #include <iostream>
    
    class A {
    public:
        virtual void func1() = 0;
        virtual void func2() = 0;
    };
    
    class B : public A {
    public:
        virtual void func2() 
        {
    	std::cout << "B::func2()" << std::endl;
        }
    };
    
    class C : public B {
    public:
        virtual void func1() 
        {
    	std::cout << "C::func1()" << std::endl;
        }
    };
    
    int main()
    {
        A * c = new C;
        // Can't do this, because B is abstract
        // A * b = new B;  
    
        c->func1();
        c->func2();
    }
    $ g++ abstract.cpp
    $ a.out
    C::func1()
    B::func2()
    $
    Last edited by medievalelks; 06-18-2009 at 09:36 AM.

  4. #4
    "Why use dynamic memory?"
    Join Date
    Aug 2006
    Posts
    186
    because in A there is func2() which is pure
    and you define it again in B not as pure but just virtual

    you are trying to make a function virtual while it already exists as virtual

    i think that's the problem
    "C makes it easy to shoot yourself in the foot; C++ makes it harder, but when you do, it blows away your whole leg."-Bjarne Stroustrup
    Nearing the end of finishing my 2D card game! I have to work on its 'manifesto' though <_<

  5. #5
    Registered User
    Join Date
    Apr 2008
    Posts
    890
    It's not a problem if that's what you want. You may want a sub-interface that also cannot be instantiated.

    Code:
    class Bird {
    public:
        virtual ~Bird() {}
        virtual void sing() = 0;
        virtual void eat() = 0;
        virtual void walk() = 0;
    };
    
    class FlyingBird : public Bird {
    public:
        virtual void fly() = 0;
    };
    Last edited by medievalelks; 06-18-2009 at 09:36 AM.

  6. #6
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    839
    multiple inheritance can cause confusion
    only if you're confused about how multiple inheritance works.

    @ OP:

    sure there's no reason you can't do that. you just have to implement both classes' abstract methods before you can instantiate something deriving from them.

  7. #7
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,265
    you are trying to make a function virtual while it already exists as virtual

    i think that's the problem
    That's not a problem. In fact, if you override a virtual function in a base class, it is automatically virtual whether you specify it or not.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Abstract or Interface?
    By audinue in forum Tech Board
    Replies: 6
    Last Post: 07-02-2009, 08:09 AM
  2. Abstract constructor
    By Shal in forum C++ Programming
    Replies: 9
    Last Post: 05-18-2006, 02:14 PM
  3. Abstract Factory pattern
    By Just in forum C++ Programming
    Replies: 3
    Last Post: 02-18-2005, 10:58 AM
  4. abstract class
    By xddxogm3 in forum C++ Programming
    Replies: 5
    Last Post: 01-01-2005, 09:08 AM
  5. abstract vs pure abstract class
    By Kohatian 3279 in forum C++ Programming
    Replies: 2
    Last Post: 05-10-2002, 12:12 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21