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

query on design of classes

I am working with a class which has a constructor which takes several
parameters:
class MyClass {public: MyClass(SomeType A, SomeOtherType B,
YetAnotherType C) {} // ....}

I want to amend MyClass slightly so that it takes more parameters:
class MyClass {public: MyClass(SomeType A, SomeOtherType B,
YetAnotherType C, AddOneMoreType D) {} // ....}
I am told not to do that because MyClass should not be changed and
because adding more parameters is seen as unwieldy. I am told to
derive a class from MyClass and to add the extra parameters such as D
of type AddOneMoreType etc. to the derived class. This must be quite
a common thing to do but I'm not sure how to design this.

Something like class MyNewClass: public MyClass{ public:
MyNewClass(AddOneMoreType D){} } but this doesn't work because this
doesn't reflect the fact that members of MyNewClass also take
parameters A, B and C from the base class.

Another idea is class MyNewClass: public MyClass{ public:
MyNewClass(SomeType A, SomeOtherType B, YetAnotherType C,
AddOneMoreType D) {} } but that leads to the objection that using
too many parameters in the constructor is seen as unwieldy.

Thank you for any suggestions.

Paul Epstein
Dec 5 '07 #1
5 1267
pa**********@att.net wrote:
I am working with a class which has a constructor which takes several
parameters:
class MyClass {public: MyClass(SomeType A, SomeOtherType B,
YetAnotherType C) {} // ....}

I want to amend MyClass slightly so that it takes more parameters:
class MyClass {public: MyClass(SomeType A, SomeOtherType B,
YetAnotherType C, AddOneMoreType D) {} // ....}
I am told not to do that because MyClass should not be changed and
because adding more parameters is seen as unwieldy. I am told to
derive a class from MyClass and to add the extra parameters such as D
of type AddOneMoreType etc. to the derived class. This must be quite
a common thing to do but I'm not sure how to design this.

Something like class MyNewClass: public MyClass{ public:
MyNewClass(AddOneMoreType D){} } but this doesn't work because this
doesn't reflect the fact that members of MyNewClass also take
parameters A, B and C from the base class.

Another idea is class MyNewClass: public MyClass{ public:
MyNewClass(SomeType A, SomeOtherType B, YetAnotherType C,
AddOneMoreType D) {} } but that leads to the objection that using
too many parameters in the constructor is seen as unwieldy.
class MyNewClass: public MyClass
{
public:
MyNewClass( SomeType A,
SomeOtherType B,
YetAnotherType C,
AddOneMoreType D ) : MyClass( A, B, C )
{}
}

is the correct way, but some classes are just not meant to be used as
base classes. What if MyClass has no virtual destructor?
Dec 5 '07 #2
On Dec 5, 10:42 pm, anon <a...@no.nowrote:
pauldepst...@att.net wrote:
I am working with a class which has a constructor which takes several
parameters:
class MyClass {public: MyClass(SomeType A, SomeOtherType B,
YetAnotherType C) {} // ....}
I want to amend MyClass slightly so that it takes more parameters:
class MyClass {public: MyClass(SomeType A, SomeOtherType B,
YetAnotherType C, AddOneMoreType D) {} // ....}
I am told not to do that because MyClass should not be changed and
because adding more parameters is seen as unwieldy. I am told to
derive a class from MyClass and to add the extra parameters such as D
of type AddOneMoreType etc. to the derived class. This must be quite
a common thing to do but I'm not sure how to design this.
Something like class MyNewClass: public MyClass{ public:
MyNewClass(AddOneMoreType D){} } but this doesn't work because this
doesn't reflect the fact that members of MyNewClass also take
parameters A, B and C from the base class.
Another idea is class MyNewClass: public MyClass{ public:
MyNewClass(SomeType A, SomeOtherType B, YetAnotherType C,
AddOneMoreType D) {} } but that leads to the objection that using
too many parameters in the constructor is seen as unwieldy.

class MyNewClass: public MyClass
{
public:
MyNewClass( SomeType A,
SomeOtherType B,
YetAnotherType C,
AddOneMoreType D ) : MyClass( A, B, C )
{}

}

is the correct way, but some classes are just not meant to be used as
base classes. What if MyClass has no virtual destructor?- Hide quoted text -

- Show quoted text -
Thanks a lot. I wanted to avoid long constructors. (In reality I
would be adding parameters D, E etc to MyNewClass.) Perhaps I could
accomplish this by means of just adding a pointer. I was hoping there
was some syntax available that looked a bit like class MyNewClass:
public MyClass
{
public:
MyNewClass( AddOneMoreType D ) : MyClass( A, B, C )
{}

}
I know this is illegal though (hence my question). Thanks again for
your solution.

Paul Epstein
Dec 5 '07 #3
pa**********@att.net wrote:
On Dec 5, 10:42 pm, anon <a...@no.nowrote:
>pauldepst...@att.net wrote:
>>I am working with a class which has a constructor which takes several
parameters:
class MyClass {public: MyClass(SomeType A, SomeOtherType B,
YetAnotherType C) {} // ....}
I want to amend MyClass slightly so that it takes more parameters:
class MyClass {public: MyClass(SomeType A, SomeOtherType B,
YetAnotherType C, AddOneMoreType D) {} // ....}
I am told not to do that because MyClass should not be changed and
because adding more parameters is seen as unwieldy. I am told to
derive a class from MyClass and to add the extra parameters such as D
of type AddOneMoreType etc. to the derived class. This must be quite
a common thing to do but I'm not sure how to design this.
Something like class MyNewClass: public MyClass{ public:
MyNewClass(AddOneMoreType D){} } but this doesn't work because this
doesn't reflect the fact that members of MyNewClass also take
parameters A, B and C from the base class.
Another idea is class MyNewClass: public MyClass{ public:
MyNewClass(SomeType A, SomeOtherType B, YetAnotherType C,
AddOneMoreType D) {} } but that leads to the objection that using
too many parameters in the constructor is seen as unwieldy.
class MyNewClass: public MyClass
{
public:
MyNewClass( SomeType A,
SomeOtherType B,
YetAnotherType C,
AddOneMoreType D ) : MyClass( A, B, C )
{}

}

is the correct way, but some classes are just not meant to be used as
base classes. What if MyClass has no virtual destructor?- Hide quoted text -

- Show quoted text -

Thanks a lot. I wanted to avoid long constructors. (In reality I
would be adding parameters D, E etc to MyNewClass.) Perhaps I could
accomplish this by means of just adding a pointer. I was hoping there
was some syntax available that looked a bit like class MyNewClass:
public MyClass
>{
public:
MyNewClass( AddOneMoreType D ) : MyClass( A, B, C )
{}

}
IMO this is better then inheritance:

public MyClass
{
public:
MyNewClass( MyClass C, AddOneMoreType D ) : C( C ), D( D )
{}

private:
MyClass C;
AddOneMoreType D;

}

PS Better to pass C and D via reference or a pointer - depends what
types they are.
Dec 5 '07 #4
On Dec 5, 11:54 pm, anon <a...@no.nowrote:
pauldepst...@att.net wrote:
On Dec 5, 10:42 pm, anon <a...@no.nowrote:
pauldepst...@att.net wrote:
I am working with a class which has a constructor which takes several
parameters:
class MyClass {public: MyClass(SomeType A, SomeOtherType B,
YetAnotherType C) {} // ....}
I want to amend MyClass slightly so that it takes more parameters:
class MyClass {public: MyClass(SomeType A, SomeOtherType B,
YetAnotherType C, AddOneMoreType D) {} // ....}
I am told not to do that because MyClass should not be changed and
because adding more parameters is seen as unwieldy. I am told to
derive a class from MyClass and to add the extra parameters such as D
of type AddOneMoreType etc. to the derived class. This must be quite
a common thing to do but I'm not sure how to design this.
Something like class MyNewClass: public MyClass{ public:
MyNewClass(AddOneMoreType D){} } but this doesn't work because this
doesn't reflect the fact that members of MyNewClass also take
parameters A, B and C from the base class.
Another idea is class MyNewClass: public MyClass{ public:
MyNewClass(SomeType A, SomeOtherType B, YetAnotherType C,
AddOneMoreType D) {} } but that leads to the objection that using
too many parameters in the constructor is seen as unwieldy.
class MyNewClass: public MyClass
{
public:
MyNewClass( SomeType A,
SomeOtherType B,
YetAnotherType C,
AddOneMoreType D ) : MyClass( A, B, C )
{}
}
is the correct way, but some classes are just not meant to be used as
base classes. What if MyClass has no virtual destructor?- Hide quoted text -
- Show quoted text -
Thanks a lot. I wanted to avoid long constructors. (In reality I
would be adding parameters D, E etc to MyNewClass.) Perhaps I could
accomplish this by means of just adding a pointer. I was hoping there
was some syntax available that looked a bit like class MyNewClass:
public MyClass
{
public:
MyNewClass( AddOneMoreType D ) : MyClass( A, B, C )
{}
}

IMO this is better then inheritance:

public MyClass
{
public:
MyNewClass( MyClass C, AddOneMoreType D ) : C( C ), D( D )
{}

private:
MyClass C;
AddOneMoreType D;

}

PS Better to pass C and D via reference or a pointer - depends what
types they are.- Hide quoted text -

- Show quoted text -
Thanks anon. Yes I also prefer this. This is exactly the kind of
thing I was looking for. I was surprised at the first line of your
code -- public MyClass I expected to see class MyClass
Is this an error caused by inadvertently lapsing into java or am I
missing something?

Thanks again.

Paul Epstein
Dec 6 '07 #5
>public MyClass
>{
public:
MyNewClass( MyClass C, AddOneMoreType D ) : C( C ), D( D )
{}

private:
MyClass C;
AddOneMoreType D;

}

PS Better to pass C and D via reference or a pointer - depends what
types they are.- Hide quoted text -

- Show quoted text -

Thanks anon. Yes I also prefer this. This is exactly the kind of
thing I was looking for. I was surprised at the first line of your
code -- public MyClass I expected to see class MyClass
Is this an error caused by inadvertently lapsing into java or am I
missing something?
Stupid copy/paste.

Should be:

class MyNewClass
{
public:
MyNewClass( MyClass C, AddOneMoreType D ) : C( C ), D( D )
{}

private:
MyClass C;
AddOneMoreType D;

}
Dec 6 '07 #6

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

Similar topics

3
by: Patchwork | last post by:
Hi Everyone, Please take a look at the following (simple and fun) program: //////////////////////////////////////////////////////////////////////////// ///////////// // Monster Munch, example...
3
by: Omer van Kloeten | last post by:
The Top Level Design: The class Base is a factory class with a twist. It uses the Assembly/Type classes to extract all types that inherit from it and add them to the list of types that inherit...
6
by: laj | last post by:
HI, Excuse this new question, I am trying to impress my wife by putting a db together for her dance studio. I put a table with all students new and old with fields including name and address and...
15
by: Richard Hollenbeck | last post by:
I tried to ask this question before on the 14th of January but I never got a reply. I'm still struggling with the problem. I'll try to rephrase the question: I have a crosstab query with rows...
0
by: Edward Diener | last post by:
In Borland's VCL it was possible to divide a component into design time and run time DLLs. The design time DLL would only be necessary when the programmer was setting a component's properties or...
17
by: tshad | last post by:
Many (if not most) have said that code-behind is best if working in teams - which does seem logical. How do you deal with the flow of the work? I have someone who is good at designing, but...
3
by: faceman28208 | last post by:
Over the past few years I have consulted on six large projects that all independently arrived at the same moronic design desision: The use of SQL query classes. No, I don't mean a class...
20
by: Brad Pears | last post by:
I am completely new to vb .net. I am using visual Studio 2005 to redo an Access 2000 application into a .net OO application using SQL Server 2000 - so a complete rewrite and re-thinking of how...
9
by: Grizlyk | last post by:
Somebody have offered std colors to C++ in the msg here: http://groups.google.com/group/comp.lang.c++/browse_frm/thread/2e5bb3d36ece543b/1acf6cd7e3ebdbcd#1acf6cd7e3ebdbcd The main objection to...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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.