GIDForums  

Go Back   GIDForums > Computer Programming Forums > Miscellaneous Programming Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 04-Jul-2006, 11:19
netnut netnut is offline
Member
 
Join Date: Dec 2005
Location: India
Posts: 174
netnut will become famous soon enough

Problems understanding inheritence.


Hi,

After quite a bit of procedural programming, we started inheritence today at school. Now, I understood what inheritence basically does, but I could not convince myself of its advantages. Moreover I seem to get confused asking myself "what should inherit what". We just had a discussion on the topic (all theoretical) today and haven't been given any problems to solve yet... but I have a question of my own (and I don't know even if inheritence is required or not):

Suppose I have a class student with all the necessary details. Now each student is issued a "school library card", which is a class on its own. Should, in this case, student inherit library-card of vice versa... or is this a simply absurd question that originated in my mind?

Please join this discussion... (and help me in this unknown territory)

Regards,
netnut.
  #2  
Old 05-Jul-2006, 00:43
crystalattice's Avatar
crystalattice crystalattice is offline
Flame War Instigator
 
Join Date: Apr 2004
Location: San Diego
Posts: 1,576
crystalattice is just really nicecrystalattice is just really nicecrystalattice is just really nicecrystalattice is just really nicecrystalattice is just really nice

Re: Problems understanding inheritence.


The easiest way to think of inheritance is via the "IS-A" method. For example, you could have class Automobile which has characteristics of having 4 wheels and an engine. Subclasses could be Car, Truck, Minivan, etc. with each one inheriting class Automobile's wheels and engine but having their own characteristics (e.g. 4 doors vs. 2 doors, sliding passenger door, etc.).

This way you can say a Car "IS-A" Automobile, but you couldn't say Car "IS-A" Minivan. In your Student/Library card example, ask yourself if a Library Card "IS-A" type of Student. The answer should be "No".

A better way to use your example is Library Card "IS-A" Identification and Student "IS-A" School Member. Then class Identification could not only have Library Card as a subclass, but you could also have Driver's License, Student ID card, etc. and your School Member class could be subclassed into Student, Administrator, Teacher, Custodian, etc.

Hope that makes more sense now. It took me a while to figure out how it all works with objects, so don't feel bad or stupid.
__________________
Start Programming with Python-A beginner's guide to programming and the Python language.
-------------
Common Sense v2.0-Striving to make the world a little bit smarter.
  #3  
Old 05-Jul-2006, 10:05
netnut netnut is offline
Member
 
Join Date: Dec 2005
Location: India
Posts: 174
netnut will become famous soon enough

Re: Problems understanding inheritence.


Thanks C. That "is-a" thing is a very good way to solve "what should inherit what" problem.
Now for the library card thing. This is what I understand of your code:

*There should be 2 base classes - SchoolMember & Identification
*Those 2 classes will have derived classes. Which would include student and library card respectively

So I believe, as of now, that student has no relation with a library card (It does not qualify the "is-a" relation). Now what if there is a such a scenario wherein every student is issued a school library card? Wouldn't I require an object of the "card class" inside the student class... isn't that inheritence?

netnut
  #4  
Old 05-Jul-2006, 11:55
cable_guy_67's Avatar
cable_guy_67 cable_guy_67 is offline
Senior Member
 
Join Date: Oct 2004
Location: Nescopeck, PA
Posts: 1,109
cable_guy_67 is a jewel in the roughcable_guy_67 is a jewel in the roughcable_guy_67 is a jewel in the roughcable_guy_67 is a jewel in the rough

Re: Problems understanding inheritence.


Quote:
Originally Posted by netnut
So I believe, as of now, that student has no relation with a library card (It does not qualify the "is-a" relation). Now what if there is a such a scenario wherein every student is issued a school library card? Wouldn't I require an object of the "card class" inside the student class... isn't that inheritence?

As you drift into "has-a" relationships.

Here is some reading to get started at parashift.

Some stuff from Scott Meyers

Mark
__________________
"Opportunity is missed by most people because it comes dressed in overalls and looks like work."
--Thomas Alva Edison
"Those who would give up essential liberty to purchase a little temporary safety, deserve neither liberty nor safety."
--Benjamin Franklin
"A happy person is not a person in a certain set of circumstances, but rather a person with a certain set of attitudes."
--Hugh Downs
  #5  
Old 06-Jul-2006, 06:51
netnut netnut is offline
Member
 
Join Date: Dec 2005
Location: India
Posts: 174
netnut will become famous soon enough

Re: Problems understanding inheritence.


Thanks Mark, those links were excellent. I've bookmarked them for future reference.
The two articles seem to have generated in me a feeling that I have known this topic for long time. Let me try and solve a few problems, and I'll be back if I face any difficulty...

Thanks all..
netnut.
  #6  
Old 08-Jul-2006, 11:21
netnut netnut is offline
Member
 
Join Date: Dec 2005
Location: India
Posts: 174
netnut will become famous soon enough

Re: Problems understanding inheritence.


So after a few reads at the resources, here is what I gather (please correct me if you find anything wrong):

"Public inheritence" -> "Is-A" relationship
"Private inheritence" -> "Has-A" relationship

An employee is (should be) inherited publicly by a manager; "person" should be public for "student".
"Name" should be private for "person" which in turn should be public for a student.

What about protected inheritence and containership? In the real world how is a train related to a ticket? And what is the equivalent implementation in C++?

Thanks in advance.
netnut.
 
 

Recent GIDBlogWriting a book by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Problems while burning CD's netnut Computer Software Forum - Windows 16 18-Jan-2008 00:45
if loop problems bco1109 C++ Forum 13 11-Oct-2005 22:06
Chaintech Geforce 5600 FX problems bartster74 Computer Hardware Forum 8 04-May-2004 14:16
Having problems displaying a line of variable length using winio. warny_maelstrom C Programming Language 8 15-Feb-2004 12:56
CD Burner Problems Joe9995 Computer Hardware Forum 0 07-Feb-2004 11:47

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 16:26.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.