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?
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?
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.
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:
Do I need to set the two base classes of CLamp as extern classes?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:
My house contains a dog. That doesn't mean my house is a dog.Code:class CLamp { private: CSwitch switch; CBulb bulb; };
You need to include their header files.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
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?
The normal way would be:Of course you would name the variable something more meaningful and use a number other than 5, but that's how you do it.Code:CLamp my_lamp(5);
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); // ... };
can you please show us your whole code? or atleast what matters..
headers, base class, derived classes, classes in action etc
I got the problem related to this thread figured out now. Thanks for your help.