Hi,
I'm putting together a presentation to show some of the productivity gains that I think C# may offer over C++. I've written some code in C# which I'd like to convert to C++. I don't have the time or up to date C++ to do the conversion justice, hence posting here.
Code to convert:
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
namespace LinqTest
{
public class Vehicle
{
public int Id { get; set; }
public Color Colour { get; set; }
}
public class Tyre
{
public int Id { get; set; }
public int BikeId { get; set; }
public int Size { get; set; }
public string Brand { get; set; }
}
public class Car : Vehicle {}
public class Bike : Vehicle {}
public class Example
{
private readonly IList<Car> cars;
private readonly IList<Tyre> bikeTyres;
private readonly IList<Bike> bikes;
public Example()
{
cars = new List<Car>
{
new Car {Id = 0, Colour = Color.Red},
new Car {Id = 1, Colour = Color.Blue},
new Car {Id = 2, Colour = Color.Green},
new Car {Id = 3, Colour = Color.Green}
};
bikeTyres = new List<Tyre>
{
new Tyre {Id = 0, BikeId = 0, Brand = "TyreCo1", Size = 23},
new Tyre {Id = 1, BikeId = 0, Brand = "TyreCo1", Size = 23},
new Tyre {Id = 2, BikeId = 1, Brand = "TyreCo2", Size = 30},
new Tyre {Id = 3, BikeId = 1, Brand = "TyreCo2", Size = 30},
new Tyre {Id = 4, BikeId = 2, Brand = "TyreCo3", Size = 23},
new Tyre {Id = 5, BikeId = 2, Brand = "TyreCo3", Size = 23}
};
bikes = new List<Bike>
{
new Bike {Id = 0, Colour = Color.Red},
new Bike {Id = 1, Colour = Color.Blue},
new Bike {Id = 2, Colour = Color.Green}
};
}
public IEnumerable<Vehicle> FindVehiclesByColour(Color colour)
{
var carVehicles = from car in cars
where car.Colour == colour
select car as Vehicle;
var bikeVehicles = from bike in bikes
where bike.Colour == colour
select bike as Vehicle;
return carVehicles.Union(bikeVehicles);
}
public IEnumerable<Bike> FindBikesByTyreSize(int size)
{
return (from bike in bikes
join tyre in bikeTyres on bike.Id equals tyre.BikeId
where tyre.Size == size
select bike).Distinct();
}
}
}
Thanks in advance.