View Full Version : Multiple Inheritance

20th February 2006, 07:44
Hi Guys for the following Diagram How do v convert it to Code

| Base |
+----------------------------+ |
| Derived | |
+----------------------------+ |
^ ______________ |
| |
| Multiple |

So that I can access the Base's Function as well as the Derived's function

20th February 2006, 08:29
Take a look:

class Base {
virtual void method1() { std::cout << "Base::method1()" << std::endl; }
virtual void method2() { std::cout << "Base::method2()" << std::endl; }
class Derieved : public Base {
virtual void method1() { std::cout << "Derieved::method1()" << std::endl; }
class Multilply : public Derieved {
virtual void method2() { std::cout << "Multiply::method2()" << std::endl; }

Multiply obj;


This will produce the following:


Now line-by-line comments:

Class Multiply inherits all the methods from class Derieved. It doesn't overload method1(), so Multiply::method1() in fact calls Derieved::method1()
Mulitply::method2() is overloaded
Class Base is Muliply class' ancestor, so it could be dynamic_cast<>()ed to Base class to access its methods

20th February 2006, 09:44
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

20th February 2006, 23:01
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.


21st February 2006, 04:00
Thanks That Helped