-
March 20th, 2014, 10:18 PM
#1
Inheritance
What is the difference between HAS-A and IS-A relationship ?
-
March 21st, 2014, 02:13 AM
#2
Re: Inheritance
Originally Posted by Harris1995
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.
-
March 21st, 2014, 06:50 AM
#3
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)
-
March 21st, 2014, 06:51 AM
#4
Re: Inheritance
Thanks.
-
March 21st, 2014, 06:57 AM
#5
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)
-
March 21st, 2014, 07:01 AM
#6
Re: Inheritance
Thank you very much.
-
March 21st, 2014, 08:19 AM
#7
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".
-
March 22nd, 2014, 12:42 AM
#8
Re: Inheritance
Originally Posted by OReubens
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|