Question Your opinion regarding constructor

bondra

Well-known member
Joined
Oct 24, 2020
Messages
77
Programming Experience
Beginner
If you look at the code below. What would you say is wrong with the Puppy constructor? Is there any obvious between the classes the class Dog and the inherited Puppy?

Puppy class:
class Puppy : Dog
    {
        // Properties
        public int AgeInMonths { get; set; }
        override public int Age { get; set; } = 0;
      
        // Constructors
        public Puppy(string _name, int _ageInMonths, string _favFood, string _breed, bool _isHungry, List<Toy> _toyList) : base(_name, _ageInMonths, _favFood, _breed, _isHungry, _toyList)
        {
            Name = _name;
            AgeInMonths = _ageInMonths;
            FavFood = _favFood;
            Breed = _breed;
            IsHungry = _isHungry;
            Toys = _toyList;
        }

        public Puppy()
        {
            Name = "Puppy";
            AgeInMonths = 7;
            FavFood = "Whiskas";
            Breed = "Skogskatt";
            IsHungry = false;
            Toys =
            new List<Toy> {
                new Cattree {Size = "Large"},
                new Ball {Color = "Yellow"},
            };

        }
    }
}

C#:
    class Dog : Animal
    {
     
        // Constructors
        public Dog () {
            Name = "Doggy";
            Age = 4;
            FavFood = "Frolic";
            Breed = "Rotweiler";
            IsHungry = false;
            new List<Toy> {
                new Cattree {Size = "Large"},
                new Ball {Color = "Yellow"},
            };
        }
     
        public Dog(string _name, int _age, string _favFood, string _breed, bool _isHungry, List<Toy> _toyList)
            : base(_name, _age, _favFood, _breed, _isHungry, _toyList)
        {
            Name = _name;
            Age = _age;
            FavFood = _favFood;
            Breed = _breed;
            IsHungry = _isHungry;
            Toys = _toyList;
        }
 
If the base class Dog already sets Name, FavFood, Breed, IsHungry, and Toys, then there is no need for your Puppy class to set them.

Now as for passing in _ageInMonths to Dog's constructor's age parameter, shouldn't you be dividing the value by 12 since presumably that parameter is supposed to be age in years?

This is a programming style thing, but the leading underscores in C# is usually used for class member variables, not parameters, by convention for people who don't follow the "Don't use Hungarian notation" advice regarding C# naming conventions. I forget which programming language it was which had a convention where parameters were decorated with leading underscores for parameters.

Not related to your constructor, I think that your Age property override needs to be smarter to be able to do conversions between AgeInMonths and the expected behavior where Age is age in years.
 
Delete puppy class. It's pointless. If you have a dog class, then add an attribute to that class to define the age or youth of the dog. A puppy is the same as a dog. A puppy is still a dog.

That's like having a class of child and having a class of baby to refer to the child.
 
Thanks for your quick answer!

Now as for passing in _ageInMonths to Dog's constructor's age parameter, shouldn't you be dividing the value by 12 since presumably that parameter is supposed to be age in years?
That would probably be a good idea! But if I keep this for now, should the following be removed in the puppy class? How else would I set age to 0 when it's a puppy?
C#:
override public int Age { get; set; } = 0;

If the base class Dog already sets Name, FavFood, Breed, IsHungry, and Toys, then there is no need for your Puppy class to set them.
Removing the inhertied fields leave me with some errors. I don't think I understand what it should look like overhere if the base field not are needed here?
Capture.PNG

C#:
        public Puppy(int _ageInMonths)
        {
            Name = _name;
            AgeInMonths = _ageInMonths;
            FavFood = _favFood;
            Breed = _breed;
            IsHungry = _isHungry;
            Toys = _toyList;
        }



This is a programming style thing, but the leading underscores in C# is usually used for class member variables
Should it be this instead?
 
Delete puppy class. It's pointless. If you have a dog class, then add an attribute to that class to define the age or youth of the dog. A puppy is the same as a dog. A puppy is still a dog.

That's like having a class of child and having a class of baby to refer to the child.
I totally agree on this point. It's a school assignment so the purpose is more to show a class that's inheriting.
 
Most schools don't know how to teach people about coding or programming. Their examples and assignments they give students are diabolical. Almost all people who come here with school assignments are mostly ridiculed by myself. Which is why I am reluctant to say much more on the subject. They'd have done a better job of giving you a class of Vehicle, and then have you inherit vehicle as your base class. It would make more sense to have a class type of cars since all cars are vehicles and all vehicles have names. So a car could inherit the vehicle class to acquire a name property of that brand name of vehicle etc.

Above you would do the same, except the example they gave you is rather lame and a bad example to follow. But as @jmcilhinney always says to me, while he has yet to disagree with me, he remind me it is often the principle of the exercise which the student is graded on. And the instructions of the assignment should still be followed as requested by their instructor.
 
Here is a good example of the type of example I am referring too, which would have been a better example to use : C# Inheritance

Ditch the puppy class.

Create a class of animal,
Then declare a class of types of animals to inherit from your animal class, such as cat, dog, pony etc. That link should spark your imagination.
 
Thanks for your quick answer!


That would probably be a good idea! But if I keep this for now, should the following be removed in the puppy class? How else would I set age to 0 when it's a puppy?
C#:
override public int Age { get; set; } = 0;


Removing the inhertied fields leave me with some errors. I don't think I understand what it should look like overhere if the base field not are needed here?
View attachment 1348
C#:
        public Puppy(int _ageInMonths)
        {
            Name = _name;
            AgeInMonths = _ageInMonths;
            FavFood = _favFood;
            Breed = _breed;
            IsHungry = _isHungry;
            Toys = _toyList;
        }




Should it be this instead?

Like this:
C#:
class Puppy : Dog
{
    public int AgeInMonths { get; set; }

    public override int Age
    {
        get => AgeInMonths / 12;
        set => AgeInMonths = value * 12;
    };

    public Puppy(string name, int ageInMonths, string favFood, string breed, bool isHungry, List<Toy> toyList)
        : base(name, ageInMonths / 12, favFood, breed, isHungry, toyList)
    {
         AgeInMonths = _ageInMonths;
    }

    public Puppy()
        : base("Puppy", 0, "Whiskas", "Skogskatt", isHungry : false, null)
    {
        AgeInMonths = 7;
        Toys = new List<Toy>
               {
                   new Cattree {Size = "Large"},
                   new Ball {Color = "Yellow"},
               };
    }
}
 
Back
Top Bottom