473,672 Members | 3,969 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 1961
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("h i");
// 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(strin g 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.goo glegroups.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******** ******@TK2MSFTN GP10.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.co m>
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

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

Similar topics

1
4139
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 capabilities
8
4221
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++ ---- --- subclass' methods can yes, default must use "virtual" in front override base class' of base class' method
5
2421
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 visible and gives it new functionality, but retains the look of the top half of the master page. I then hide the original submit button. Ok, now, after pressing the Submit button in the user control, I want to change the look of the master page.
7
1980
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 inheritence. One more article I read was Allen holub's "Why extends is evil". http://www.javaworld.com/javaworld/jw-08-2003/jw-0801-toolbox.html One general advice I found was that - extending behaviour is better done with interfaces and...
5
2121
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 disagreement about whether is essential in C++". Are there any disadvantages of using multiple inheritence?
0
8503
marktang
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...
0
8419
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,...
0
8945
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8642
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,...
0
8696
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 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...
0
5720
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();...
1
2836
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
2
2092
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1836
bsmnconsultancy
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.