I've just started to learn inheritence of classes. And upon initializing of an inherrited class I want to pass in the existing fields from the base class.
I've tried to inherit the constructor of the base class but can't get my head around Line 15: base(horsepower, color).
I've tried to inherit the constructor of the base class but can't get my head around Line 15: base(horsepower, color).
C#:
class Vehicle
{
protected int Horsepower { get; set; }
protected string Color { get; set; }
public Vehicle(int horsepower = 0, string color = "")
{
Horsepower = horsepower;
Color = color;
}
}
class Truck : Vehicle
{
private int Load { get; set; }
public Truck (int load = 0) : base(horsepower, color)
{
Load = load;
}
}
}
C#:
class Program
{
static void Main(string[] args)
{
Vehicle bil = new Vehicle(300, "Blue");
Truck truck = new Truck();
// How can I pass in the fields from the base class constructor in the sub class constructor?:
// Truck truck = new Truck (100, "Red", 200);
Console.ReadLine();
}
}
Last edited: