473,765 Members | 2,061 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1592

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**********@y ahoo.dewrote in message
news:1a******** *************** ******@40tude.n et...
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
1470
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 Class Addview is a child of AssocView EditView's constructor will call a "super()" onto PagOptionsView as so: parent::PagOptionsView($id, $errorArray); EditView's constructor will next instantiate an AddView object for
5
6434
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 std::endl;
5
1688
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 first then the constructor of class B and finally the constructor of class A. Now to my question. When you have inheritance do you use the assignemt operator anything?
22
23383
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 examples?
45
6362
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 parameters, I get CS1501 (no method with X arguments). Here's a simplified example which mimics the circumstances: namespace InheritError { // Random base class. public class A { protected int i;
14
1926
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
1943
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 think of several ad-hoc ways to achieve inheritance, but is there an accepted standard idiom? -- John W. Kennedy "But now is a new thing which is very old-- that the rich make themselves richer and not poorer, which is the true Gospel, for...
2
2097
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: function A(){ var _this = this; this.a = 10;
7
3740
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 goes like this: Shape | +-- Ellipse | +-- Circle
2
2589
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
9404
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
10168
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9837
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8833
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7381
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5423
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3929
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3532
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2806
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.