473,382 Members | 1,420 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,382 software developers and data experts.

Initializing base portion of a derived class

(Please disregard the previous message; I accidentally sent it before it was
completed.)

I have source code similar to the following.

public class Vehicle
{
protected string dataV;
// ... more protected fields
}

public class FourWheelVehicle : Vehicle
{
protected string dataFWV;
// ... more protected fields
}

public class SuvVehicle : FourWheelVehicle
{
protected string dataSUV;
// ... more protected fields
}

Here's my problem. The number of fields in each class is large; some are
value types and some are reference types. When I write constructors for
FourWheelVehicle and SuvVehicle, I'll be writing a lot of tedious copying,
for instance, something like:

// default constructor
public FourWheelVehicle()
{
this.dataV = "defaultV";
// initialization of other base class protected fields
this.dataFWV = "defaultFWV";
// initialization of other derived class protected fields
}

It seems like it'd be a lot easier to write something like this:

// copy constructor for "base"-sliced part of derived class
public FourWheelVehicle(Vehicle v)
{
base = v.Clone(); // oops! illegal
this.dataFWV = "defaultFWV";
// initialization of other derived class protected fields
}

Even if I could do that, I'm not sure what to do about the further derived
classes, like SuvVehicle and further derived classes. Perhaps something
like:

public SuvVehicle(FourWheelVehicle fwv)
{
base = v.Clone(); // (still illegal)
this.dataSUV = "defaultSUV";
// initialization of other derived class protected fields
}

Might it be ultimately easier to aggregate all the fields into an instance
of a protected object in the class (e.g. VehicleData), or is this overkill?
This would let me do something like:

public FourWheelVehicle(Vehicle v)
{
this.VehicleData = v.VehicleData.Clone();
this.FourWheelVehicleData = new FourWheelVehicleData();
}

This isolates the initialization code to the Clone() method and the
constructor of the data-encapsulation class, rather than scattering it over
all the constructors. But the creation of an additional class solely to hold
data seems like it's just adding unnecessary complication/abstraction.

It seems like there should be a simple solution to this, but that's probably
because I haven't thought about it enough yet. I'd appreciate any input that
a more experienced developer could give. Thanks!

Sincerely,
JJ Feminella
Computer Science/Economics Undergraduate
University of Virginia | School of Engineering and Applied Science
Nov 16 '05 #1
3 3454
public class Vehicle {
protected string dataV;
public Vehicle() {
this.dataV = @"DataV";
}
}

public class FourWheelVehicle : Vehicle {
protected string dataFWV;
public FourWheelVehicle() : base() {
this.dataFWV = @"DataFWV";
}
}

public class SportsUtilityVehicle : FourWheelVehicle {
protected string dataSUV;
public SportsUtilityVehicle() : base() {
this.dataSUV = @"DataSUV";
}
}

within each instance of the SportsUtilityVehicle class, the following values
will be set
dataV = @"DataV"
dataFWV = @"DataFWV"
dataSUV = @"DataSUV"

You do not really need to add the " : base()" construct to do this either;
it simply helps readability - the default constructors for each class are
setting the values. As each class is instantiated, it creates an instance of
its base class using default constructors anyway.

HTH.
Martin.
"J.J. Feminella" <(substitute as necessary) | jjf2k at virginia dot edu>
wrote in message news:%2****************@TK2MSFTNGP11.phx.gbl...
(Please disregard the previous message; I accidentally sent it before it was completed.)

I have source code similar to the following.

public class Vehicle
{
protected string dataV;
// ... more protected fields
}

public class FourWheelVehicle : Vehicle
{
protected string dataFWV;
// ... more protected fields
}

public class SuvVehicle : FourWheelVehicle
{
protected string dataSUV;
// ... more protected fields
}

Here's my problem. The number of fields in each class is large; some are
value types and some are reference types. When I write constructors for
FourWheelVehicle and SuvVehicle, I'll be writing a lot of tedious copying,
for instance, something like:

// default constructor
public FourWheelVehicle()
{
this.dataV = "defaultV";
// initialization of other base class protected fields
this.dataFWV = "defaultFWV";
// initialization of other derived class protected fields
}

It seems like it'd be a lot easier to write something like this:

// copy constructor for "base"-sliced part of derived class
public FourWheelVehicle(Vehicle v)
{
base = v.Clone(); // oops! illegal
this.dataFWV = "defaultFWV";
// initialization of other derived class protected fields
}

Even if I could do that, I'm not sure what to do about the further derived
classes, like SuvVehicle and further derived classes. Perhaps something
like:

public SuvVehicle(FourWheelVehicle fwv)
{
base = v.Clone(); // (still illegal)
this.dataSUV = "defaultSUV";
// initialization of other derived class protected fields
}

Might it be ultimately easier to aggregate all the fields into an instance
of a protected object in the class (e.g. VehicleData), or is this overkill? This would let me do something like:

public FourWheelVehicle(Vehicle v)
{
this.VehicleData = v.VehicleData.Clone();
this.FourWheelVehicleData = new FourWheelVehicleData();
}

This isolates the initialization code to the Clone() method and the
constructor of the data-encapsulation class, rather than scattering it over all the constructors. But the creation of an additional class solely to hold data seems like it's just adding unnecessary complication/abstraction.

It seems like there should be a simple solution to this, but that's probably because I haven't thought about it enough yet. I'd appreciate any input that a more experienced developer could give. Thanks!

Sincerely,
JJ Feminella
Computer Science/Economics Undergraduate
University of Virginia | School of Engineering and Applied Science

Nov 16 '05 #2
Martin,

Thanks for responding. I don't think my original question was very clear, I
got a little bogged down in providing examples of what I was trying to do.
What I wanted to do was to give myself a way, through constructors, of
instantiating multiple derived instances whose base portions were the same.
But I can't figure out a good way to slice off the base portion and factor
that out separately, then assign the appropriate derived class fields. It
seems like I have to make an entirely new set of assignments and some kind
of convoluted copy constructor to do that.

Also, my actual "Vehicle" and derived classes have many different fields. It
seems like it'd be tedious to write a lot of assignment statements, which is
why I wrestled with the idea of encapsulating all the data into an
aggregation class, e.g. VehicleData, etc., although this seems like an
unnecessary complication. This would certainly simplify assignment, though;
it'd only take a simple "this.VehicleData = new VehicleData();" to
instantiate and it'd keep my constructors tidy. I'd appreciate thoughts on
this route as well.

Best wishes,
JJ

"Martin Robins" <martin - robins @ ntlworld dot com> wrote in message
news:#f**************@TK2MSFTNGP12.phx.gbl...
public class Vehicle {
protected string dataV;
public Vehicle() {
this.dataV = @"DataV";
}
}

public class FourWheelVehicle : Vehicle {
protected string dataFWV;
public FourWheelVehicle() : base() {
this.dataFWV = @"DataFWV";
}
}

public class SportsUtilityVehicle : FourWheelVehicle {
protected string dataSUV;
public SportsUtilityVehicle() : base() {
this.dataSUV = @"DataSUV";
}
}

within each instance of the SportsUtilityVehicle class, the following values will be set
dataV = @"DataV"
dataFWV = @"DataFWV"
dataSUV = @"DataSUV"

You do not really need to add the " : base()" construct to do this either;
it simply helps readability - the default constructors for each class are
setting the values. As each class is instantiated, it creates an instance of its base class using default constructors anyway.

HTH.
Martin.
"J.J. Feminella" <(substitute as necessary) | jjf2k at virginia dot edu>
wrote in message news:%2****************@TK2MSFTNGP11.phx.gbl...
(Please disregard the previous message; I accidentally sent it before it

was
completed.)

I have source code similar to the following.

public class Vehicle
{
protected string dataV;
// ... more protected fields
}

public class FourWheelVehicle : Vehicle
{
protected string dataFWV;
// ... more protected fields
}

public class SuvVehicle : FourWheelVehicle
{
protected string dataSUV;
// ... more protected fields
}

Here's my problem. The number of fields in each class is large; some are
value types and some are reference types. When I write constructors for
FourWheelVehicle and SuvVehicle, I'll be writing a lot of tedious copying, for instance, something like:

// default constructor
public FourWheelVehicle()
{
this.dataV = "defaultV";
// initialization of other base class protected fields
this.dataFWV = "defaultFWV";
// initialization of other derived class protected fields
}

It seems like it'd be a lot easier to write something like this:

// copy constructor for "base"-sliced part of derived class
public FourWheelVehicle(Vehicle v)
{
base = v.Clone(); // oops! illegal
this.dataFWV = "defaultFWV";
// initialization of other derived class protected fields
}

Even if I could do that, I'm not sure what to do about the further derived classes, like SuvVehicle and further derived classes. Perhaps something
like:

public SuvVehicle(FourWheelVehicle fwv)
{
base = v.Clone(); // (still illegal)
this.dataSUV = "defaultSUV";
// initialization of other derived class protected fields
}

Might it be ultimately easier to aggregate all the fields into an instance of a protected object in the class (e.g. VehicleData), or is this

overkill?
This would let me do something like:

public FourWheelVehicle(Vehicle v)
{
this.VehicleData = v.VehicleData.Clone();
this.FourWheelVehicleData = new FourWheelVehicleData();
}

This isolates the initialization code to the Clone() method and the
constructor of the data-encapsulation class, rather than scattering it

over
all the constructors. But the creation of an additional class solely to

hold
data seems like it's just adding unnecessary complication/abstraction.

It seems like there should be a simple solution to this, but that's

probably
because I haven't thought about it enough yet. I'd appreciate any input

that
a more experienced developer could give. Thanks!

Sincerely,
JJ Feminella
Computer Science/Economics Undergraduate
University of Virginia | School of Engineering and Applied Science


Nov 16 '05 #3


How about a "copy constructor" in the base class to initialize the base
fields? Then the derived class constructors could also have copy
constructors that call the base constructor.

Or do I understand that you're looking for some way to have multiple
instances "derive" from another instance? In that case, you could use
containment rather than subclassing to minimize storage.
"J.J. Feminella" <(substitute as necessary) | jjf2k at virginia dot edu>
wrote in message news:%2****************@TK2MSFTNGP12.phx.gbl...
Martin,

Thanks for responding. I don't think my original question was very clear, I got a little bogged down in providing examples of what I was trying to do.
What I wanted to do was to give myself a way, through constructors, of
instantiating multiple derived instances whose base portions were the same. But I can't figure out a good way to slice off the base portion and factor
that out separately, then assign the appropriate derived class fields. It
seems like I have to make an entirely new set of assignments and some kind
of convoluted copy constructor to do that.

Also, my actual "Vehicle" and derived classes have many different fields. It seems like it'd be tedious to write a lot of assignment statements, which is why I wrestled with the idea of encapsulating all the data into an
aggregation class, e.g. VehicleData, etc., although this seems like an
unnecessary complication. This would certainly simplify assignment, though; it'd only take a simple "this.VehicleData = new VehicleData();" to
instantiate and it'd keep my constructors tidy. I'd appreciate thoughts on
this route as well.

Best wishes,
JJ

"Martin Robins" <martin - robins @ ntlworld dot com> wrote in message
news:#f**************@TK2MSFTNGP12.phx.gbl...
public class Vehicle {
protected string dataV;
public Vehicle() {
this.dataV = @"DataV";
}
}

public class FourWheelVehicle : Vehicle {
protected string dataFWV;
public FourWheelVehicle() : base() {
this.dataFWV = @"DataFWV";
}
}

public class SportsUtilityVehicle : FourWheelVehicle {
protected string dataSUV;
public SportsUtilityVehicle() : base() {
this.dataSUV = @"DataSUV";
}
}

within each instance of the SportsUtilityVehicle class, the following values
will be set
dataV = @"DataV"
dataFWV = @"DataFWV"
dataSUV = @"DataSUV"

You do not really need to add the " : base()" construct to do this either;
it simply helps readability - the default constructors for each class are setting the values. As each class is instantiated, it creates an instance of
its base class using default constructors anyway.

HTH.
Martin.
"J.J. Feminella" <(substitute as necessary) | jjf2k at virginia dot edu>
wrote in message news:%2****************@TK2MSFTNGP11.phx.gbl...
(Please disregard the previous message; I accidentally sent it before it was
completed.)

I have source code similar to the following.

public class Vehicle
{
protected string dataV;
// ... more protected fields
}

public class FourWheelVehicle : Vehicle
{
protected string dataFWV;
// ... more protected fields
}

public class SuvVehicle : FourWheelVehicle
{
protected string dataSUV;
// ... more protected fields
}

Here's my problem. The number of fields in each class is large; some
are value types and some are reference types. When I write constructors for FourWheelVehicle and SuvVehicle, I'll be writing a lot of tedious

copying, for instance, something like:

// default constructor
public FourWheelVehicle()
{
this.dataV = "defaultV";
// initialization of other base class protected fields
this.dataFWV = "defaultFWV";
// initialization of other derived class protected fields
}

It seems like it'd be a lot easier to write something like this:

// copy constructor for "base"-sliced part of derived class
public FourWheelVehicle(Vehicle v)
{
base = v.Clone(); // oops! illegal
this.dataFWV = "defaultFWV";
// initialization of other derived class protected fields
}

Even if I could do that, I'm not sure what to do about the further derived classes, like SuvVehicle and further derived classes. Perhaps something like:

public SuvVehicle(FourWheelVehicle fwv)
{
base = v.Clone(); // (still illegal)
this.dataSUV = "defaultSUV";
// initialization of other derived class protected fields
}

Might it be ultimately easier to aggregate all the fields into an instance of a protected object in the class (e.g. VehicleData), or is this

overkill?
This would let me do something like:

public FourWheelVehicle(Vehicle v)
{
this.VehicleData = v.VehicleData.Clone();
this.FourWheelVehicleData = new FourWheelVehicleData();
}

This isolates the initialization code to the Clone() method and the
constructor of the data-encapsulation class, rather than scattering it

over
all the constructors. But the creation of an additional class solely

to hold
data seems like it's just adding unnecessary complication/abstraction.

It seems like there should be a simple solution to this, but that's

probably
because I haven't thought about it enough yet. I'd appreciate any
input that
a more experienced developer could give. Thanks!

Sincerely,
JJ Feminella
Computer Science/Economics Undergraduate
University of Virginia | School of Engineering and Applied Science



Nov 16 '05 #4

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

Similar topics

50
by: Dan Perl | last post by:
There is something with initializing mutable class attributes that I am struggling with. I'll use an example to explain: class Father: attr1=None # this is OK attr2= # this is wrong...
8
by: Dave | last post by:
class base { public: base(const base &other) { // Init. here... } // Stuff }; class derived: public base { public:
7
by: Douglas Peterson | last post by:
Take a look at this code, it looks funny as its written to be as short as possible: -- code -- struct Base { ~Base() { *((char*)0) = 0; } }; struct Derived : public Base
27
by: tuvok | last post by:
Is it correct that the virtual dtor of base gets called implicitly? Here's some code to demonstrate what I mean: Class B has a virtual destructor, so has class D which is derived from B. Deleting...
3
by: J.J. Feminella | last post by:
(Please disregard the previous message; I accidentally sent it before it was completed.) I have source code similar to the following. public class Vehicle { protected string dataV; // ......
11
by: anongroupaccount | last post by:
What measures should be taken to avoid this sort of thing? class Base { }; class Derived1 : public Base { private: int i, j, k;
1
by: archana | last post by:
Hi all, I am confuse in concept of inheritance. I am having following 2 classes class base1 { public void abc() { System.Console.WriteLine("base1 abc"); }
12
by: siddhu | last post by:
Dear experts, #include <stdio.h> class Base { }; class Der1:public Base {
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.