tags:

views:

79

answers:

4

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 ?

A: 

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 { /* ... */ };
Klark
So i just use "," NICE
zreed
+3  A: 
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.

Tyler McHenry
thank you very much !
zreed
+1 Somebody actually read the question.
Martin York
How is having a member variable be protected instead of private not good design?
Fred
@Fred: There is a whole line of thought that protected in itself is a bad idea (personally not going to argue either way). Stroustrup regrets introducing the concept See "Design and Evolutionof the C++ programming language"
Martin York
@Fred Protected is part of the class's interface, just like public is -- you just access it differently (inheriting instead of instantiating). Except in POD structs, data members should not be part of the interface to a class, otherwise you break encapsulation.
Tyler McHenry
Jerry Coffin
Note that the use of 'protected' is just as invasive to your code as the use of 'public'. Protected members become part of the 'Public Interface' (don't confuse this term with access control) and you are contractually obligated to maintain this interface however the internal representation of the class changes (otherwise you introduce breaking changes which is what encapsulation is supposed to prevent). So protected member broaden the 'Public Interface' without providing any real access control as it can be bypassed by inheritance. (Not saying it is bad thing just something to note).
Martin York
+1  A: 

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
{
...
}
nonnb
Can i pass on protected int, float ?
zreed
+2  A: 

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...]

Jerry Coffin