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) 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)
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)
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
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 :).
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
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.
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.
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 :).
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
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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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++
---- ...
|
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...
|
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...
|
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...
|
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
|
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...
|
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...
|
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,...
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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...
|
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: 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...
|
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...
| |