Solved

parents not knowing their children

Posted on 2004-09-06
11
192 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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

785 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