473,700 Members | 2,648 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Object creation - Do we really need to create a parent for a derieved object - can't the base object just point to an already created base object

OK!
I had this nagging doubt

Consider (without worrying abt access specifiers)
class Kid : public Parent{...};

Parent::someFun c()
{
Kid k;
}
ISSUE- this new Kid , while constructing it, can't I make the
parent of this new kid point to the already existing parent. DO we
REALLY NEED to create a Parent each time a Kid object is created.

Thanks a lot!
JON

Sep 21 '05 #1
9 2075
* jon wayne:
OK!
I had this nagging doubt

Consider (without worrying abt access specifiers)
class Kid : public Parent{...};
This says that a Kid is always a Parent, from the moment it's born.

Parent::someFun c()
{
Kid k;
}
ISSUE- this new Kid , while constructing it, can't I make the
parent of this new kid point to the already existing parent. DO we
REALLY NEED to create a Parent each time a Kid object is created.


You have said, via the inheritance, that every Kid has a Parent base class
sub-object.

But probably the inheritance is plain wrong, otherwise you wouldn't ask.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Sep 21 '05 #2
jon wayne wrote:
OK!
I had this nagging doubt

Consider (without worrying abt access specifiers)
class Kid : public Parent{...};

Parent::someFun c()
{
Kid k;
}
ISSUE- this new Kid , while constructing it, can't I make the
parent of this new kid point to the already existing parent.
No.

DO we REALLY NEED to create a Parent each time a Kid object is created.


Yes.

If these facts are wrong for your program, then you shouldn't be using
inheritance.

john
Sep 21 '05 #3
jon wayne wrote:
OK!
I had this nagging doubt

Consider (without worrying abt access specifiers)
class Kid : public Parent{...};

Parent::someFun c()
{
Kid k;
}
ISSUE- this new Kid , while constructing it, can't I make the
parent of this new kid point to the already existing parent. DO we
REALLY NEED to create a Parent each time a Kid object is created.


I think you need a different design. Yours says that a Kid is also a Parent.
Here's a suggestion:

class Person
{
// members common to all people
};

class Kid : public Person
{
// members common to just Kids
// including possibly pointers to Parents
};

class Parent : public Person
{
// members common to just parents
std::vector<Kid > children; // (or vector<Kid*>) the parent's children
};

This still isn't right, assuming you are trying to model human families,
because _two_ parents share the same children, but you get the idea.

DW
Sep 21 '05 #4
David White wrote:
jon wayne wrote:
OK!
I had this nagging doubt

Consider (without worrying abt access specifiers)
class Kid : public Parent{...};

Parent::someFun c()
{
Kid k;
}
ISSUE- this new Kid , while constructing it, can't I make the
parent of this new kid point to the already existing parent. DO we
REALLY NEED to create a Parent each time a Kid object is created.

I think you need a different design. Yours says that a Kid is also a Parent.
Here's a suggestion:

class Person
{
// members common to all people
};

class Kid : public Person
{
// members common to just Kids
// including possibly pointers to Parents
};

class Parent : public Person
{
// members common to just parents
std::vector<Kid > children; // (or vector<Kid*>) the parent's children
};

This still isn't right, assuming you are trying to model human families,
because _two_ parents share the same children, but you get the idea.

DW


I felt that the OP was just using Kid and Parent as role describing
names, rather like people use Base and Derived. But yes if he *really*
has a class called Kid derived from a class called Parent, then
something is seriously wrong.

john
Sep 21 '05 #5

"jon wayne" <jo**********@g mail.com> wrote in message
news:11******** **************@ g44g2000cwa.goo glegroups.com.. .
OK!
I had this nagging doubt

Consider (without worrying abt access specifiers)
class Kid : public Parent{...};

Parent::someFun c()
{
Kid k;
}
ISSUE- this new Kid , while constructing it, can't I make the
parent of this new kid point to the already existing parent. DO we
REALLY NEED to create a Parent each time a Kid object is created.

Thanks a lot!
JON


I think the OP is mistaking an object-level relationship with class-level
relationship. What the OP wants to model, is the relationship between a kid
and a parent, which, naturally, is an object-level relationship. This
relationship can be built up on object reference in general, or pointers to
objects in C++:

class Person
{
public:
Person* parent1;
Person* parent2;
// ...
};

Person Merlin;
Person Amily;
Person Oliver;

// Oliver is the son of Merlin and Amily:
Oliver.parent1 = &Merlin;
Oliver.parent2 = &Amily;

The OP takes the fact that a human kid usually inherits from his/her
parent(s) and try to use C++ inheritance to model at class level, which is
the wrong anology. In C++, a derived class is-a and includes a base class.
For example, an apple is a fruit, and an apple includes everything you can
imagine in a fruit, so you can use an apple in anyway you can use a fruit.

A kid is not (necessarily) a parent. You certainly should not, and cannot,
treat your kid exactly the same way you treat your parent. A parental
relationship is down to individuals, and is an object-level relationship.

Ben
Sep 21 '05 #6
"John Harrison" <jo************ *@hotmail.com> wrote in message
news:If******** ********@newsfe 1-gui.ntli.net...
David White wrote:
I think you need a different design. Yours says that a Kid is also a Parent. Here's a suggestion:

[snip]
I felt that the OP was just using Kid and Parent as role describing
names, rather like people use Base and Derived. But yes if he *really*
has a class called Kid derived from a class called Parent, then
something is seriously wrong.


I thought the use of the name 'Kid', rather than 'Child', along with the
implication that the parent already exists, suggested that the names were
meant to be taken literally. Anyway, the OP can clarify if necessary.

DW

Sep 21 '05 #7

"jon wayne" <jo**********@g mail.com> wrote in message
news:11******** **************@ g44g2000cwa.goo glegroups.com.. .
OK!
I had this nagging doubt

Consider (without worrying abt access specifiers)
class Kid : public Parent{...};

Parent::someFun c()
{
Kid k;
}
ISSUE- this new Kid , while constructing it, can't I make the
parent of this new kid point to the already existing parent.
What do you mean by "parent of"? Using class names which are the same as
concepts (such as parent, meaning the specific class "Parent", as well as
the generic meaning "base class", sometimes also called "parent class"),
makes it difficult to determine what you're referring to.

There are two kinds of parent-child relationships (disregarding your class
names for the moment). One is inheritence, which you've shown, where some
people refer to the base class as the parent class.

There is also the "tree" relationship, where a "parent node" contains
pointers to one or more "child nodes", and a "child node" often contains a
pointer to its "parent node". If that's what you're talking about, then you
should not be using inheritence here. So I'll assume you're talking about
inheritence...

The Kid class has no "pointer" to a "parent", at least not in the code
you've shown. It is derived from Parent, and can be considered to actually
_be_ a Parent in this case (since you've used public inheritence).

When you create a Kid object, memory is allocated for a Kid object. Then,
the constructor for the Parent class initializes that part of the new Kid
object which is declared within the Parent class. Then, the constructor for
the Kid class is executed, initializing any members which were declared in
the Kid class itself.

So you personally don't create a Parent object, and then create a Kid
object. If you want a Kid object, then you just create one. Because you've
defined Kid as a class publicly derived from Parent, a Kid object can also
be treated as a Parent object (if you're using a reference or pointer
variable). That's called polymorphism. But there's no "pointer" to a
Parent object inside the Kid object. It's just part of the Kid object.
DO we
REALLY NEED to create a Parent each time a Kid object is created.


You don't do that, the compiler does that. You've declared Kid as being
derived from Parent, so every member that is declared within the Parent
class is _automatically_ part of a Kid object as well.

But, if you really _want_ a pointer to a Parent object inside your Kid
object, then you're talking about a whole different subject, and you
probably didn't want to use inheritence in the first place.

-Howard


Sep 21 '05 #8
On Wed, 21 Sep 2005 05:28:18 GMT, al***@start.no (Alf P. Steinbach)
wrote:
Consider (without worrying abt access specifiers)
class Kid : public Parent{...};


This says that a Kid is always a Parent, from the moment it's born.


Well, actually the Kid is first a Parent, even before it is born <g>
-- the base class is always constructed first.

--
Bob Hairgrove
No**********@Ho me.com
Sep 21 '05 #9
"jon wayne" <jo**********@g mail.com> wrote in message
news:11******** **************@ g44g2000cwa.goo glegroups.com.. .
OK!
I had this nagging doubt

Consider (without worrying abt access specifiers)
class Kid : public Parent{...};

Parent::someFun c()
{
Kid k;
}
ISSUE- this new Kid , while constructing it, can't I make the
parent of this new kid point to the already existing parent. DO we
REALLY NEED to create a Parent each time a Kid object is created.


You are saying a Kid IS A parent (which he will eventually be). But I think
you also want to know the kids parents themselves. So there are, in fact, 3
parents. The kid (who is a parent) his father and his mother. Which gets
compounded by the fact that his father was a kid...

You need to think about your heirarchy. Why do you have a kid class in the
first place? Arent' they both, in fact, both kids and parents?

So something like this:

class Person
{
private:
Person* Father;
Person* Mother;

std::vector<Per son*> Children;

public:
Person(Person* father, Person* mother): Father(father), Mother(mother)
{};
AddChild(Person * child) { Children.push_b ack(child); };
}

Now you have within each person a pointer to their father, their mother, and
potential children.

Because, in your stated scenario, not only is a Kid a Parent, but a Parent
is a Kid, hence, they are the same thing.

I use pointers because I'm used to them. I presume you can do the same
thing with references, just not positive on the syntax.
Sep 22 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

106
5558
by: A | last post by:
Hi, I have always been taught to use an inialization list for initialising data members of a class. I realize that initialsizing primitives and pointers use an inialization list is exactly the same as an assignment, but for class types it has a different effect - it calls the copy constructor. My question is when to not use an initalisation list for initialising data members of a class?
6
2297
by: Busin | last post by:
Classes B, C, D and E are derived from base class A. A* CreateB(); A* CreateC(); A* CreateD(); A* CreateE(); class X { public:
6
7961
by: Squeamz | last post by:
Hello, Say I create a class ("Child") that inherits from another class ("Parent"). Parent's destructor is not virtual. Is there a way I can prevent Parent's destructor from being called when a Child object goes out of scope? Specifically, I am dealing with a C library that provides a function that must be called to "destruct" a particular struct (this struct is dynamically allocated by another provided function). To avoid memory
13
7406
by: Stuart McGraw | last post by:
I haven't been able to figure this out and would appreciate some help... I have two tables, both with autonumber primary keys, and linked in a conventional master-child relationship. I've created forms for both those tables, and inserted the child table form into the master table form as a subform. It works just as it is supposed to, in that I can create a new master record, and then add detail records.
11
3835
by: Kevin Prichard | last post by:
Hi all, I've recently been following the object-oriented techiques discussed here and have been testing them for use in a web application. There is problem that I'd like to discuss with you experts. I would like to produce Javascript classes that can be "subclassed" with certain behaviors defined at subclass time. There are plenty of ways to do this through prototyping and other techniques, but these behaviors need to be static and...
15
1921
by: Sam Kong | last post by:
Hello! I got recently intrigued with JavaScript's prototype-based object-orientation. However, I still don't understand the mechanism clearly. What's the difference between the following two? (1)
22
4183
by: Cylix | last post by:
I have a 4row x 1col table, I would like to drop all the content of row three. Since Mac IE5.2 does not suppport deleteRow method, I have also try to set the innerHTML=''; but it does not work. How can I delete the node from DOM in other way? Thanks.
9
5060
by: koschwitz | last post by:
Hi, I hope you guys can help me make this simple application work. I'm trying to create a form displaying 3 circles, which independently change colors 3 times after a random time period has passed. I'm struggling with making the delegate/invoke thing work, as I know GUI objects aren't thread-safe. I don't quite understand the concept I'm supposed to use to modify the GUI thread-safe. Below is my form and my Circle class. Currently,...
23
3137
by: tonytech08 | last post by:
What I like about the C++ object model: that the data portion of the class IS the object (dereferencing an object gets you the data of a POD object). What I don't like about the C++ object model: that most OO features are not available for class object design without loss of POD-ness. So, I'm more than leaning toward "bad" because of the limitations and
0
8725
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, weíll explore What is ONU, What Is Router, ONU & Routerís main usage, and What is the difference between ONU and Router. Letís take a closer look ! Part I. Meaning of...
0
8644
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9074
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8970
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
5902
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4403
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4656
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2392
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2027
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.