If I derive a child from multiple parents and I want to use the vitual keyword in the class declaration, for example:
class Child:virtual public Parent1, virtual public Parent2
{
}
Can I, as a rule of thumb, always use the virtual keyword when I work with multiple inheritance to avoid duplicating parent class instances? Or am I going to cause problems for myself in doing so? I know I should avoid inheritance from multiple parents but this is just for interest sake...
Multiple Inheritance in C++
-
- Registered User
- Posts: 116
- Joined: 04 Apr 2006, 02:00
- Location: Pretoria::www.systemshock.co.za
- Contact:
Multiple Inheritance in C++
"I also believe that the more we help the students, the better they will be when we need them to give us a service." by jee
www.systemshock.co.za
www.systemshock.co.za
In short. Yes and No.
To avoid this problem you should virtually inherit the base classes before they are joined in multiple inheritance.
Remember that this is only a problem when you have two classes that have (along the hierarchy) inherited from the same parent and then you want to inherit both of these classes in a sub class. (a sort of Object Oriented inbreeding )
Example:
This could cause ambiguity because both parents inherit from the same base class.
What you do now, is to Virtually inherit the mother and father classes to cut the ambiguity out there already.
Like this:
So, in conclusion. Don't ALWAYS use virtual inheritance to avoid duplication, because it will not always be the case.
And when you do, do as I showed here, by virtually inheriting before you join the base classes with multiple inheritance.
Does this answer your question?
To avoid this problem you should virtually inherit the base classes before they are joined in multiple inheritance.
Remember that this is only a problem when you have two classes that have (along the hierarchy) inherited from the same parent and then you want to inherit both of these classes in a sub class. (a sort of Object Oriented inbreeding )
Example:
Code: Select all
Class Human {}
Class Mother: Public Human{}
Class Father: Public Human{}
Class Child: Public Mother, Public Father{}
What you do now, is to Virtually inherit the mother and father classes to cut the ambiguity out there already.
Like this:
Code: Select all
Class Human {}
Class Mother: virtual Public Human{}
Class Father: virtual Public Human{}
Class Child: Public Mother, Public Father{}
And when you do, do as I showed here, by virtually inheriting before you join the base classes with multiple inheritance.
Does this answer your question?
-
- Registered User
- Posts: 116
- Joined: 04 Apr 2006, 02:00
- Location: Pretoria::www.systemshock.co.za
- Contact:
Yes, that was my understanding from what I read in my textbooks but I just needed some confirmation that I'm understanding it correctly...
Thanx Kronos
Thanx Kronos
"I also believe that the more we help the students, the better they will be when we need them to give us a service." by jee
www.systemshock.co.za
www.systemshock.co.za
-
- Registered User
- Posts: 116
- Joined: 04 Apr 2006, 02:00
- Location: Pretoria::www.systemshock.co.za
- Contact:
//---------------------------------------------------------------------------
#include <iostream.h>
#include <conio.h>
#pragma hdrstop
//---------------------------------------------------------------------------
class MusicalComposition
{
protected:
char title[20];
char composer[20];
char date[4];
public:
MusicalComposition(char inTitle[], char inComposer[], char inDate[]);
void showData();
};
MusicalComposition::MusicalComposition(char inTitle[], char inComposer[], char inDate[])
{
strcpy(title, inTitle);
strcpy(composer, inComposer);
strcpy(date, inDate);
}
void MusicalComposition::showData()
{
cout << "Title: " << title << endl;
cout << "Composer: " << composer << endl;
cout << "Year: " << date << endl;
}
//---------------------------------------------------------------------------
class NationalAnthem:public MusicalComposition
{
protected:
char nation[30];
public:
NationalAnthem(char inNation[], char inTitle[], char inComposer[], char inDate[]);
void showData();
};
NationalAnthem::NationalAnthem(char inNation[], char inTitle[], char inComposer[], char inDate[]):MusicalComposition(inTitle, inComposer, inDate)
{
strcpy(nation, inNation);
}
void NationalAnthem::showData()
{
MusicalComposition::showData();
cout << "Nation: " << nation << endl;
}
//---------------------------------------------------------------------------
#pragma argsused
int main(int argc, char* argv[])
{
MusicalComposition aPieceOfMusic("It's on", "KoRn", "2000");
aPieceOfMusic.showData();
NationalAnthem anAnthem("South Africa", "Nkosi Sikeleli", "Mandela", "1994");
anAnthem.showData();
system("pause");
return 0;
}
//---------------------------------------------------------------------------
Hey Kronos,
Could you have a look at this piece of code please...
When I compile it, it outputs the second instance's (anAnthem) date[] member incorrectly, it gives me the following:
Title: Nkosi Sikeleli
Composer: Mandela
Year: 1994South Africa
Nation: South Africa
It's as if it's not stopping at the 4 character when plotting the char array, don't know if there's a problem with the referencing of the memory cause when I plot every character individually, everything's fine until I get to date[4] and onwards, then it starts to plot the nation[] member from the NationalAnthem child class.
Okay, if there is not a problem with my code, how does this work then...
When I include the virtual keyword at the child class declaration:
class NationalAnthem:virtual public MusicalComposition
Everything's okay, when I remove it again, I sit with the problem that the date is output like:
1994South Africa
BTW, I'm using Borland C++ Builder 6 if it makes any difference
#include <iostream.h>
#include <conio.h>
#pragma hdrstop
//---------------------------------------------------------------------------
class MusicalComposition
{
protected:
char title[20];
char composer[20];
char date[4];
public:
MusicalComposition(char inTitle[], char inComposer[], char inDate[]);
void showData();
};
MusicalComposition::MusicalComposition(char inTitle[], char inComposer[], char inDate[])
{
strcpy(title, inTitle);
strcpy(composer, inComposer);
strcpy(date, inDate);
}
void MusicalComposition::showData()
{
cout << "Title: " << title << endl;
cout << "Composer: " << composer << endl;
cout << "Year: " << date << endl;
}
//---------------------------------------------------------------------------
class NationalAnthem:public MusicalComposition
{
protected:
char nation[30];
public:
NationalAnthem(char inNation[], char inTitle[], char inComposer[], char inDate[]);
void showData();
};
NationalAnthem::NationalAnthem(char inNation[], char inTitle[], char inComposer[], char inDate[]):MusicalComposition(inTitle, inComposer, inDate)
{
strcpy(nation, inNation);
}
void NationalAnthem::showData()
{
MusicalComposition::showData();
cout << "Nation: " << nation << endl;
}
//---------------------------------------------------------------------------
#pragma argsused
int main(int argc, char* argv[])
{
MusicalComposition aPieceOfMusic("It's on", "KoRn", "2000");
aPieceOfMusic.showData();
NationalAnthem anAnthem("South Africa", "Nkosi Sikeleli", "Mandela", "1994");
anAnthem.showData();
system("pause");
return 0;
}
//---------------------------------------------------------------------------
Hey Kronos,
Could you have a look at this piece of code please...
When I compile it, it outputs the second instance's (anAnthem) date[] member incorrectly, it gives me the following:
Title: Nkosi Sikeleli
Composer: Mandela
Year: 1994South Africa
Nation: South Africa
It's as if it's not stopping at the 4 character when plotting the char array, don't know if there's a problem with the referencing of the memory cause when I plot every character individually, everything's fine until I get to date[4] and onwards, then it starts to plot the nation[] member from the NationalAnthem child class.
Okay, if there is not a problem with my code, how does this work then...
When I include the virtual keyword at the child class declaration:
class NationalAnthem:virtual public MusicalComposition
Everything's okay, when I remove it again, I sit with the problem that the date is output like:
1994South Africa
BTW, I'm using Borland C++ Builder 6 if it makes any difference
"I also believe that the more we help the students, the better they will be when we need them to give us a service." by jee
www.systemshock.co.za
www.systemshock.co.za
-
- Registered User
- Posts: 116
- Joined: 04 Apr 2006, 02:00
- Location: Pretoria::www.systemshock.co.za
- Contact:
Sorry, Kronos, I never got back to you about this. Well, I tried it with the method as virtual and didn't work, only when I include the virtual keyword at the child class delcaration, did it work. So theory doesn't add up to the practical, but it's no issue cause this was just for the course I'm busy now and I know I should avoid multiple inheritance anyway...
Thank you for your assistance...
Thank you for your assistance...
"I also believe that the more we help the students, the better they will be when we need them to give us a service." by jee
www.systemshock.co.za
www.systemshock.co.za