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

Basic OO question

bz
Hi,

I have a base class with several constructors and I want to create a
derived class, as below

class MyBase {
public MyBase() {...}
public MyBase(int ID) {...}
public MyBase(string ID_str) {...}
//....
public void Load() {....}
}

class MyDerived: MyBase {
public MyDerived() {....}
}

If I create a variable of type MyDerived, I can access Load method,
which obvious executes the Load from MyBase
However, I cannot create a variable of type MyDerived using other than
default constructor

MyDerived ob1 = new MyDerived(); // compiles ok
MyDerived ob2 = new MyDerived(SomeID);

This later gives me compilation error that there is no constructor
with 1 parameter.

So, the contructors from base class are not inherited, as with other
public methods? Do I have to define in MyDerived all constructors I
want to use, and they'll call the corresponding constructors from
MyBase?

Thanks

Oct 8 '07 #1
5 1226
"bz" <bz*****@gmail.comwrote
So, the contructors from base class are not inherited, as with other
public methods?
Correct. Constructors are not inherited.
Do I have to define in MyDerived all constructors I
want to use, and they'll call the corresponding constructors
fromMyBase?
Yup. That's exactly what you need to do.

--
Chris Mullins
Oct 8 '07 #2
and they'll call the corresponding constructors from MyBase?

By default they will call the default (base) constuctor. To use
specific constructors, you need to give it more of a clue:

class ClassA {
public ClassA() {
Console.WriteLine("ClassA.ctor()");
}
public ClassA(int x) {
Console.WriteLine("ClassA.ctor(int x)");
}
}
class ClassB : ClassA {
public ClassB() { // uses ClassA.ctor() by default
Console.WriteLine("ClassB.ctor()");
}
public ClassB(int x) : base(x) { // need to be specific
Console.WriteLine("ClassB.ctor(int x)");
}
}

Oct 8 '07 #3
Hi bz,

Constructors are not inherited. However - a constructor in the deriving
class always has to (explicitly or implicitly) call base class's constructor.

More info here:
http://godevelop.blogspot.com/2007/1...tors-in-c.html
(that's my blog, I just coincidentally posted about what you're asking about
today!)
or here:
http://www.yoda.arachsys.com/csharp/constructors.html

Cheers,
--
_____________
Adam Bieganski
http://godevelop.blogspot.com
"bz" wrote:
Hi,

I have a base class with several constructors and I want to create a
derived class, as below

class MyBase {
public MyBase() {...}
public MyBase(int ID) {...}
public MyBase(string ID_str) {...}
//....
public void Load() {....}
}

class MyDerived: MyBase {
public MyDerived() {....}
}

If I create a variable of type MyDerived, I can access Load method,
which obvious executes the Load from MyBase
However, I cannot create a variable of type MyDerived using other than
default constructor

MyDerived ob1 = new MyDerived(); // compiles ok
MyDerived ob2 = new MyDerived(SomeID);

This later gives me compilation error that there is no constructor
with 1 parameter.

So, the contructors from base class are not inherited, as with other
public methods? Do I have to define in MyDerived all constructors I
want to use, and they'll call the corresponding constructors from
MyBase?

Thanks

Oct 8 '07 #4
bz wrote:
So, the contructors from base class are not inherited, as with other
public methods? Do I have to define in MyDerived all constructors I
want to use, and they'll call the corresponding constructors from
MyBase?
Yes.

Your IDE should have a "generate constructors from super class".

Note that:
should have != has

Arne
Oct 8 '07 #5
bz
Thank you all for your answers

Regards

On 8 Oct, 23:13, Arne Vajhøj <a...@vajhoej.dkwrote:
bz wrote:
So, the contructors from base class are not inherited, as with other
public methods? Do I have to define in MyDerived all constructors I
want to use, and they'll call the corresponding constructors from
MyBase?

Yes.

Your IDE should have a "generate constructors from super class".

Note that:
should have != has

Arne

Oct 8 '07 #6

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

Similar topics

6
by: pauldepstein | last post by:
I am reading Grimshaw and Ortega's "C++ and Numerical Methods." They construct a vector class which contains the variable vec, a float* variable where the length of the array (number of...
6
by: DH | last post by:
I have a VERY basic question about figuring database size. I've inherited a database which is generally similar to this basic one: Item, Red, Blue, Green, Yellow (text), (int),(int),(int),(int)...
9
by: Malcolm | last post by:
After some days' hard work I am now the proud possessor of an ANSI C BASIC interpreter. The question is, how is it most useful? At the moment I have a function int basic(const char *script,...
4
by: Ramesh | last post by:
hi, Let me ask some basic questions. Can anybody explain me about the following questions: 1. When we have to create sn key? Whenever we compiled Component we have to create or it is a one time...
13
by: Pete | last post by:
I'm cross posting from mscom.webservices.general as I have received no answer there: There has been a number of recent posts requesting how to satisfactorily enable BASIC authorization at the...
5
by: Aussie Rules | last post by:
Hi, Having a mental block on this one. Have done it before but can't rack my brain on how... I have an object, with a bunch on property, and I add that object to a combo box. I want the...
4
by: MikeB | last post by:
I've been all over the net with this question, I hope I've finally found a group where I can ask about Visual Basic 2005. I'm at uni and we're working with Visual Basic 2005. I have some books, ...
1
by: frankhanretty | last post by:
Do I have to install Visual basic on the remote terminals as I did on the server? I have an visual basic 5 application running fine on my client's server and he is now networked. He wants to run the...
4
by: Chris Asaipillai | last post by:
Hi there My compay has a number of Visual Basic 6 applications which are front endeed onto either SQL Server or Microsoft Access databases. Now we are in process of planning to re-write these...
3
by: Scott Stark | last post by:
Hello, I'm trying to get a better handle on OOP programming principles in VB.NET. Forgive me if this question is sort of basic, but here's what I want to do. I have a collection of Employee...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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,...

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.