Advertisement
Guest User

Untitled

a guest
May 3rd, 2015
256
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.67 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4.  
  5. class Vehicle
  6. {
  7. public:
  8. virtual std::string compareTo(Vehicle* v) = 0;
  9. };
  10.  
  11. class Bicycle : public Vehicle
  12. {
  13. public:
  14. Bicycle() { color_ = "red"; }
  15. std::string compareTo(Vehicle* v) { return "We're different vehicles."; }
  16. std::string compareTo(Bicycle* b) { return color_.compare(b->color_) ? "We're different bicycles." : "We're the same bicycle."; }
  17.  
  18. private:
  19. std::string color_;
  20. };
  21.  
  22. class Car : public Vehicle
  23. {
  24. public:
  25. Car() { style_ = "sedan"; }
  26. std::string compareTo(Vehicle* v) { return "We're different vehicles."; }
  27. std::string compareTo(Car* c) { return style_.compare(c->style_) ? "We're different cars." : "We're the same car."; }
  28.  
  29. private:
  30. std::string style_;
  31. };
  32.  
  33. int main()
  34. {
  35. Vehicle* compareFrom = new Bicycle();
  36.  
  37. std::vector<Vehicle*> compareTos;
  38. compareTos.push_back(new Bicycle());
  39. compareTos.push_back(new Car());
  40.  
  41. std::vector<Vehicle*>::iterator it;
  42. for (it = compareTos.begin(); it != compareTos.end(); ++it)
  43. std::cout << compareFrom->compareTo(*it) << std::endl;
  44.  
  45. return 0;
  46. }
  47.  
  48. std::string Bicycle::compareTo(Vehicle* v) {
  49. if (Bicycle* b = dynamic_cast<Bicycle*>(v)) {
  50. return compareTo(b);
  51. } else {
  52. return "We're different vehicles.";
  53. }
  54. }
  55.  
  56. #include <iostream>
  57. #include <iomanip>
  58. #include <string>
  59. #include <typeinfo>
  60.  
  61. struct vehicle {
  62. virtual bool compare_to(vehicle const& other) const = 0;
  63. };
  64.  
  65. bool vehicle::compare_to(vehicle const& other) const {
  66. return typeid(*this) == typeid(other);
  67. }
  68.  
  69. struct car : vehicle {
  70. std::string color;
  71.  
  72. car(std::string const& color) : color(color) { }
  73.  
  74. bool compare_to(vehicle const& other) const {
  75. bool result = vehicle::compare_to(other);
  76. return result and (color == static_cast<car const&>(other).color);
  77. }
  78. };
  79.  
  80. struct bike : vehicle {
  81. int spokes;
  82.  
  83. bike(int spokes) : spokes(spokes) { }
  84.  
  85. bool compare_to(vehicle const& other) const {
  86. bool result = vehicle::compare_to(other);
  87. return result and (spokes == static_cast<bike const&>(other).spokes);
  88. }
  89. };
  90.  
  91. int main() {
  92. car c1("blue");
  93. car c2("red");
  94. bike b1(42);
  95.  
  96. std::cout << std::boolalpha;
  97. std::cout << c1.compare_to(c2) << "n"
  98. << c1.compare_to(b1) << "n"
  99. << c1.compare_to(c1) << "n";
  100. }
  101.  
  102. enum Kind {
  103. HUMAN_POWERED = (0x1 << 0),
  104. MOTOR_POWERED = (0x1 << 1),
  105. BICYCLE = (0x1 << 2) | HUMAN_POWERED,
  106. UNICYCLE = (0x1 << 3) | HUMAN_POWERED,
  107. CAR = (0x1 << 4) | MOTOR_POWERED
  108. };
  109.  
  110. bool areSameClass (Vehicle const & lhs, Vehicle const & rhs)
  111. {
  112. return (lhs->getKind () & rhs->getKind ()) & (HUMAN_POWERED | MOTOR_POWERED);
  113. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement