Solved

parents not knowing their children

Posted on 2004-09-06
11
186 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
 
LVL 17

Expert Comment

by:rstaveley
ID: 11994144
I find thinking of a derived class as a "child class" unhelpful.
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 3

Expert Comment

by:prashantsabnekar
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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 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.

758 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now