Facebook
From Rude Dormouse, 3 Years ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 156
  1. #include <iostream>
  2. #include <vector>
  3. #include <memory>
  4. #include <string>
  5.  
  6. class Vehicle {
  7. public:
  8.    std::string name() const { return name_; }
  9.    int number_of_wheels() const { return number_of_wheels_; }
  10.  
  11.    virtual ~Vehicle() = default;
  12.  
  13. protected:
  14.    Vehicle(const std::string &name, int number_of_wheels)
  15.        : name_(name), number_of_wheels_(number_of_wheels) {}
  16.  
  17.    std::string name_;
  18.    int number_of_wheels_;
  19. };
  20.  
  21. class Car : public Vehicle{
  22. public:
  23.    Car(const std::string  &name, int horsepower)
  24.        : Vehicle(name, 4), horsepower_(horsepower) {}
  25.  
  26.    int horsepower() const { return horsepower_; }
  27.  
  28. private:
  29.    int horsepower_;
  30. };
  31.  
  32. class Bike : public Vehicle {
  33. public:
  34.    Bike(const std::string &name, bool has_derailleur)
  35.        : Vehicle(name, 2), has_derailleur_(has_derailleur) {}
  36.  
  37.    bool has_derailleur() { return has_derailleur_; }
  38.  
  39. private:
  40.    bool has_derailleur_;
  41. };
  42.  
  43. void display_vehicle_info(std::vector<std::unique_ptr<Vehicle>> a){
  44.     for (int i = 0; i <=5; i++) {
  45.         if(typeid (a) == typeid (Car)){
  46.             Car &some_car = dynamic_cast<Car &>(*a[i]);
  47.             std::cout << "car: " << some_car.name() << "has: " << some_car.horsepower() << " HP" << std::endl;
  48.         }
  49.         if(typeid (a) == typeid (Bike)){
  50.             Bike &some_bike = dynamic_cast<Bike &>(*a[i]);
  51.             std::cout << "bike: " << some_bike.name() << " - derailleur: " << some_bike.has_derailleur() << std::endl;
  52.         }
  53.     }
  54. }
  55.  
  56. //        if(typeid (a) == typeid (Car)){
  57. //        Car *some_car = dynamic_cast<Car *>(a[i].get());
  58. //        if (some_car != nullptr) { // cast successful
  59. //            std::cout << i << ": HP" << some_car->horsepower() << std::endl;
  60. //        } else { // nope
  61. //            std::cout << i << ": not a Car" << std::endl;
  62. //        }
  63. //        }
  64. //        if(typeid (a) == typeid (Bike)){
  65. //            Bike *some_bike = dynamic_cast<Bike *>(a[i].get());
  66. //            if (some_bike != nullptr) { // cast successful
  67. //                std::cout << i << ": H_D" << some_bike->has_derailleur() << std::endl;
  68. //            } else { // nope
  69. //                std::cout << i << ": not a Bike" << std::endl;
  70. //            }
  71. //        }
  72.  
  73. int main()
  74. {
  75.    std::vector<std::unique_ptr<Vehicle>> vehicles;
  76.  
  77.    vehicles.emplace_back(std::make_unique<Bike>("Fixie", false));
  78.    vehicles.emplace_back(std::make_unique<Car>("Porsche", 400));
  79.    vehicles.emplace_back(std::make_unique<Bike>("Road Bike", true));
  80.    vehicles.emplace_back(std::make_unique<Bike>("MTB", true));
  81.    vehicles.emplace_back(std::make_unique<Car>("Passat", 115));
  82.  
  83.    display_vehicle_info(vehicles);
  84. }
  85.