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

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 1940
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
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
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
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
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
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
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
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...

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.