Multiple Inheritance and Heterogeneous Collection in C#

587 Views Asked by At

struggling with an assignment targeted at translating something that works in C++ into C#.

I am aware that true multiple inheritance is not supported in C#, but that it may be simulated through interfaces and composition.

In C++ I have a design where two classes inherit from one parent class. Let's say Vehicle is the parent class while Car and Boat inherit from Vehicle.

There a second class from which three more classes are derived. Let's call this License. The three additional classes inherit from both the three Vehicle classes AND the License class.

So we have something like:

    // C++
    class Vehicle
    {
     public:
         virtual void move();
    };

    class Car : public Vehicle
    {
     public:
         void move();
    };

    class Boat : public Vehicle
    {
     public:
         void move();
    };

    class License
    {
     public:
          void renew(); 
    };

    class LicensedVehicle : public Vehicle, public License {...};
    class LiscensedCar : public Car, public License {...};
    class LisencedBoat : public Boat, public License {...};

Now in my main method I can create objects for the three multiply inherited classes above and add them to two arrays of pointers one of type Vehicle and one of type License. I can then access the Vehicle half of the functionality from one array and the License functionality from the other. For example:

    // C++
    Vehicle* VehicleHeteroArray[3];
    License* LiscenseHeteroArray[3];

    LisencedVehicle* lvPtr = new LisencedVehicle();
    LisencedCar* lcPtr = new LisencedCar();
    LisencedBoat* lbPtr = new LisencedBoat();

    VehicleHeteroArray[0] = lvPtr;
    LiscenseHeteroArray[0] = lcPtr;

    VehicleHeteroArray[1] = lcPtr;
    LiscenseHeteroArray[1] = lcPtr;

    VehicleHeteroArray[2] = lbPtr;
    LiscenseHeteroArray[2] = lbPtr;

Now I can loop through these two arrays and access every method in Vehicle and License. For instance:

    // C++
   for (int i = 0; i < 3; i++)
   {
      VehicleHeteroArray[i].move();
      LiscenseHeteroArray[i].renew();
   }

Now this works fine in C++, so my question regards translating this into C#. Is there any reasonable way of doing something similar?

So far I have my three vehicle classes set up and my three "simulated multiple inheritance" classes set up by doing something like:

    // C#  
    class Vehicle
    {
         public virtual void move();
    }

    class Car : Vehicle
    {
     public override void move();
    }

    class Boat : Vehicle
    {
     public override void move();
    }

    interface ILicense
    {
      void renew();
    }        

    class License : ILicense
    {
     public void renew(); 
    }

    class LicensedVehicle : Vehicle, ILicense 
    {
         License l;

         void ILicense.renew()
         {
              l.renew();
         }
    }

    class LiscensedCar : Car, ILicense {...}
    class LisencedBoat : Boat, ILicense {...}

I know I can create a heterogeneous array of type Vehicle and access move(), but is there any way to have a heterogeneous collection to also access renew() like I did in C++ with LiscenseHeteroArray[]?

0

There are 0 best solutions below