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

inheritence question

Hi all,

If I call the base class from a derived classes constructor, is the
base class constructor executed before the calling derived classes
constructor? :

public someclass( someparam foo) : base( foo)

Aug 14 '06 #1
11 1628
The base class constructor is ALWAYS called before a derived class constructor.

WhiteWizard
aka Gandalf
MCSD.NET, MCAD, MCT
"^MisterJingo^" wrote:
Hi all,

If I call the base class from a derived classes constructor, is the
base class constructor executed before the calling derived classes
constructor? :

public someclass( someparam foo) : base( foo)

Aug 14 '06 #2
Hi,

Yes

Anyway It's very easy to test it:
class A{
string s;
A(string s){this.s = s;}
}
class B:A{
B(string s):base(s)
{
Console.Write(s);
}
}

--
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"^MisterJingo^" <mi*********@gmail.comwrote in message
news:11**********************@m79g2000cwm.googlegr oups.com...
Hi all,

If I call the base class from a derived classes constructor, is the
base class constructor executed before the calling derived classes
constructor? :

public someclass( someparam foo) : base( foo)

Aug 14 '06 #3
Hello, ^MisterJingo^!

MIf I call the base class from a derived classes constructor, is the
Mbase class constructor executed before the calling derived classes
Mconstructor? :

I suppose code sample below can answer your question

public class Base
{
public Base()
{
Console.WriteLine("Base");
}
}
public class Child : Base
{
public Child() : base()
{
Console.WriteLine("Child");
}

}
--
Regards, Vadym Stetsyak
www: http://vadmyst.blogspot.com
Aug 14 '06 #4
Thanks for the quick replies all.

I've inherited a system which consists of a data layer base class.
Classes derive from this and pass the connection info to the base
constructor as such:

base(conn object, string table)

child(conn object) : base (object, table)
The problem is that I need to inherit from Child class, but I need a
way of passing my derived classes table string to the base class, else
it will be looking at the child classes table in the DB, rather than my
new classes.
Is there any way I can do this? I've thought about making that class
an interface, but i'm not sure what I have to do will work like that.
For example:

base(conn object, string table)

child(conn object) : base (object, table)

myclass(conn object)
myclass needs to be inherited from, but on occasion, myclass will also
have to be instantiated. The system I have been given shows DB tables
as represented by classes.
Each class at some point needs to be instantiated so it can be used to
interface with certain tables in the DB.

Anyone know how I can go about doing this? If it's not clear what I
mean, I can post some code examples.

Thanks :).

Aug 14 '06 #5
^MisterJingo^ <mi*********@gmail.comwrote:
If I call the base class from a derived classes constructor, is the
base class constructor executed before the calling derived classes
constructor? :

public someclass( someparam foo) : base( foo)
The base class constructor is called first.

See http://www.pobox.com/~skeet/csharp/constructors.html for more on
this.

--
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
Aug 14 '06 #6
Hi,

"^MisterJingo^" <mi*********@gmail.comwrote in message
news:11**********************@i3g2000cwc.googlegro ups.com...
Thanks for the quick replies all.

I've inherited a system which consists of a data layer base class.
Classes derive from this and pass the connection info to the base
constructor as such:

base(conn object, string table)

child(conn object) : base (object, table)
Where does table comes from in the child class??
The above code does not compile (unless table is a constant).
--
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
Aug 14 '06 #7

Ignacio Machin ( .NET/ C# MVP ) wrote:
Hi,

"^MisterJingo^" <mi*********@gmail.comwrote in message
news:11**********************@i3g2000cwc.googlegro ups.com...
Thanks for the quick replies all.

I've inherited a system which consists of a data layer base class.
Classes derive from this and pass the connection info to the base
constructor as such:

base(conn object, string table)

child(conn object) : base (object, table)

Where does table comes from in the child class??
The above code does not compile (unless table is a constant).

Hi Ignacio,

That was a bit of pseudo code. I'll give a better example below:

public class Base
{
public Base(string _blah)
{
Console.WriteLine(_blah);
}
}

public class Child : Base
{
static string blah = "Child";
public Child(string _blah) : base(blah)
{
blah = _blah;
Console.WriteLine(blah);
}
}

public class Adult : Child
{
static string blah = "Adult";
public Adult()
: base(blah)
{
Console.WriteLine(blah);
Console.ReadLine();
}

static void Main()
{
Adult c = new Adult();
}
}

Because the child constructor calls base first, it passes the child
'blah' string to base, I need to get the adult 'blah' string down to
base. I guess I could have a method which passes this down and sets it
in the constructor. I wasn;t sure if that was the ideal solution though.

Aug 14 '06 #8
Hi Ignacio,

That was a bit of pseudo code. I'll give a better example below:
And again the code below is a pseudo code too

Note that you are ALWAYS passing to Base the same string ( Child.blah )

Usually you declare a constructor in a derived class that has ALL the needed
values than the base class needs (or assume constants). You are not doing
this and this is where your problems come from.
--
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

public class Base
{
public Base(string _blah)
{
Console.WriteLine(_blah);
}
}

public class Child : Base
{
static string blah = "Child";
public Child(string _blah) : base(blah)
{
blah = _blah;
Console.WriteLine(blah);
}
}

public class Adult : Child
{
static string blah = "Adult";
public Adult()
: base(blah)
{
Console.WriteLine(blah);
Console.ReadLine();
}

static void Main()
{
Adult c = new Adult();
}
}

Because the child constructor calls base first, it passes the child
'blah' string to base, I need to get the adult 'blah' string down to
base. I guess I could have a method which passes this down and sets it
in the constructor. I wasn;t sure if that was the ideal solution though.

Aug 15 '06 #9
Ignacio Machin ( .NET/ C# MVP ) wrote:
Hi Ignacio,

That was a bit of pseudo code. I'll give a better example below:

And again the code below is a pseudo code too
The code I posted should compile.
Note that you are ALWAYS passing to Base the same string ( Child.blah )

Usually you declare a constructor in a derived class that has ALL the needed
values than the base class needs (or assume constants). You are not doing
this and this is where your problems come from.

I can't make changes to the base, and I think I must have been tired
yesterday not to see adding another constructor to the child would
solve this issue. Thanks for the help :).

Aug 15 '06 #10
Hi,

"^MisterJingo^" <mi*********@gmail.comwrote in message
news:11**********************@b28g2000cwb.googlegr oups.com...
Ignacio Machin ( .NET/ C# MVP ) wrote:
Hi Ignacio,

That was a bit of pseudo code. I'll give a better example below:

And again the code below is a pseudo code too

The code I posted should compile.
Yes, but I mean that you are always passing the same string to Base, when
your original problem was how to pass a string defined in the Adult class.
--
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
Aug 15 '06 #11
Ignacio Machin ( .NET/ C# MVP ) wrote:
Hi,

"^MisterJingo^" <mi*********@gmail.comwrote in message
news:11**********************@b28g2000cwb.googlegr oups.com...
Ignacio Machin ( .NET/ C# MVP ) wrote:
Hi Ignacio,

That was a bit of pseudo code. I'll give a better example below:

And again the code below is a pseudo code too
The code I posted should compile.

Yes, but I mean that you are always passing the same string to Base, when
your original problem was how to pass a string defined in the Adult class.
Ah, I see what you mean :). The code was a very simplified version of
the project I've been handed to extend. But I've solved it now.

Thanks.

Aug 15 '06 #12

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...
10
by: Malay Haldar | last post by:
I have a function object, from which I derive another function object in which I overwrite the operator(). But now the operator() in the base class seems inaccessible. For example, the following...
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...
7
by: Ron Vecchi | last post by:
Latley I have been messing around with polymorphsim and inheritence and really enjoying the benefits. public myclass() { 'mycode } public myclass(string s) : this() { 'more code
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: tirumalab | last post by:
Hi all, im working on C#, in my current proj we're using one form as the base form and deriving it from all the forms ,its working fine with windows applicatons.But we want to develop the same...
6
by: CapMaster | last post by:
I'm having some trouble using Inheritence classes in c++. I have my base class (Time.h) and my derived class is called (MilTime.h). I started writing my MilTime.cpp for the function definitions. Now,...
4
by: arnaudk | last post by:
I have two unrelated types of data elements (objects) which I want to hold in two related types of containers, one for each element type. But this seems contradictory - for consistency, it seems that...
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: 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?
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
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...
0
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...

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.