confusion on multiple inheritance

This is a discussion on confusion on multiple inheritance within the C++ Programming forums, part of the General Programming Boards category; I have a confusion on multiple inheritance. Suppose, I have classes A,B,C each having the same method . Now, how ...

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    11

    confusion on multiple inheritance

    I have a confusion on multiple inheritance. Suppose, I have classes A,B,C each having the same method . Now, how does X will know which method to call ?

    class A { /* ... */ };
    class B { /* ... */ };
    class C { /* ... */ };
    class X : public A, public B, public C { /* ... */ };
    Last edited by Stream; 02-12-2011 at 01:46 AM.

  2. #2
    printf("Hello Cboard\n"); codeprada's Avatar
    Join Date
    Jan 2011
    Location
    In a little room at the back of your brain
    Posts
    68
    X would call the method in X. to access other methods from other classes use X.C::method(); assuming that B is not derived from A and C from B.

    Code:
    #include <iostream>
    
    using namespace std;
    
    class A
    {
    public:
        A() {};
        ~A() {};
        char getName()
        {
            return 'A';
        }
    };
    
    class B
    {
    public:
        B() {};
        ~B() {};
        char getName()
        {
            return 'B';
        }
    };
    
    class C
    {
    public:
        C() {};
        ~C() {};
        char getName()
        {
            return 'C';
        }
    };
    
    class X : public A, public B, public C
    {
    public:
        X() : A(), B(), C() {};
        ~X() {};
        char getName()
        {
            return 'X';
        }
    };
    
    int main(int argc, char * argv[])
    {
        X sample;
        cout << "sample.getName() -> " << sample.getName() << endl
             << "sample.A::getName() -> " << sample.A::getName() << endl
             << "sample.B::getName() -> " << sample.B::getName() << endl
             << "sample.C::getName() -> " << sample.C::getName() << endl;
        return 0;
    }
    Last edited by codeprada; 02-12-2011 at 11:55 AM.
    We shouldn't be quick to criticize unless we can do better.
    Code:
    public function __clone() { die ( "I'm one of a kind" ); }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 06-08-2009, 04:03 PM
  2. references and multiple inheritance - incompatible?
    By m37h0d in forum C++ Programming
    Replies: 11
    Last Post: 09-03-2008, 03:35 PM
  3. Multiple inheritance in C#
    By DavidP in forum C# Programming
    Replies: 1
    Last Post: 06-27-2008, 05:41 PM
  4. Multiple Inheritance - Size of Classes?
    By Zeusbwr in forum C++ Programming
    Replies: 10
    Last Post: 11-26-2004, 09:04 AM
  5. Multiple virtual inheritance
    By kitten in forum C++ Programming
    Replies: 3
    Last Post: 08-10-2001, 11:04 PM

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