inheritance problem..

Netsid

Member
Joined
Feb 10, 2020
Messages
21
Programming Experience
Beginner
"There is no argument given that corresponds to the required formal parameter 'Horsepower' of Vehicle.Vehicle(double, string)"
not sure why i cant get this to work.. feels so stupid plz help =)

Vehicle:
using System;

namespace vehicleclass
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("");
        }
    }
        public class Truck : Vehicle
        {
            private double load;
            public Truck(double HorsePower, string Color)
            {
                horsepower = HorsePower;
                color = Color;
            }
            public override void Calc_topspeed(double topspeed)
            {
                base.Calc_topspeed(topspeed);
                topspeed = horsepower / load;
            }
        }
        public class Vehicle
        {
            protected double horsepower;
            protected string color;

            public Vehicle(double Horsepower, string Color)
            {
                horsepower = Horsepower;
                color = Color;

            }
            public virtual void Calc_topspeed(double topspeed)
            {
                topspeed = horsepower / 0.8;
            }
            public void horn()
            {
                Console.WriteLine("Car horn sounds funky..");
            }

        }
    }
 
If a constructor in a derived class doesn't explicitly invoke a constructor of its base class then it will implicitly invoke the parameterless constructor of its base class. If the base class has no parameterless constructor, that's not possible, so you must explicitly invoke the constructor with parameters that you want. That means that this:
C#:
public Truck(double HorsePower, string Color)
needs to be changed to this:
C#:
public Truck(double HorsePower, string Color)
    : base(HorsePower, Color)
Because you're invoking the base constructor and it is setting those protected fields, there's no need to set them in the derived constructor. As such, that constructor body should be empty:
C#:
public Truck(double HorsePower, string Color)
    : base(HorsePower, Color)
{ }
 
On another note, method parameters should begin with a lower-case letter. You obviously know that, because you've done it correctly in the Calc_topspeed method. I'm guessing that you changed the case so as to distinguish the parameters from the fields. That was the wrong course of action. Frankly, your naming conventions are conventions at all, as you're all over the place. That code ought to look like this:
C#:
public class Truck : Vehicle
{
    private double load;

    public Truck(double horsePower, string color)
        : base(horsePower, color)
    { }

    public override void CalculateTopSpeed(double topSpeed)
    {
        base.CalculateTopSpeed(topSpeed);
        topSpeed = horsePower / load;
    }
}
public class Vehicle
{
    protected double horsePower;
    protected string color;

    public Vehicle(double horsePower, string color)
    {
        this.horsePower = horsePower;
        this.color = color;

    }

    public virtual void CalculateTopSpeed(double topSpeed)
    {
        topSpeed = horsePower / 0.8;
    }

    public void Horn()
    {
        Console.WriteLine("Car horn sounds funky..");
    }
}
You've still got some issues there though. Firstly, as a general rule, fields and properties are data and thus should be noun-based while methods are behaviour and should be verb-based. The name Horn is a noun so it is not a name for a method. That should be SoundHorn or TootHorn or something else that indicates that you're doing something with or to the horn.

More importantly though, your CalculateTopSpeed methods (or whatever poor name you're currently using for them) are nonsense. You pass in a double by value and then assign to that parameter but you don't return anything. That means that you have an input that you never use and a result that you never output. Using just the Vehicle class as an example, that method should either have no parameter and return a result:
C#:
public virtual double CalculateTopSpeed()
{
    return horsePower / 0.8;
}
or else have an out parameter and use it for output:
C#:
public virtual void CalculateTopSpeed(out double topSpeed)
{
    topSpeed = horsePower / 0.8;
}
If you are required to use an output parameter for this assignment then do so but, generally speaking, you'd never have a void method with a single output parameter. If you only want to output one value then return that value. If you want to output multiple values then you would either return one and use a parameter for the others or use parameters for them all. To decide which, you first need to determine whether the output values are the same in nature. If they are, use output parameters for all, otherwise, return one that has particular significance. As an example, if you had a method that output the height and width of a rectangle then you would use two output parameters, because the two values are of equal significance. In the case of the multitude of TryParse methods in the .NET Framework used to validate and parse text input, the method returns a bool to indicate whether the input is valid as that is basically always used. If the input is valid, the parsed result is output using a parameter.
 
On another note, method parameters should begin with a lower-case letter. You obviously know that, because you've done it correctly in the Calc_topspeed method. I'm guessing that you changed the case so as to distinguish the parameters from the fields. That was the wrong course of action. Frankly, your naming conventions are conventions at all, as you're all over the place. That code ought to look like this:
C#:
public class Truck : Vehicle
{
    private double load;

    public Truck(double horsePower, string color)
        : base(horsePower, color)
    { }

    public override void CalculateTopSpeed(double topSpeed)
    {
        base.CalculateTopSpeed(topSpeed);
        topSpeed = horsePower / load;
    }
}
public class Vehicle
{
    protected double horsePower;
    protected string color;

    public Vehicle(double horsePower, string color)
    {
        this.horsePower = horsePower;
        this.color = color;

    }

    public virtual void CalculateTopSpeed(double topSpeed)
    {
        topSpeed = horsePower / 0.8;
    }

    public void Horn()
    {
        Console.WriteLine("Car horn sounds funky..");
    }
}
You've still got some issues there though. Firstly, as a general rule, fields and properties are data and thus should be noun-based while methods are behaviour and should be verb-based. The name Horn is a noun so it is not a name for a method. That should be SoundHorn or TootHorn or something else that indicates that you're doing something with or to the horn.

More importantly though, your CalculateTopSpeed methods (or whatever poor name you're currently using for them) are nonsense. You pass in a double by value and then assign to that parameter but you don't return anything. That means that you have an input that you never use and a result that you never output. Using just the Vehicle class as an example, that method should either have no parameter and return a result:
C#:
public virtual double CalculateTopSpeed()
{
    return horsePower / 0.8;
}
or else have an out parameter and use it for output:
C#:
public virtual void CalculateTopSpeed(out double topSpeed)
{
    topSpeed = horsePower / 0.8;
}
If you are required to use an output parameter for this assignment then do so but, generally speaking, you'd never have a void method with a single output parameter. If you only want to output one value then return that value. If you want to output multiple values then you would either return one and use a parameter for the others or use parameters for them all. To decide which, you first need to determine whether the output values are the same in nature. If they are, use output parameters for all, otherwise, return one that has particular significance. As an example, if you had a method that output the height and width of a rectangle then you would use two output parameters, because the two values are of equal significance. In the case of the multitude of TryParse methods in the .NET Framework used to validate and parse text input, the method returns a bool to indicate whether the input is valid as that is basically always used. If the input is valid, the parsed result is output using a parameter.

Im trying to keep track of all the naming rules but they dont have any logic!o_O:LOL: This is (obvi) another school project, i got UML code for 2 classes with these parameters and methods, so it is what it is.. thanks for all the help (y):)
 
Im trying to keep track of all the naming rules but they dont have any logic!
Do you mean that the naming conventions used by your teacher or those out in the wild? If you mean the latter then you could not be more wrong. Basically, use Pascal casing by default. That means no underscores and the first letter in each word or part of the name begins with an upper-case letter. The exceptions to that rule are parameters and other variables. They should use camel casing, so start with a lower-case letter but otherwise the same. That is the case for local variables and private fields. Public fields should use Pascal casing.
 
Do you mean that the naming conventions used by your teacher or those out in the wild? If you mean the latter then you could not be more wrong. Basically, use Pascal casing by default. That means no underscores and the first letter in each word or part of the name begins with an upper-case letter. The exceptions to that rule are parameters and other variables. They should use camel casing, so start with a lower-case letter but otherwise the same. That is the case for local variables and private fields. Public fields should use Pascal casing.
ok thanks! found a nice table on github aswell
 
And be sure to avoid @andrewmanuja 's crazy (and incorrect) use of Hungarian notation. Firstly, the .NET Framework's recommendation is to not use Hungarian at all, but a lot of VB6 programmers going into VB.NET seem carry over the Hungarian.

Should you decide to use Hungarian notation because you find that it's the best thing since sliced bread -- well it was back when you were doing code reviews on paper and didn't have IDEs that could do symbol lookups -- be consistent about how you use it, and use the correct prefixes for the appropriate types. Personally, I would recommend avoiding Hungarian.
 
Hi Skydiver,

Look, I am still a beginner for programming and of course will tend to make mistakes. I assume, any beginner would have done mistakes and people learn things by doing mistakes too.

Thus, an expert's job shouldn't be to criticize people's mistakes and instead guide them.

During my learning process (since I am not following any programming course) , I heavily depend on the articles on the internet and sometimes youtube videos as well.

As a result, there could be a higher probability where I could be misled to incorrect directions.

Thanks.

Andrew
 
And be sure to avoid @andrewmanuja 's crazy (and incorrect) use of Hungarian notation. Firstly, the .NET Framework's recommendation is to not use Hungarian at all, but a lot of VB6 programmers going into VB.NET seem carry over the Hungarian.

Should you decide to use Hungarian notation because you find that it's the best thing since sliced bread -- well it was back when you were doing code reviews on paper and didn't have IDEs that could do symbol lookups -- be consistent about how you use it, and use the correct prefixes for the appropriate types. Personally, I would recommend avoiding Hungarian.

This is what im trying to follow but sometimes i just finish writing all the code before i rename everything after this, i think thats a lot faster while im still learning. Found this on github anyway..
namingstandars.png
 
Back
Top Bottom