Doubt in inheritance exercise in C#

Asked

Viewed 1,395 times

6

I’m trying to solve an inheritance exercise and I found a question that I saw that you have a chance to show up in other places and so I thought I should ask here. The exercise asked to first create a class Voo which represents an aeroplane flight and is capable of handling seat occupancy information, part of which is that each flight has a maximum of 100 seats.

Then it is asked to create an heiress class that allows the definition of the number of maximum chairs and to split the plane into smokers and non-smokers. Regarding the part of smokers I know how to implement. What I don’t know is the variable number of chairs because of the way I implemented the base class.

As there are 100 seats, I created an array of 100 size bool in the base class in which the i-nth entry is true if the seat i is busy. I basically did this by initializing the array in the constructor as follows:

public class Voo
{
    private bool[] ocupacaoAssentos;

    public int Numero { get; private set; }

    public Data Data { get; private set; }

    public Voo(int numeroVoo, Data data)
    {
        this.Numero = numeroVoo;

        this.Data = data;

        this.ocupacaoAssentos = new bool[100];
    }
}

So the methods manipulate this array. The problem is that the class heiress would not have been able to modify this. As far as I know, in C# the heiress class constructor always calls the base class constructor.

A possible solution (in C#) would implement this with a collection class that does not have fixed size and then create a static readonly field that says the maximum of seats and then modify it in the heiress class and from there do checks so that everything stays within the limits. I don’t know if the exercise point would be valid, because the exercise is in Java and I don’t know if Java has these collection classes like C#.

But what if I couldn’t modify the base class for some reason or for some other detail of the problem I just couldn’t apply such a solution? How could I handle it? This is a case that seems to me that the base class is more general than the heiress class.

  • 2

    I don’t know if I understand, but the question of the daughter class having to manipulate the array (I don’t know if this is really necessary, I didn’t do this analysis), you could change the array to be proteceted, so the daughter would have access. You have to think about it, there are few cases that a protected field is really necessary. In C# it is much more common to use a collection (a list for example) than an array. An array can be considered implementation detail or premature optimization in some cases. The base class has to be more general even.

  • So @bigown, switching to the collection class seemed to me something better done, but the solution of having a static readonly field saying the maximum seems bad too, because it would have to be protected. In this case, I thought I would do it differently: by the number of variable seats in the base class with standard 100 and then use the daughter class only to implement the functionality of smokers. So it would make more sense to inherit?

2 answers

4

There are several ways to do this. One simple way is to create a new constructor that allows defining the number of seats:

public Voo(int numeroVoo, int numeroAssentos, Data data)
{
   ...
   this.ocupacaoAssentos = new bool[numeroAssentos];
   ...
}

In the heiress class invoke this new constructor using ":base()":

public ClasseHerdeira(int numeroVoo, int numeroAssentos, Data data)
{
   :base(numeroVoo, numeroAssentos, data) { }
}

Another way is to create a setNumerAssent() method in the Flight class and invoke this method in the heiress class constructor.

2

To access the inherited constructor use :base that you will access the class builders

The keyword base is used to access the members of the base class from within a derived class (Translation).

Reference: base (C# Reference)

Example

Base Class

public class Voo
{
    private bool[] ocupacaoassentos;
    public int Numero { get; private set; }
    public DateTime Data { get; private set; }

    public Voo()
    {
        this.Numero = 0;
        this.Data = DateTime.Now;
        this.ocupacaoassentos = new bool[100];
    }
    public Voo(int NumeroVoo, bool[] OcupacaoAssentos)
    {
        this.Numero = NumeroVoo;
        this.Data = DateTime.Now;
        this.ocupacaoassentos = OcupacaoAssentos;
    }
    public Voo(int NumeroVoo, DateTime Data, bool[] OcupacaoAssentos)
    {
        this.Numero = NumeroVoo;
        this.Data = Data;
        this.ocupacaoassentos = OcupacaoAssentos;
    }               
}

Derivative Class (Class Inheritance Flight)

public class HerdaVoo: Voo
{
    //Construtor sem paramentros igual classe base
    public HerdaVoo()
        :base() { }

    //Construtor com paramentro seguindo o construtor da classe base
    public HerdaVoo(int NumeroVoo, bool[] OcupacaoAssentos)
        : base(NumeroVoo, OcupacaoAssentos) { }

    //Construtor com paramentro seguindo o construtor da classe base
    public HerdaVoo(int NumeroVoo, DateTime Data, bool[] OcupacaoAssentos)
        :base(NumeroVoo, Data, OcupacaoAssentos) { }
}

Notice that it will access the 3 constructors of the base class note in the figure:

inserir a descrição da imagem aqui

Coding

HerdaVoo herda = new HerdaVoo(100, DateTime.Now, new bool[500]);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.