I have a public abstract class that I want to inherit from in two other
classes. My public abstract one has a constructor with several
parameters. For some reason I cannot get to that constructor unless I
create a constructor in my two other classes like this:
public ClassName(strin g szParameter1, string szParameter2, string
szParameter3) : base(szParamete r1, szParameter2, szParameter3)
{
}
This doesn't make sense to me. The reason I did this was to save on
code and now I've essentially got my parameters listed 2 times more
than I thought I'd need for each class that inherits from the abstract
one.
Is there a better way to do this? 12 1622
"Doug" <dn******@dtgne t.com> wrote in message
news:11******** *************@l 41g2000cwc.goog legroups.com... I have a public abstract class that I want to inherit from in two other classes. My public abstract one has a constructor with several parameters. For some reason I cannot get to that constructor unless I create a constructor in my two other classes like this:
public ClassName(strin g szParameter1, string szParameter2, string szParameter3) : base(szParamete r1, szParameter2, szParameter3) { }
This doesn't make sense to me. The reason I did this was to save on code and now I've essentially got my parameters listed 2 times more than I thought I'd need for each class that inherits from the abstract one.
Is there a better way to do this?
First, read Mattias' answer. Similarly, destructors are not inherited.
Second, if by "better" you mean simpler, you can consider defining and using
a default constructor in the base class, thus avoiding the:
: base(szParamete r1, szParameter2, szParameter3)
But, if the derived class constructor's parameters are needed as arguments
to the base class constructor, that won't work for you.
--
Peter [MVP Visual Developer]
Jack of all trades, master of none.
"Doug" <dn******@dtgne t.com> wrote in message
news:11******** *************@l 41g2000cwc.goog legroups.com... I have a public abstract class that I want to inherit from in two other classes. My public abstract one has a constructor with several parameters. For some reason I cannot get to that constructor unless I create a constructor in my two other classes like this:
public ClassName(strin g szParameter1, string szParameter2, string szParameter3) : base(szParamete r1, szParameter2, szParameter3) { }
This doesn't make sense to me. The reason I did this was to save on code and now I've essentially got my parameters listed 2 times more than I thought I'd need for each class that inherits from the abstract one.
Is there a better way to do this?
First, read Mattias' answer. Similarly, destructors are not inherited.
Second, if by "better" you mean simpler, you can consider defining and using
a default constructor in the base class, thus avoiding the:
: base(szParamete r1, szParameter2, szParameter3)
But, if the derived class constructor's parameters are needed as arguments
to the base class constructor, that won't work for you.
--
Peter [MVP Visual Developer]
Jack of all trades, master of none.
Normally, when folks say "This doesn't make sense to me", that tells me that
you expected one thing, but the framework gave you another thing. Can you
tell us what you expected would happen?
--
--- Nick Malik [Microsoft]
MCSD, CFPS, Certified Scrummaster http://blogs.msdn.com/nickmalik
Disclaimer: Opinions expressed in this forum are my own, and not
representative of my employer.
I do not answer questions on behalf of my employer. I'm just a
programmer helping programmers.
--
Thanks for both of your responses. Unfortunately, I do need a
constructor with parameters in the base class because both of my
inherited ones need that same constructor. So I'll have to go with
what I'm doing. Thanks again.
Normally, when folks say "This doesn't make sense to me", that tells me that
you expected one thing, but the framework gave you another thing. Can you
tell us what you expected would happen?
--
--- Nick Malik [Microsoft]
MCSD, CFPS, Certified Scrummaster http://blogs.msdn.com/nickmalik
Disclaimer: Opinions expressed in this forum are my own, and not
representative of my employer.
I do not answer questions on behalf of my employer. I'm just a
programmer helping programmers.
--
Thanks for both of your responses. Unfortunately, I do need a
constructor with parameters in the base class because both of my
inherited ones need that same constructor. So I'll have to go with
what I'm doing. Thanks again.
I didn't realize Constructors were not inherited. I thought that by
having an abstract class that two other classes inherited from that I
could essentially have this:
public abstract class AbstractClass
{
decimal m_m1;
decimal m_m2;
decimal m_m3;
public AbstractClass(d ecimal m1, decimal m2, decimal m3)
{
m_m1 = m1;
m_m2 = m2;
m_m3 = m3;
}
}
public class InheritedClass1 : AbstractClass
{
}
public class InheritedClass2 : AbstractClass
{
}
And then I could call one of the Inherited Classes like this:
InheritedClass1 oNewClass = new InheritedClass1 (m1, m2, m3);
and that by doing this, that this line would go to the Constructor in
AbstractClass. This saves a lot of code for me. Instead I have to do
this for my inherited classes:
public class InheritedClass1 : AbstractClass
{
public InheritedClass1 (decimal m1, decimal m2, decimal m3) :
base (m1, m2, m3)
{
}
}
public class InheritedClass2 : AbstractClass
{
public InheritedClass2 (decimal m1, decimal m2, decimal m3) :
base (m1, m2, m3)
{
}
}
It still saves some code, but it seems like some excess to me that I
have to have a constructor in my Inherited classes and that I have to
repeate the parameters in the base entry as well. But I think I know
why now. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Ricky Romaya |
last post by:
Hi,
Are there any ways to get multiple inheritace in PHP4? For example, I have
3 parent class, class A, B, and C. I want class X to inherit all those 3
classes. Consider merging those 3 classes is out of the question, as there
are class Y which only inherits from class A, etc.
TIA
|
by: Jimmy Johns |
last post by:
Hi, I have some classes as follows:
#include <iostream>
using namespace std;
class A {
public:
virtual A* clone() const = 0;
};
|
by: Doug |
last post by:
I have a public abstract class that I want to inherit from in two other
classes. My public abstract one has a constructor with several
parameters. For some reason I cannot get to that constructor unless I
create a constructor in my two other classes like this:
public ClassName(string szParameter1, string szParameter2, string
szParameter3) : base(szParameter1, szParameter2, szParameter3)
{
}
|
by: Busin |
last post by:
When a child class inherits from a base class, will the child class inherits
everything of the base class, including all member variables and functions?
Or is such inheritance "selective", like not inheriting all constructors,
assignment operators, destructor, etc.? Thanks!
|
by: Mike Tyka |
last post by:
Hello community,
i'm fairly new to using the STL but i've been experimenting a bit with it.
I tried to derive a new class say MyString from string like so:
class MyString: public string{
public:
MyString(){}
};
| |
by: Gianni Mariani |
last post by:
In the code below, controller::controller() is never invoked, however,
it appears there is no way to make a compile-time rule that this should
not happen. The code below seems to make compilers complain that
controller::controller() is private, even though it is never used.
What do others do to work-around this ? I suppose I can simply
not implement controller::controller(), that way I get a linking
error if there exists errant code....
|
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;
|
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
|
by: Jess |
last post by:
Hello,
I've been reading Effective C++ about multiple inheritance, but I
still have a few questions. Can someone give me some help please?
First, it is said that if virtual inheritance is used, then "the
responsibility for initializing a virtual base is borne by the most
derived class in the hierarchy". What does it mean? Initializing
base class is usually done automatically by the compiler, but a
derived class can invoke the base...
|
by: Tim Van Wassenhove |
last post by:
Hello,
When i read the CLI spec, 8.10.2 Method inheritance i read the
following:
"A derived object type inherits all of the instance and virtual methods
of its base object type. It does not inherit constructors or
static methods...."
In the C# spec, 17.2.1 Inheritance i read the following:
|
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 usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
| |
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,...
|
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
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 most users, this new feature is actually very convenient. If you want to control the update process,...
|
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...
|
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...
|
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 into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
| |
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |