Results 1 to 5 of 5

Thread: Multiple Inheritance

  1. #1
    Join Date
    Jan 2006
    Location
    Kerala
    Posts
    371
    Thanks
    76
    Thanked 37 Times in 32 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Multiple Inheritance

    Hi Guys for the following Diagram How do v convert it to Code
    Qt Code:
    1. .
    2. +----------------------------+
    3. | Base |
    4. +----------------------------+
    5. ^
    6. |----------------------+
    7. +----------------------------+ |
    8. | Derived | |
    9. +----------------------------+ |
    10. ^ ______________ |
    11. | |
    12. +----------------------------+
    13. | Multiple |
    14. +----------------------------+
    To copy to clipboard, switch view to plain text mode 
    So that I can access the Base's Function as well as the Derived's function
    We can't solve problems by using the same kind of thinking we used when we created them

  2. #2
    Join Date
    Feb 2006
    Location
    Kirovohrad, Ukraine
    Posts
    72
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Multiple Inheritance

    Take a look:
    Qt Code:
    1. class Base {
    2. //..
    3. virtual void method1() { std::cout << "Base::method1()" << std::endl; }
    4. virtual void method2() { std::cout << "Base::method2()" << std::endl; }
    5. };
    6. class Derieved : public Base {
    7. //..
    8. virtual void method1() { std::cout << "Derieved::method1()" << std::endl; }
    9. };
    10. class Multilply : public Derieved {
    11. //..
    12. virtual void method2() { std::cout << "Multiply::method2()" << std::endl; }
    13. };
    14.  
    15. Multiply obj;
    16.  
    17. obj.method1();
    18. obj.method2();
    19. dynamic_cast<Base>(obj).method2();
    To copy to clipboard, switch view to plain text mode 
    This will produce the following:
    Derieved::method1()
    Multiply::method2()
    Base::method1()
    Now line-by-line comments:
    1. Class Multiply inherits all the methods from class Derieved. It doesn't overload method1(), so Multiply::method1() in fact calls Derieved::method1()
    2. Mulitply::method2() is overloaded
    3. Class Base is Muliply class' ancestor, so it could be dynamic_cast<>()ed to Base class to access its methods

  3. #3
    Join Date
    Jan 2006
    Location
    Kerala
    Posts
    371
    Thanks
    76
    Thanked 37 Times in 32 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Multiple Inheritance

    Suppose there is a base class called the Table. It holds the database table's name and the primary key, . Table contains the functions table() and the primaryKeyField(), virtual bool insert() = 0, update(), remove() - Forget about the update and the remove

    Next there is a staff class derived from the table Class. The staff table writes to the staff-table in database. table() return the staff-table, and primaryKeyField(),The Employee & the ResearchFellow classes needs to derive from both the Table class and the staff class. So that the table() gives the "Employee-Table" as the return value. and I can call Staff::insert() to insert values to the StaffTable.

    Or Is there a better design Pattern
    We can't solve problems by using the same kind of thinking we used when we created them

  4. #4
    Join Date
    Jan 2006
    Location
    N.B. Canada
    Posts
    47
    Thanked 8 Times in 7 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Multiple Inheritance

    I am not sure if I understand this correctly, but here goes...

    What you have there is a Table class, that is an abstract class due to the abstract method insert().

    Staff inherits from this Table class, and implements its own table() method, as well as implements the abstract method insert().

    Next, as I understand it, you need two classes Employee and ResearchFellow, that each need to have their own table() method, but need to use the Staff::Insert() method for inserting. However, it is not clear to me if Employee's and ResearchFellow's table() method would be the same or not. If they are the same, is there a point in having two classes instead of one? Therefore I am assuming that their table() methods are in fact different.

    This can be achieved simply by making Employee and ResearchFellow inherit just from Staff, and they would implement their own table() method. This way when table() is called on either of the two classes, their respective table() method would actually be executed. When insert() method is called on an Employee or ResearchFellow object, the call would end up going to the Staff::insert(), since neither of the two classes reimplement insert() from Staff. Remember, public inheritence means IS A, and that's the golden rule, and there are no exceptions. So I don't really see a need for multiple inheritence here.

    I may have misunderstood. Please clear up what methods are the same and what are different between what classes.

    Bojan
    The march of progress:
    C:
    printf("%10.2f", x);
    C++:
    cout << setw(10) << setprecision(2) << showpoint << x;
    Java:
    java.text.NumberFormat formatter = java.text.NumberFormat.getNumberInstance();
    formatter.setMinimumFractionDigits(2);
    formatter.setMaximumFractionDigits(2);
    String s = formatter.format(x);
    for (int i = s.length(); i < 10; i++) System.out.print(' ');
    System.out.print(s);

  5. #5
    Join Date
    Jan 2006
    Location
    Kerala
    Posts
    371
    Thanks
    76
    Thanked 37 Times in 32 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Multiple Inheritance

    Thanks That Helped
    We can't solve problems by using the same kind of thinking we used when we created them

Similar Threads

  1. Object and multiple inheritance for interfaces
    By brcain in forum Qt Programming
    Replies: 8
    Last Post: 29th June 2021, 16:29
  2. Multiple Inheritance for QGraphicsItem
    By pankaj.patil in forum Qt Programming
    Replies: 2
    Last Post: 1st July 2008, 15:49
  3. QThread and Multiple inheritance
    By ^NyAw^ in forum General Programming
    Replies: 6
    Last Post: 10th January 2008, 11:50
  4. Multiple Inheritance & Qt
    By kefeng.chen in forum Qt Programming
    Replies: 8
    Last Post: 21st March 2006, 19:37
  5. Multiple inheritance & Qt
    By dublet in forum Qt Programming
    Replies: 11
    Last Post: 8th March 2006, 09:12

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.