Solved

parents not knowing their children

Posted on 2004-09-06
11
200 Views
Last Modified: 2010-05-18
under current oop paradigms a base class has no idea of the classes that is derived from it which is like saying that a parent has a child but does not know that the child has blue eyes why?
0
Comment
Question by:GrooverMD
11 Comments
 
LVL 23

Expert Comment

by:Mysidia
ID: 11993828
A child class is an extension of  base class.

The base class is more general and doesn't incorporate the specific
elements.

Actually the logic is more like: the child has blue eyes, but the more
general class Lifeform (which the object instance 'humanChild' happens to be
an indirect member of) doesn't include the idea of having eyes.
0
 
LVL 48

Expert Comment

by:AlexFM
ID: 11993852
This is not only OOP issue. Any program should be written by such way, that it's various parts don't know anything (or know minimum) about each other. In this case, when you change some part of the program, other parts remain unchanged.
About base class: suppose that base class knows something about derived classes. This means, it has some specific code for this. Changing of derived classes may require changing of the base class.
Class (parent) doesn't know anything about it's childs, it doesn't know whether it has childs.
0
 
LVL 3

Expert Comment

by:teratoma
ID: 11994041
This is the Liskov Substitution Principle.  A derived object should be easily substuted for a base object.  A class us used in terms of a public interface.  A client uses the class according to this interface.  A derived class must not change the results of using the interface from the client's perspective.  A Square and a Circle might be able to draw themselves, but the Circle can't require extra information about its radius, and the Square can't take control of the screen to optimize the drawing of itself based on its rectangular nature.  Requiring extra information and/or creating undocumented side-effects are not consistent with the base-class interface.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 17

Expert Comment

by:rstaveley
ID: 11994144
I find thinking of a derived class as a "child class" unhelpful.
0
 
LVL 4

Expert Comment

by:Prashant Sabnekar
ID: 11994402
Never think base class and derived class as, blood relations.
In programming we create base classes so that others can reuse it without any information to base class. And there is no point to know how many classes use it.
We can know how many instances has been created for our base class, by putting a static count variable in our base class.

Actually i am a bit busy now, but i will give you a very good answer soon
bye bye
Prashant
0
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 11994895
>> doesn't include the idea of having eyes

I wouldn't agree to that. Baseclasses normally are designed to have a good part of common attributes of all derived classes. So the baseclass of HumanChild is Child or Human or PrimateChild, all of them have eyes.

>> Class (parent) doesn't know anything about it's childs

By defining virtual functions - especially with pure virtual functions - baseclasses can determine what kind of implementations a derived class has to provide.

>> it doesn't know whether it has childs.

That's true, but with pure virtual functions a baseclass or protected/private constructors a baseclass can prevent getting instantiated if not derived.

Regards, Alex



0
 
LVL 9

Accepted Solution

by:
jhshukla earned 125 total points
ID: 11996513
> A child class is an extension of  base class.
> The base class is more general and doesn't incorporate the specific elements.
True.

A base class has a general functionality. for example when you are dealing with employees in an organization you would have classes like
Employee
tempEmployee
permEmployee
mgmtEmployee

1. mgmtEmployee has some added functionality that all employees don't have.
2. all employees are either temp or permanent. so you can think of permEmployee an tempEmployee as siblings with different propeties

and about your question "why?": so that a class can perform all functions independently without needing extraneous info.
In case, (this is occasional but not rare) that
1. you want to use a child class in parent class OR
2. have two classes dependent on each other
you can use this construct:

FILE: class1.h

#include "class2.h"

class class1{
...
};

FILE: class2.h

class class1;

class class2{
};
0
 
LVL 23

Expert Comment

by:Mysidia
ID: 12304292
this question was answered several times over in the comments

I think  jhshukla  answered it most thoroughly and precisely
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

680 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question