Thread: inheritance and polymorphism question of object of class A inhibiting class B and C

  1. #1
    Registered User
    Join Date
    Jul 2004
    Posts
    222

    inheritance and polymorphism question of object of class A inhibiting class B and C

    I searched through the web and my textbook and I can only find examples of only for example class A inheriting characteristics of another class, less say class B. How hard would it be for class A to inherit characters of both class B and class C?

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You can do that, it's called "Multiple inheritance", and as long as B and C aren't related, it's pretty much straight-forward.

    Code:
    class B { ... };
    class C { ... };
    
    class A: public B, public C
    { ... };
    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Jul 2004
    Posts
    222
    I'm getting a problem with Microsoft Visual C++ that I am experiencing an error message of
    (7) : error C2504: 'CSwitch' : base class undefined
    (8) : error C2504: 'CBulb' : base class undefined

    in the code:
    Code:
    class CLamp: public CSwitch, public CBulb
    {
    public:
    	CLamp(int x);				// constructor that will construct the lamp with x watts
    };
    Do I need to set the two base classes of CLamp as extern classes?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You need to include their header files.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Jul 2004
    Posts
    222
    I realized that I have included my header files in the wrong places. I have another problem: given my current CLamp declaration, how would I declare a new CLamp variable using my own constructor?

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    The normal way would be:
    Code:
    CLamp my_lamp(5);
    Of course you would name the variable something more meaningful and use a number other than 5, but that's how you do it.

    However, you are not using inheritance correctly. A lamp has a switch and a bulb. Public inheritance of for when something works like something else. When an object is made of another object, you use containment:
    Code:
    class CLamp
    {
        CSwitch switch;
        CBulb bulb;
    public:
        CLamp(int watts);
    
        // ...
    };

  7. #7
    Registered User
    Join Date
    Sep 2005
    Posts
    196
    can you please show us your whole code? or atleast what matters..

    headers, base class, derived classes, classes in action etc

  8. #8
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by stanlvw View Post
    I'm getting a problem with Microsoft Visual C++ that I am experiencing an error message of
    (7) : error C2504: 'CSwitch' : base class undefined
    (8) : error C2504: 'CBulb' : base class undefined

    in the code:
    Code:
    class CLamp: public CSwitch, public CBulb
    {
    public:
    	CLamp(int x);				// constructor that will construct the lamp with x watts
    };
    This is an awful example. A lamp is not a switch, nor is it a bulb. It is an object which CONTAINS a switch and a bulb. Inheritance is not what you want here. What you want is composition:

    Code:
    class CLamp
    {
    private:
        CSwitch switch;
        CBulb bulb;
    };
    My house contains a dog. That doesn't mean my house is a dog.

  9. #9
    Registered User
    Join Date
    Jul 2004
    Posts
    222
    I got the problem related to this thread figured out now. Thanks for your help.

Popular pages Recent additions subscribe to a feed