-
October 11th, 2009, 12:42 PM
#1
Object Oreinted inheritance design question
Hello,
I have two OOP design questions:
1. I have four classes say A, B, C, and D. B is derived from A. Class C is derived from B, but it is an optional class in my toolkit. Class D is also optional, but it needs to be derived from class C if it is used, otherwise it should be derived from class B.
One way to solve this I guess if use preprocessor definitions, and have #ifdef's in the code. But, is there any other good design way to handle this?
2. Say I have 3 classes, A, B, and C. I want C to either be derived from class A or class B. I know I can use multiple inheritance to derive C from both A and B, but I want to be abelt o make this decision dynamically when an instance of class C is created. Is that possible with C++?
Regards,
Ellay K.
-
October 11th, 2009, 01:33 PM
#2
Re: Object Oreinted inheritance design question
Originally Posted by ekhule
Hello,
I have two OOP design questions:
1. I have four classes say A, B, C, and D. B is derived from A. Class C is derived from B, but it is an optional class in my toolkit.
"Optional" in what way? I think you need to clarify what you mean by "optional". Compile-time "optional"? Run-time "optional"?
Regards,
Paul McKenzie
-
October 11th, 2009, 01:54 PM
#3
Re: Object Oreinted inheritance design question
Originally Posted by ekhule
I have two OOP design questions:
An inheritance relationship isn't just a technicality. It should express some meaning.
In the first case, whether D is derived from C or B gives very different meaning to D.
Also in the second case, whether C is derived from A or B or both A and B obviously makes C very different.
In both cases it makes sense to introduce different most derived classes, like D1/D2 and C1/C2/C3 respectively.
Note that in OO design, using inheritance to get at implementation is not considered a very good idea. Base classes should represent abstract ideas, not be carriers of implementation. If you want to make classes share implementation it's better to use delegation, or possibly private inheritance.
-
October 11th, 2009, 07:03 PM
#4
Re: Object Oreinted inheritance design question
Paul,
Optional as in the class is a layered toolkit.. each layer does some operation on data before passing it up to the parent layer. Someone might not want to use class C in my library, so it is compile-time optional. However if they want to use class D then they must derive it from class B instead of class C.
Do you see where I am getting at?
Regards,
Ellay K.
-
October 11th, 2009, 07:12 PM
#5
Re: Object Oreinted inheritance design question
You cannot create an inheritance relationship dinamically in C++. It needs to be done in compile time.
Since you're talking about design decisions concerning base/derived classes, it's good to remember that inheritance is commonly abused. You should not inherit to reuse base class code, but to reuse code that uses a base class polymorphically. You should use inheritance when the Liskov Substitution Principle applies.
-
October 12th, 2009, 12:11 AM
#6
Re: Object Oreinted inheritance design question
Originally Posted by ltcmelo
You cannot create an inheritance relationship dinamically in C++. It needs to be done in compile time.
There's something called dynamic inheritance. It's when you make it possible for a class to switch its inherited implementation at runtime. It's also called the State design pattern. It allows you to change the "gut" of a class at runtime.
But what you say is true. Dynamic inheritance doesn't change the inherited type, only the inherited implementation.
-
October 12th, 2009, 12:51 AM
#7
Re: Object Oreinted inheritance design question
ekhule, what you are describing here sounds like http://en.wikipedia.org/wiki/Policy-based_design and/or mix-in base classes
Wakeup in the morning and kick the day in the teeth!! Or something like that.
"i don't want to write leak free code or most efficient code, like others traditional (so called expert) coders do."
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|