Can somebody give me a example: How do i multiple inherit int, float from first class and char, string from second class into the third class ?
1) You cannon inherit from base types.
2) Normal multiple inheritance looks like this:
class A { /* ... */ };
class B { /* ... */ };
class C { /* ... */ };
class X : public A, private B, public C { /* ... */ };
class A
{
private:
int i;
float f;
};
class B
{
private:
char c;
std::string s;
};
class C : public A, public B
{
};
Objects of type C now contain members i, f, c, and s. Note that you won't be able to access these members from within methods of C, since they remain private to A and B respectively. In order to access these from within C methods, they would have to have been originally declared as public or protected rather than private, but that is not good design.
Do you mean inheriting from a class with int and float field and a second class containing a char and string field?
class1
{
int anInt;
float aFloat;
}
class2
{
char aChar;
string aString;
}
class3 : public class1, public clas2
{
...
}
Normally, you don't use multiple inheritance to get access to data (data should normally be private, so a derived class can't access it anyway).
Multiple inheritance basically produces an object with more than one set of properties. For example, consider doors, some wood and some steel:
class steel {
unsigned int AISI_number;
char Rockwell_scale;
unsigned int Rockwell_number;
};
class wood {
double density;
std::string species;
};
class door {
int width;
int height;
unsigned char num_hinges;
};
class wooden_door : public wood, public door {};
class steel_door : public steel, public door {};
This is a bit contrived, because it's probably pretty rare that we'd actually care much about the steel in a steel door (e.g., that it's 1020 steel that has been hardened to Rockwell C40), but I hope the general idea comes through anyway. [And yes, I'm aware that all the data is inaccessible, because it's all private, and there's no code to access it in any of the classes...]