0

I'M reading a book on C#, here is some of the code.

public BitterCritter(): base("", 2, 2, 0){}

I see that you can specify what attributes the child class inherits based on it's parent class through parameters. But what if you specify the base class but insert no parameters? Does it then it none, or all of the like the original class? Thanks.

One more thing, is it just me or are constructors in C# hard to see in the code. I first learned about constructors in python, where they were clearly surrounded by double underscores __MyConstructor__? Again, thanks.

2
Contributors
1
Reply
4
Views
7 Years
Discussion Span
Last Post by apegram
3

I see that you can specify what attributes the child class inherits based on it's parent class through parameters. But what if you specify the base class but insert no parameters? Does it then it none, or all of the like the original class? Thanks.

This could be some unfortunate wording. The child class inherits all of the properties, you're providing arguments via the parameters as to what those values may happen to be.

From your code example, this is what an example implementation of the class and base class may happen to be:

public class BitterCritter : Critter
    {
        public BitterCritter()
            : base(string.Empty, 2, 2, 0)
        {
        }

        public BitterCritter(string name, int arms, int legs, int tails)
            : base(name, arms, legs, tails)
        {
        }
    }

    public class Critter
    {
        public Critter(string name, int arms, int legs, int tails)
        {
            this.Name = name;
            this.Arms = arms;
            this.Legs = legs;
            this.Tails = tails;
        }

        public string Name { get; set; }
        public int Arms { get; set; }
        public int Legs { get; set; }
        public int Tails { get; set; }
    }

In it, I have a Critter class that has public Name, Arms, Legs, and Tails properties. It has a constructor that accepts parameters that are used to initialize these properties.

The BitterCritter class inherits from Critter, except that I have provided two constructors, each of which explicitly calls the base constructor. The second constructor has the same parameters as the base and simply passes them right through to the base. The first constructor accepts no arguments, so it calls the base constructor and supplies its own default arguments. In my example, if you instantiate a BitterCritter object without supplying arguments, you end up with an object with no name, 2 arms, 2 legs, and 0 tails.

This could be a way to use the BitterCritter object.

static void Main(string[] args)
    {
        BitterCritter bc = new BitterCritter();
        DisplayCritter(bc);

        bc = new BitterCritter("Rover", 0, 4, 1);
        DisplayCritter(bc);

        Console.Read();
    }

    static void DisplayCritter(Critter critter)
    {
        Console.WriteLine("Name:\t{0}\nArms:\t{1}\nLegs:\t{2}\nTails:\t{3}", critter.Name, critter.Arms, critter.Legs, critter.Tails);
    }

What if you removed the base constructor call from the parameter-less BitterCritter constructor? So you simply had:

public BitterCritter()
        {
        }

In my hastily constructed example, that would not compile! The base class of Critter has only one constructor. And the way inheritance works with object instantiation, all constructors all called, beginning with the base and working down through the derived classes. (Meaning if you do not call those base constructors explicitly, the compiler will be left to figure it out and do it for you.) With a base class that only has a constructor with parameters, you must explicitly call that constructor and provide the appropriate arguments. If you want a parameter-less BitterCritter constructor without supplying default values to the Critter constructor, you would need to create a parameter-less Critter constructor as well.

Edited by apegram: n/a

Votes + Comments
your psts are always well written and informative. keep it up :)
Very nice explanations
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.