Instance and Inheritance Question

  • Thread starter Thread starter needin4mation
  • Start date Start date
N

needin4mation

I know this doesn't have much of a context, but, in this (take from
Petzold's Programming Windows with C#) snippet:

class DatePlus: Date
{
public DatePlus(){}
public DatePlus(int year, int month, int day): base(year, month,
day){}
..
..
..
}

I understand the default constructor for DatePlus(){}. My question is
about the second one. Is it saying that DatePlus accepts, in its
formal constructor a year, month and day, but then immediately passes
them to the base class that it inherited from (Date)?

Thank you.
 
I know this doesn't have much of a context, but, in this (take from
Petzold's Programming Windows with C#) snippet:

class DatePlus: Date
{
public DatePlus(){}
public DatePlus(int year, int month, int day): base(year, month,
day){}
.
.
.
}

I understand the default constructor for DatePlus(){}. My question is
about the second one. Is it saying that DatePlus accepts, in its
formal constructor a year, month and day, but then immediately passes
them to the base class that it inherited from (Date)?

It passes them to the base *constructor* in Date. Note that the
constructor itself isn't inherited in DatePlus.
 
But this:

public DatePlus(int year, int month, int day)

is the constructor for DatePlus and nothing is done here, the body is
empty {}, because the constructor for "Date" (represented by base) is
getting the values and doing whatever with them, correct?
 
But this:

public DatePlus(int year, int month, int day)

is the constructor for DatePlus and nothing is done here, the body is
empty {}, because the constructor for "Date" (represented by base) is
getting the values and doing whatever with them, correct?

Yes.
 
How could the base class constructor be evoked first before the
DatePlus formal constructor? If that is so (and I believe you, not
fussing), why is anything at all in the DatePlus formal constructor?
Why not just have DatePlus():base(year,month,day){}?
 
How could the base class constructor be evoked first before the
DatePlus formal constructor? If that is so (and I believe you, not
fussing), why is anything at all in the DatePlus formal constructor?
Why not just have DatePlus():base(year,month,day){}?

Because then year, month day wouldn't be present in the first place. In
the DatePlus (int year, int month, int day) constructor, it's the
values of the year, month and day parameters that are passed to the
base class constructor.
 
I don't want to beat a dead horse and I appreciate the answers, but I
ask because the other person said that "base" is invoked first. This
makes me think that when the data is sent to DatePlus that it bypasses
the formal constructor parameters and goes straight to the base class
constructor. It just didn't make sense to say it went straight there.
It looks like DatePlus requires certain parameters (int year, int
month, int day) and that if that isn't met, say a string was sent to
it, it would die and nevery make it to the base class constructor.
Thanks again.
 
Maybe a rephrasing would help:

The _body_ of the base class constructor (Date) runs before the _body_
of the derived class constructor (DatePlus). So, the sequence of events
is:

Someone calls new on DatePlus, passing it year, month and day, which
results in an invocation of the DatePlus (derived class) constructor.

The first thing that happens in the DatePlus constructor is that a call
is made to the base class (Date), passing the arguments passed to the
DatePlus constructor (year, month, and day).

The base class constructor (Date(int year, int month, int day) ) is
invoked.

The body of the base class constructor runs.

Control is returned to the body of the derived class constructor, which
contains no executable code, so that constructor finishes, and the
DateTime is now fully constructed.

The DateTime(int year, int month, int day) constructor must be declared
because otherwise the call

DateTime d = new DateTime(2005, 9, 8);

would be illegal. A class can be constructed only by the constructors
it declares itself; the constructors declared by its base class are
*not* inherited and exposed by the derived class.

By the way, you can see the chain of events more clearly in those
constructors that use operators in their calls to base class
constructors, e.g.

public DateTime(int year, int month, int day) : base(year < 50 ? year +
2000 : year < 100 ? year + 1900 : year, month, day)
{ }

In this case, the expression that uses ternary operators is evaluated
_before_ the base class constructor is called, while the (empty) body
of the constructor is run _after_ the base class constructor completes.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top