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

inheriting from a class with constructors

What is the proper way of defining a class that inherits from a base class
that has no constructor with 0 arguments?

The following code generates compiler error CS1501, "No overload for method
'BaseClass' takes '0' arguments".

class BaseClass
{
public BaseClass(double x)
{
}
}

class TestClass : BaseClass
{
}

Inserting the constructor

public TestClass(double x)
{
}

into TestClass still generates the same error.

Nov 17 '05 #1
3 1658
I think you want:

class TestClass : BaseClass {
public TestClass(double x) : base(x)
{
// whatever
}
}

This is "constructor chaining".

--
I may not always have the best answers, but at least I'm not posting
questions in the wrong newsgroups...
"G. Purby" <GP****@discussions.microsoft.com> wrote in message
news:E9**********************************@microsof t.com...
What is the proper way of defining a class that inherits from a base class
that has no constructor with 0 arguments?

The following code generates compiler error CS1501, "No overload for
method
'BaseClass' takes '0' arguments".

class BaseClass
{
public BaseClass(double x)
{
}
}

class TestClass : BaseClass
{
}

Inserting the constructor

public TestClass(double x)
{
}

into TestClass still generates the same error.

Nov 17 '05 #2
It goes something like this:

If you define a class without a constructor, then the compiler will
generate an implicit constructor that takes no arguments.

So, if you write:
class A
{
};

The compiler will generate:
class A
{
public A()
{
// does nothing.
}
}
However, if you define a class with a constructor, then the compiler
will NOT generate a default constructor for you.

So if you type:
class B
{
public B( double d )
{
// more code goes here.
}
}

The compiler will generate exactly that.

Suppose now that you want to inherit from B:

class C : B
{
public C()
{
// yet more code goes here.
}
}

Then the constructor for class C MUST call a constructor for class B.
Since you have not explicitly specified what constructir to call for
class B, the compiler will attempt to call a parametless constructor.

So, the compiler will TRY to generate:

class C : B
{
public C()
{
B();
}
}

But since you have already defined a constructor for class B, one that
DOES take a parameter, the compiler will not generate a default
parameterless constructor for B, and as such, the compilation will
fail.

To remedy this, either explicitly define a paramaterless constructor
for B, as follows:

class B
{
public B( double d )
{
// more code goes here.
}

public B()
{
// whatever.
}
}

Or, modify class C to explicitly call the constructor in class class B,
as follows:

class C : B
{
public C() : base(0.0)
{
// yet more code goes here.
}
}

Nov 17 '05 #3
Thank you, Mohammad. The way you have shown of defining a constructor with a
": base()" clause is exactly the feature I need. Problem solved.

What had originally confused me was that the compiler complained about a
wrong number of arguments, not a failure to invoke the base constructor. You
have made clear why it would do that.
Nov 17 '05 #4

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

Similar topics

3
by: sureshsundar007 | last post by:
Hi all, I'm trying to inherit the SimpleWorkerRequest class but cant able to do so. I'm getting an error called "System.Web.Hosting.SimpleWorkerRequest.SimpleWorkerRequest()' is inaccessible...
2
by: SpotNet | last post by:
Hi Newsgroup, Reconstructing my common dialog assembly using C# 2.0 .NET Framework 2.0. I have the following; public class FileDialogBase: System.Windows.Forms.FileDialog { //No constructor...
2
by: Jim Heavey | last post by:
I amd playing around with inheritance a little in VB.Net If I create a new class which inherits from ListViewItem and I am only wanting to override the ToString Method. In this situation, If I...
4
by: Brad Langhorst | last post by:
Books online says that constructors are not inherited in vb.net (grrr - why not?) I have a series of classes that share the same constructors and find myself cutting and pasting when i make...
4
by: elziko | last post by:
I would like to do the following: Create a class that inherits from a usercontrol. Then add a control to the designer Call this new class ClassA. Many controls I need will start off like this...
2
by: TCook | last post by:
Hello All, First I tried to inherit from the TabPageCollection class but received the following error message: No overload for method 'TabPageCollection' takes '0' arguments I also tried...
2
by: Christof Warlich | last post by:
Hi, I'd like to define a class that should behave as much as posible like std::string, but that has some small additional property: class ExtendedString: public std::string { public: void...
0
by: Jack Jackson | last post by:
There is a class (over which I have no control) that has no public constructors - new instances are created via a Static function. I would like to modify the behavior of one of the methods of...
4
by: Thomas Wittek | last post by:
Hi! I'm relatively new to Python, so maybe there is an obvious answer to my question, that I just didn't find, yet. I've got quite some classes (from a data model mapped with SQL-Alchemy)...
4
by: AalaarDB | last post by:
struct base { int x, y, z; base() {x = 0; y = 0; z = 0;}; base(int x1, int y1, int z1) {x = x1; y = y1; z = z1;}; }; struct intermediate1 : public virtual base {}; struct intermediate2 :...
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: 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?
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...
0
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,...
0
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
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,...
0
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...

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.