CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8

Thread: Inheritance

  1. #1
    Join Date
    Mar 2014
    Posts
    17

    Question Inheritance

    What is the difference between HAS-A and IS-A relationship ?

  2. #2
    Join Date
    Jul 2013
    Posts
    576

    Re: Inheritance

    Quote Originally Posted by Harris1995 View Post
    What is the difference between HAS-A and IS-A relationship ?
    It's like the difference between having a question and being the question.

    Now code some C++ and ask about that.

  3. #3
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

    Re: Inheritance

    See http://en.wikipedia.org/wiki/Is-a

    A car is-a vehicle and has-a steering wheel.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  4. #4
    Join Date
    Mar 2014
    Posts
    17

    Re: Inheritance

    Thanks.

  5. #5
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

    Re: Inheritance

    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  6. #6
    Join Date
    Mar 2014
    Posts
    17

    Re: Inheritance

    Thank you very much.

  7. #7
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    4,626

    Re: Inheritance

    and the unfortunate reality is that sometimes you think something IS-A something else (because it's assumed so in the real world), when it really isn't in the context of programming.

    A circle is NOT a special case of an ellipse in C++ class design, even though every math book will tell you it is. Equally a square is not a rectangle.

    In the case of C++ and all other programming languages:
    "x IS-A y" means that x shares all of the properties of y.
    this is why a circle isn't an ellipse, because an ellipse can have a different width and height where a circle can't.
    Trying to make a class design where a circle is derived from an ellipse will have several failure points because of this.

    HAS-A is all about ownership "it's the stuff you have in your pockets".

  8. #8
    Join Date
    Jul 2013
    Posts
    576

    Re: Inheritance

    Quote Originally Posted by OReubens View Post
    Trying to make a class design where a circle is derived from an ellipse will have several failure points because of this.
    It's quite easily fixed actually. One way is to make the classes immutable. Then the Liskov Substitution (LSP) principle isn't broken anymore and the design is fine.

    I think you're being too categorical. The ellipse/circle is a good example because it clearly demonstrates some design pitfalls. But that doesn't mean it's an impossible design. It just shows you need to be careful and explicit with your definitions and assumptions.

    IS-A vs. HAS-A is a first approximation only. The next step is always to align the design with language oriented principles,

    http://en.wikipedia.org/wiki/SOLID_(...iented_design)
    Last edited by razzle; March 22nd, 2014 at 05:05 PM.

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