CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Mar 2009
    Posts
    166

    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.

  2. #2
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Object Oreinted inheritance design question

    Quote Originally Posted by ekhule View Post
    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

  3. #3
    Join Date
    May 2009
    Posts
    2,413

    Re: Object Oreinted inheritance design question

    Quote Originally Posted by ekhule View Post
    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.

  4. #4
    Join Date
    Mar 2009
    Posts
    166

    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.

  5. #5
    Join Date
    Jan 2006
    Location
    Belo Horizonte, Brazil
    Posts
    405

    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.

  6. #6
    Join Date
    May 2009
    Posts
    2,413

    Re: Object Oreinted inheritance design question

    Quote Originally Posted by ltcmelo View Post
    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.

  7. #7
    Join Date
    Nov 2002
    Location
    Los Angeles, California
    Posts
    3,863

    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
  •  





Click Here to Expand Forum to Full Width

Featured