Abstract class and Multiple Inheritance errors??

Discussion in 'C++' started by santosh, Jan 4, 2005.

  1. santosh

    santosh Guest

    Hi All ,
    Why does the below code doesn't compile??

    class Interface
    {
    public:
    virtual void funA() = 0;
    virtual void funB() = 0;
    virtual void funD() = 0;
    Interface();
    ~Interface();
    };

    class A : public Interface
    {
    public:
    A();
    ~A();
    void funA()
    {
    printf("A::funA");
    }
    };

    class B : public Interface
    {
    public:
    B();
    ~B();
    void funB()
    {
    printf("B::funB");
    }
    };

    class D: virtual public A , virtual public B
    {
    public:
    D();
    ~D();
    void funD()
    {
    printf("D::funD");
    }
    };

    int _tmain(int argc, _TCHAR* argv[])
    {
    D *d;
    d = new D;

    return 0;
    }


    COMPILER ERRORS::
    error C2259: 'D' : cannot instantiate abstract class
    due to following members:
    'void Interface::funB(void)' : pure virtual function was not defined
    d:\TestCPP\TestCPP.cpp(11) : see declaration of 'Interface::funB'
    'void Interface::funA(void)' : pure virtual function was not defined
    d:\TestCPP\TestCPP.cpp(10) : see declaration of 'Interface::funA'

    Thanks
    -Sanotsh
    //AbstractInterface
    santosh, Jan 4, 2005
    #1
    1. Advertising

  2. santosh wrote:
    > Hi All ,
    > Why does the below code doesn't compile??
    >
    > class Interface
    > {
    > public:
    > virtual void funA() = 0;
    > virtual void funB() = 0;
    > virtual void funD() = 0;
    > Interface();
    > ~Interface();
    > };
    >

    You have abstract class Interface with 3 pure virtual functions

    > class A : public Interface
    > {
    > public:
    > A();
    > ~A();
    > void funA()
    > {
    > printf("A::funA");
    > }
    > };
    >

    The derived class doesn't define the two functions
    void funB () and void funD ()
    The whole purpose of specifying pure virtual functions in abstract
    class
    is enforce the definition in derived classes
    > class B : public Interface
    > {
    > public:
    > B();
    > ~B();
    > void funB()
    > {
    > printf("B::funB");
    > }
    > };
    >
    > class D: virtual public A , virtual public B
    > {
    > public:
    > D();
    > ~D();
    > void funD()
    > {
    > printf("D::funD");
    > }
    > };
    >
    > int _tmain(int argc, _TCHAR* argv[])
    > {
    > D *d;
    > d = new D;
    >
    > return 0;
    > }
    >
    >
    > COMPILER ERRORS::
    > error C2259: 'D' : cannot instantiate abstract class
    > due to following members:
    > 'void Interface::funB(void)' : pure virtual function was not

    defined
    > d:\TestCPP\TestCPP.cpp(11) : see declaration of

    'Interface::funB'
    > 'void Interface::funA(void)' : pure virtual function was not

    defined
    > d:\TestCPP\TestCPP.cpp(10) : see declaration of

    'Interface::funA'
    >
    > Thanks
    > -Sanotsh
    > //AbstractInterface
    Sagar Choudhary, Jan 4, 2005
    #2
    1. Advertising

  3. santosh

    Guest

    In other words, every class that inherits from an abstract base class
    must provide definitions for each of the 3 virtual functions.
    , Jan 4, 2005
    #3
  4. santosh

    Mike Hewson Guest

    wrote:
    > In other words, every class that inherits from an abstract base class
    > must provide definitions for each of the 3 virtual functions.


    Well, somewhere along the chain of inheritance, anyway. You can't stay
    virtual forever. :)

    The 'virtual' means "may be redefined later in a class derived from this
    one". The '=0' means "some class derived from this must define the
    function". Somewhere along the chain of inheritance, that is.

    Stroustrup has a good article somewhere on his website called "A Tour Of
    C++" ( pdf ). Go to http://www.research.att.com/~bs/homepage.html

    --

    Cheers
    --
    Hewson::Mike
    "This letter is longer than usual because I lack the time to make it
    shorter" - Blaise Pascal
    Mike Hewson, Jan 5, 2005
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Matthias Kaeppler
    Replies:
    1
    Views:
    420
    R.F. Pels
    May 22, 2005
  2. Sameer
    Replies:
    4
    Views:
    573
    Roedy Green
    Aug 31, 2005
  3. santosh
    Replies:
    0
    Views:
    305
    santosh
    Jan 5, 2005
  4. Uzytkownik
    Replies:
    3
    Views:
    578
    Uzytkownik
    Apr 3, 2005
  5. Replies:
    4
    Views:
    801
    Rolf Magnus
    May 17, 2006
Loading...

Share This Page