简体   繁体   中英

Inheritance of a base class and 2 derived classes

I am having all kind of difficulty getting this racer program to work. I have an abstract racer class with a abstract method and then two other classes that are derived from it.

I got an array in my main program class but errors coming up saying the index [0] and [1]

Array size cannot be specified in a variable declaration (try initializing with a 'new' expression)

Then an error for the = sign saying
Invalid token '=' in class, struct, or interface member declaration

The the new Racer must return a type

The main class is

public class Program
{
    ApplicationUtilities.DisplayApplicationInformation();
    ApplicationUtilities.DisplayDivider("Start Racer Program");
    ApplicationUtilities.DisplayDivider("Prompt for Racer information and create first Racer");

        Racer[] myarray = new Racer[2];
        myarray[0] = new Racer(HotRod);
        myarray[1] = new Racer(StreetTuner);

    public CollectRacerInformation(HotRod);
}

The abstract Racer class is

public abstract class Racer
{
    private string racerName;
    private int racerSpeed;
    private Engine engine;

    public Racer();

    public Racer(string name, int speed, Engine engine);


    Engine engine();
    public abstract bool IsDead();
}

My derived HotRod class is

public class HotRod : Racer
{
    private bool blower = true || false;

    public HotRod();

    public HotRod(string name, int speed, Engine engine);


    public override bool IsDead()
    {
        Engine engine1 = new Engine();
        engine1 = Engine1;
        Random rnd = new Random();
        rnd.NextDouble();
        bool dead = false;

        if (racerSpeed > 50 && rnd.NextDouble() > 0.6)
            if (engine1.horsePower < 300 && blower == true)
                dead = false;
            else
                dead = true;

        else if (racerSpeed > 100 && rnd.NextDouble() > 0.4)

            if (engine1.horsePower >= 300 && blower == true)
                dead = true;
            else
                dead = false;
        else
            dead = false;

        return dead;
    }

    public override string ToString()
    {
        string output;

        output = "\n============ HotRod Information ============";
        output += "\n\t              Racer's Name:\t" + racerName;
        output += "\n\t               Car's Speed:\t" + carSpeed;
        output += "\n\t          Engine Cylinders:\t" + engineCylinders;
        output += "\n\t         Engine Horsepower:\t" + engineHorsePower;
        output += "n\t               Racer's Type:\t" + racerType;
        output += "n\t          Racer with Blower:\t" + carBlower;
        output += "n\t             Still Working?:\t" + IsDead;

        return output;
    }
}

Then my derived StreetTuner class is

public class StreetTuner : Racer
{
    private bool nitrous;

    public StreetTuner();

    public StreetTuner(string name, int speed, Engine engine);

    public bool IsDead
    {
        get { return IsDead; }
        set
        {
            if (speed > 50 && rnd.NextDouble() > 0.6)
                if (horsePower < 300 && blower == true)
                    IsDead = false;
                else
                    IsDead = true;
            else if (speed > 100 && rnd.NextDouble() > 0.4)
                if (horsePower >= 300 && blower == true)
                    IsDead = true;
                else
                    IsDead = false;
        }
    }
}

Original Answer

I think you mean

    Racer[] myarray = new Racer[2];
    myarray[0] = new HotRod();
    myarray[1] = new StreetTuner();

Updated answer

after seeing your actual code, your main program needs some work. First of all, code needs to be in methods , nor directly in classes . Secondly, you're using classes as if they're variables . I suspect you want something like this:

public class Program
{
    public static void Main()
    {
        ApplicationUtilities.DisplayApplicationInformation();
        ApplicationUtilities.DisplayDivider("Start Racer Program");
        ApplicationUtilities.DisplayDivider("Prompt for Racer information and create first Racer");

        Racer[] myarray = new Racer[2];
        myarray[0] = new HotRod();
        myarray[1] = new StreetTuner();

        CollectRacerInformation(myarray[0]);
    }

}

Derived classes have an "is-a" relationship to their base class. So in your case, a HotRod is a Racer and a StreetTuner is a Racer , therefore when you declare your array to be of type Racer , then anything that is a Racer can be put into that array, thus this:

var myarray = new Racer[2];
myarray[0] = new HotRod();
myarray[1] = new StreetTuner();

You instantiate the derived types explicitly, but through inheritance they can be used wherever their base type is specified, because they are that type (base class of Racer ) as well as their concrete type ( HotRod or StreetTuner as the case may be).

You need to write your Racer constructors, like this:

// Default constructor
public Racer()
{

}

// Named value constructor
public Racer(string _name, int _speed, Engine _engine)
{
    // Store the name, speed and engine values
    racerName = _name; 
    racerSpeed = _speed; 
    engine = _engine;
}

Same goes for HotRod and StreetTuner constructors as well.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM