(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 FourWheelVehicl e : Vehicle
{
protected string dataFWV;
// ... more protected fields
}
public class SuvVehicle : FourWheelVehicl e
{
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
FourWheelVehicl e and SuvVehicle, I'll be writing a lot of tedious copying,
for instance, something like:
// default constructor
public FourWheelVehicl e()
{
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 FourWheelVehicl e(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(Four WheelVehicle 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 FourWheelVehicl e(Vehicle v)
{
this.VehicleDat a = v.VehicleData.C lone();
this.FourWheelV ehicleData = new FourWheelVehicl eData();
}
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 3 3475
public class Vehicle {
protected string dataV;
public Vehicle() {
this.dataV = @"DataV";
}
}
public class FourWheelVehicl e : Vehicle {
protected string dataFWV;
public FourWheelVehicl e() : base() {
this.dataFWV = @"DataFWV";
}
}
public class SportsUtilityVe hicle : FourWheelVehicl e {
protected string dataSUV;
public SportsUtilityVe hicle() : base() {
this.dataSUV = @"DataSUV";
}
}
within each instance of the SportsUtilityVe hicle 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******** ********@TK2MSF TNGP11.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 FourWheelVehicl e : Vehicle { protected string dataFWV; // ... more protected fields }
public class SuvVehicle : FourWheelVehicl e { 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 FourWheelVehicl e and SuvVehicle, I'll be writing a lot of tedious copying, for instance, something like:
// default constructor public FourWheelVehicl e() { 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 FourWheelVehicl e(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(Four WheelVehicle 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 FourWheelVehicl e(Vehicle v) { this.VehicleDat a = v.VehicleData.C lone(); this.FourWheelV ehicleData = new FourWheelVehicl eData(); }
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
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.VehicleDa ta = 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******** ******@TK2MSFTN GP12.phx.gbl... public class Vehicle { protected string dataV; public Vehicle() { this.dataV = @"DataV"; } }
public class FourWheelVehicl e : Vehicle { protected string dataFWV; public FourWheelVehicl e() : base() { this.dataFWV = @"DataFWV"; } }
public class SportsUtilityVe hicle : FourWheelVehicl e { protected string dataSUV; public SportsUtilityVe hicle() : base() { this.dataSUV = @"DataSUV"; } }
within each instance of the SportsUtilityVe hicle 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******** ********@TK2MSF TNGP11.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 FourWheelVehicl e : Vehicle { protected string dataFWV; // ... more protected fields }
public class SuvVehicle : FourWheelVehicl e { 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 FourWheelVehicl e and SuvVehicle, I'll be writing a lot of tedious
copying, for instance, something like:
// default constructor public FourWheelVehicl e() { 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 FourWheelVehicl e(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(Four WheelVehicle 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 FourWheelVehicl e(Vehicle v) { this.VehicleDat a = v.VehicleData.C lone(); this.FourWheelV ehicleData = new FourWheelVehicl eData(); }
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
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******** ********@TK2MSF TNGP12.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.VehicleDa ta = 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******** ******@TK2MSFTN GP12.phx.gbl... public class Vehicle { protected string dataV; public Vehicle() { this.dataV = @"DataV"; } }
public class FourWheelVehicl e : Vehicle { protected string dataFWV; public FourWheelVehicl e() : base() { this.dataFWV = @"DataFWV"; } }
public class SportsUtilityVe hicle : FourWheelVehicl e { protected string dataSUV; public SportsUtilityVe hicle() : base() { this.dataSUV = @"DataSUV"; } }
within each instance of the SportsUtilityVe hicle 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******** ********@TK2MSF TNGP11.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 FourWheelVehicl e : Vehicle { protected string dataFWV; // ... more protected fields }
public class SuvVehicle : FourWheelVehicl e { 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 FourWheelVehicl e and SuvVehicle, I'll be writing a lot of tedious copying, for instance, something like:
// default constructor public FourWheelVehicl e() { 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 FourWheelVehicl e(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(Four WheelVehicle 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 FourWheelVehicl e(Vehicle v) { this.VehicleDat a = v.VehicleData.C lone(); this.FourWheelV ehicleData = new FourWheelVehicl eData(); }
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
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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
def foo(self, data):
self.attr1=data
self.attr2.append(data)
The initialization of attr1 is obviously OK, all instances of Father
redefine it in the...
|
by: Dave |
last post by:
class base
{
public:
base(const base &other) { // Init. here... }
// Stuff
};
class derived: public base
{
public:
|
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
|
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 D calls the dtor of D and
then the dtor of B.
I was thinking that this would be true only for non-virtual dtor case,
but I wouldn't have expected...
|
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;
// ... more protected fields
}
| |
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;
|
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");
}
|
by: siddhu |
last post by:
Dear experts,
#include <stdio.h>
class Base
{
};
class Der1:public Base
{
|
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...
|
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...
|
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...
| |
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...
|
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...
|
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...
|
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
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
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...
| |