473,385 Members | 1,597 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Inheritance problems with constructor

Hi,

if I create a Person, a Vehicle is created; that's fine.
The problem is that if I create a Child the constuctor of Person is called
before and a Vehicle is created then a Bike is created and the variable is
overwritten. How can I prevent this?
class Person
{
protected Vehicle fVehicle;

public Person()
{
fVehicle = new Vehicle();
}
}

class Child : Person
{
public Child()
{
fVehicle = new Bike();
}
}

class Vehicle
{}

class Bike : Vehicle
{}
Aug 18 '06 #1
3 1575

Julius Fuchs wrote:
Hi,

if I create a Person, a Vehicle is created; that's fine.
The problem is that if I create a Child the constuctor of Person is called
before and a Vehicle is created then a Bike is created and the variable is
overwritten. How can I prevent this?
class Person
{
protected Vehicle fVehicle;

public Person()
{
fVehicle = new Vehicle();
}
}

class Child : Person
{
public Child()
{
fVehicle = new Bike();
}
}

class Vehicle
{}

class Bike : Vehicle
{}
Perhaps the problem is that you don't want to create a (generic)
vehicle for a Person. What you want, I think, this:

class Person
class Adult : Person
class Child : Person
class Vehicle
class Car : Vehicle
class Bike : Vehicle

Person does not allocate a vehicle, whereas Adult creates a Car and
Child creates a Bike.

Another possibility is to supply an alternate constructor:

public Person() : this(new Vehicle)
{ }

protected Person(Vehicle v)
{
fVehicle = v;
}

then

public Child() : base(new Bike())
{
...
}

Aug 18 '06 #2
Hi,
Another possibility is to supply an alternate constructor:[...]
Thank you very much! That's what I was looking for, very elegant!
Aug 18 '06 #3
Hi,

With your current struct you cannot avoid the creation of the Vehicle in the
parent class. Note that usually this has not further effect than just
another instance to be GCed.

Now, a more elegant solution could be achieve by changing a little the
constructors.

Person:
protected Person( Vehicle v){ fVehicle = v;}
public Person():this( new Vehicle){}

Child:
public Child(): base( new Bycicle() ){}

Note that if this would have been in any other case except in the
constructors a virtual method will solve the situation.
--
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
"Julius Fuchs" <ju**********@yahoo.dewrote in message
news:1a*****************************@40tude.net...
Hi,

if I create a Person, a Vehicle is created; that's fine.
The problem is that if I create a Child the constuctor of Person is called
before and a Vehicle is created then a Bike is created and the variable is
overwritten. How can I prevent this?
class Person
{
protected Vehicle fVehicle;

public Person()
{
fVehicle = new Vehicle();
}
}

class Child : Person
{
public Child()
{
fVehicle = new Bike();
}
}

class Vehicle
{}

class Bike : Vehicle
{}

Aug 18 '06 #4

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

Similar topics

3
by: comp.lang.php | last post by:
I'm having a serious problem in my application I simply cannot seem to fix, and it has to do with multiple inheritances whereby something is lost. Class EditView is a child of PagOptionsView...
5
by: Christian Meier | last post by:
Hi dear programmers I looked for the difference between private and protected inheritance, but couldn't find anything. Here is my sample code: #include <iostream> using std::cout; using...
5
by: Tony Johansson | last post by:
Hello! If you have the following inheritance A is the base class for the derived class B and B is the base class for the derived class C. In this case will the constructor of class C be executed...
22
by: Matthew Louden | last post by:
I want to know why C# doesnt support multiple inheritance? But why we can inherit multiple interfaces instead? I know this is the rule, but I dont understand why. Can anyone give me some concrete...
45
by: Ben Blank | last post by:
I'm writing a family of classes which all inherit most of their methods and code (including constructors) from a single base class. When attempting to instance one of the derived classes using...
14
by: Bruno van Dooren | last post by:
Hi all, i am having a problems with inheritance. consider the following: class A { public: A(int i){;} };
31
by: John W. Kennedy | last post by:
I quite understand about prototypes and not having classes as such, but I happen to have a problem involving blatant is-a relationships, such that inheritance is the bloody obvious way to go. I can...
2
by: beseecher | last post by:
Hi, In my research in the javascript language I have encountered problems with implementing prototype inheritance while preserving private methods functioning properly. Here is an example: ...
7
by: Adam Nielsen | last post by:
Hi everyone, I'm having some trouble getting the correct chain of constructors to be called when creating an object at the bottom of a hierarchy. Have a look at the code below - the inheritance...
2
by: Mahesh | last post by:
Hi, I encounted some problems using GDB with classes that use virtual inheritance. To illustrate this issue, I have created a simple test case. Here it goes. #include <iostream>
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...

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.