473,513 Members | 2,356 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

inheritence


Hello,
I have a class (base class) a class which inherites it .
The inhereted class suppose to have all the features of the base class:
the public properties. I have an argument which the base class gets and
I sent this argument to dervied class constractor also.
I get this error:
No overload for method BaseClass() taking 0 arguments. why?
Thank you!
*** Sent via Developersdex http://www.developersdex.com ***
Nov 17 '05 #1
13 1945
juli jul wrote:
I have a class (base class) a class which inherites it .
The inhereted class suppose to have all the features of the base class:
the public properties. I have an argument which the base class gets and
I sent this argument to dervied class constractor also.
I get this error:
No overload for method BaseClass() taking 0 arguments. why?
Thank you!


See http://www.pobox.com/~skeet/csharp/constructors.html

In short, constructors aren't inherited.

If this doesn't help you, please post a short but complete example
which demonstrates the problem. See
http://www.pobox.com/~skeet/csharp/complete.html for what I mean by
that.

Jon

Nov 17 '05 #2
> In short, constructors aren't inherited.

Which we Delphites knows all too well! - What where you thinking Hejlsberg!?

Give it to us! Forget about LINQ; - We need virtual constructors like in
Delphi.. We're dying out here.... *trying to get up* ... *thump*

Well, I'm on a project that involves both .NET 2.0 and Delphi 7. Delphi
still rules! Polymorphic types is what makes the difference.

Anybody knows why C# took this path? Seriously. Why doesn't C# have
polymorphic types? While Delphi has primitive RTTI, .NET got reflection. I
can't see why you choose not to go polymporphic.

Tell me please...

Happy Coding
- Michael S

ps.
Mr. Skeet: I'm hoping for a good answer =)

Nov 17 '05 #3
I don't get it, how can you inherit constructors? How will the derived
class be initialized if you skip its constructor and go directly for
the constructor of its base class?

Like in this example, which don't compile because of object dc2. But if
it did as apperently with Delphi, what would dc2.str and dc2.date be
set to? Which of DerivedClass's constructors would run? How does delphi
solve that?

public class BaseClass
{
int n;
public BaseClass (int n)
{
this.n = n;
}
}

public class DerivedClass : BaseClass
{
string str;
DateTime date;

public DerivedClass (string s) : base (5)
{
this.str = s;
this.date = DateTime.Today;
}

public DerivedClass (DateTime date) : base (5)
{
this.str = "hello";
this.date = date;
}

public static void Main()
{
DerivedClass dc1 = new DerivedClass("hi");
// dc1.n = 5, dc1.str = hi, dc1.date = todays date

DerivedClass dc2 = new DerivedClass(10);
// dc2.n = 5, dc2.str = ???, dc2.date = ???
}
}

Thanks, Patric
My C# blog: http://spaces.msn.com/members/pjsson

Nov 17 '05 #4
Provide a parameter less constructor.

class AAA{
//parameter less ctor
public AAA(){

}
}

Nov 17 '05 #5
Provide a parameter less constructor in the base class.

class AAA{
//parameter less ctor
public AAA(){

}
public AAA(int i){

}
}
class BBB: AAA{
static void Main(){
// call to parameter less ctor
AAA a = new AAA();
AAA aa = new AAA(10);

}
}
AAA a = new AAA() would give an error if the parameter less ctor is not
there in the base class.You must either define constructor as part of
your class definition or let the runtime provide one on your behalf.

Nov 17 '05 #6

"juli jul" wrote...
I have a class (base class) a class which inherites it .
The inhereted class suppose to have all the features of the base class:
the public properties. I have an argument which the base class gets and
I sent this argument to dervied class constractor also.
I get this error:
No overload for method BaseClass() taking 0 arguments. why?


In short, if you created a constructor in the base class that needs an
argument, and at the same time *not* provided a constructor with 0
arguments, the constructor in derived classes need to specify which
constructor to use in the base class.

Example:

public class BaseClass
{
public BaseClass(string arg) { }
}

This means that the only way to create an instance of BaseClass, you need to
provide a string, *even* for derived classes.

Example of a derived class:

public class Derived : BaseClass
{
public Derived(string arg) : base(arg) { }
}
HTH,

// Bjorn A
Nov 17 '05 #7

<pa**************@gmail.com> wrote in message
news:11**********************@g49g2000cwa.googlegr oups.com...
I don't get it, how can you inherit constructors? How will the derived
class be initialized if you skip its constructor and go directly for
the constructor of its base class?

Like in this example, which don't compile because of object dc2. But if
it did as apperently with Delphi, what would dc2.str and dc2.date be
set to? Which of DerivedClass's constructors would run? How does delphi
solve that?


Since the derived class didn't override the constructor with an int
parameter the dc2.str and dc2.date properties would remain uninitialized -
as you would expect.
Nov 17 '05 #8

"Michael S" <a@b.c> wrote in message
news:OP**************@TK2MSFTNGP10.phx.gbl...
In short, constructors aren't inherited.


Which we Delphites knows all too well! - What where you thinking
Hejlsberg!?

Give it to us! Forget about LINQ; - We need virtual constructors like in
Delphi.. We're dying out here.... *trying to get up* ... *thump*

Well, I'm on a project that involves both .NET 2.0 and Delphi 7. Delphi
still rules! Polymorphic types is what makes the difference.

Anybody knows why C# took this path? Seriously. Why doesn't C# have
polymorphic types? While Delphi has primitive RTTI, .NET got reflection. I
can't see why you choose not to go polymporphic.

Tell me please...


From Jon Skeet's page:

"Some people have said that they would rather constructors were inherited,
making the language act as if all derived classes had constructors with all
the parameter lists from the constructors from the base class, and just
invoking them with the parameters provided. I believe this would be a very
bad idea. Take, for instance, the FileInfo class. You must logically provide
a filename when constructing a FileInfo instance, as otherwise it won't know
what it's meant to be providing information on. However, as object has a
parameterless constructor, constructors being inherited would then mean that
FileInfo had a parameterless constructor. Some have suggested that this
could be fixed by allowing you to "override" the parameters you didn't want
invoked as private, but this goes against the idea that you should never be
able to override anything to give it more restrictive access, and also means
that class developers would have to change their code every time a new
constructor was added to a base class. "

http://www.yoda.arachsys.com/csharp/constructors.html

What exactly do you mean by polymorphic types?
Nov 17 '05 #9
Michael S <a@b.c> wrote:
In short, constructors aren't inherited.


Which we Delphites knows all too well! - What where you thinking Hejlsberg!?

Give it to us! Forget about LINQ; - We need virtual constructors like in
Delphi.. We're dying out here.... *trying to get up* ... *thump*

Well, I'm on a project that involves both .NET 2.0 and Delphi 7. Delphi
still rules! Polymorphic types is what makes the difference.

Anybody knows why C# took this path? Seriously. Why doesn't C# have
polymorphic types? While Delphi has primitive RTTI, .NET got reflection. I
can't see why you choose not to go polymporphic.


Well, I don't know about how Delphi handles things, but imagine if all
constructors were inherited in .NET. System.Object has a public
parameterless constructor, which would mean *all* types would have one.

What exactly would

new FileStream()

mean?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Nov 17 '05 #10
Scott Roberts <sr*********@nospam.yahoo.com> wrote:

<pa**************@gmail.com> wrote in message
news:11**********************@g49g2000cwa.googlegr oups.com...
I don't get it, how can you inherit constructors? How will the derived
class be initialized if you skip its constructor and go directly for
the constructor of its base class?

Like in this example, which don't compile because of object dc2. But if
it did as apperently with Delphi, what would dc2.str and dc2.date be
set to? Which of DerivedClass's constructors would run? How does delphi
solve that?


Since the derived class didn't override the constructor with an int
parameter the dc2.str and dc2.date properties would remain uninitialized -
as you would expect.


You can't *override* a constructor with an int parameter - overriding
implies inheritance, and as I said before, constructors aren't
inherited.

Instead, the sample will fail on the call
new DerivedClass(10);
as there are *no* DerivedClass constructors which take an int as the
parameter.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Nov 17 '05 #11

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
You can't *override* a constructor with an int parameter - overriding
implies inheritance, and as I said before, constructors aren't
inherited.
He asked how it would work in Delphi - where you *can* override a
constructor.
Instead, the sample will fail on the call
new DerivedClass(10);
as there are *no* DerivedClass constructors which take an int as the
parameter.


That's why it *won't* work in C#, the question was how *would* it work in
Delphi.
Nov 17 '05 #12
Scott Roberts <sr*********@nospam.yahoo.com> wrote:
You can't *override* a constructor with an int parameter - overriding
implies inheritance, and as I said before, constructors aren't
inherited.


He asked how it would work in Delphi - where you *can* override a
constructor.


Ah, right. Missed that, sorry - a nasty day of fitting unit tests onto
unmanaged code has left me somewhat frazzled :(

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Nov 17 '05 #13

"Scott Roberts" <sr*********@nospam.yahoo.com> wrote in message
news:OF**************@TK2MSFTNGP09.phx.gbl...
What exactly do you mean by polymorphic types?


Nevermind. I think you mean polymorphic object creation using class
references. The same can be done in C# with the Activator class. Perhaps not
as elegant, but functionally equivalent.
Nov 17 '05 #14

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

Similar topics

1
4127
by: John | last post by:
Hi, I am trying to create a class heirarchy similar to the following: // base interface class ICar { public: virtual void start() = 0; }; // add members to that interface, but retain base...
8
4211
by: Digital Puer | last post by:
I made the following table to help me (re)learn inheritence basics. Can someone check if it's correct? This table requires a courier-like font. Java C++ ---- ...
5
2410
by: john bailo | last post by:
For a c# web application, I created a user control that includes a form in control. The idea is, on the main Page, when the user clicks Submit from the master form, that makes the user control...
7
1959
by: preetam | last post by:
Hi, This question is more towards design than towards c++ details. By looking at books on design patterns and various google threads on the same topic, I see that composition is favoured to...
5
2110
by: Neelesh Bodas | last post by:
This might be slightly off-topic. Many books on C++ consider multiple inheritence as an "advanced" concept. Bruce Eckel says in TICPP, volume 2 that "there was (and still is) a lot of...
0
7171
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...
0
7545
jinu1996
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...
1
7111
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...
0
7539
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...
1
5095
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...
0
4751
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...
0
3228
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1605
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 ...
1
807
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.