ExtremerBG

SimpleClassHierarchy

Jun 3rd, 2024
36
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.13 KB | Source Code | 0 0
  1. #include <iostream>
  2. #include <vector>
  3.  
  4. class mainClassPlanes { //Main Class
  5. public:
  6.     mainClassPlanes(const std::string& planeName, const std::string& originCountry, int originYear)
  7.         : planeName(planeName), originCountry(originCountry), originYear(originYear){}
  8.     virtual ~mainClassPlanes() {} //destructor
  9.     virtual void displayInfo() const = 0;
  10. protected:
  11.     std::string planeName;
  12.     std::string originCountry;
  13.     int originYear;
  14. };
  15. class ww2Planes : public mainClassPlanes { //WW2 Class derived from Main Class
  16. public:
  17.     ww2Planes(const std::string& planeName, const std::string& originCountry, int originYear, const std::string& alliance, int numberBuilt, int planeSeats)
  18.         : alliance(alliance), numberBuilt(numberBuilt), planeSeats(planeSeats), mainClassPlanes(planeName, originCountry, originYear){}
  19.     virtual void displayInfo() const override {
  20.         std::cout << "Plane name: " << planeName << std::endl;
  21.         std::cout << "Country of origin: " << originCountry << std::endl;
  22.         std::cout << "Year of origin: " << originYear << std::endl;
  23.         std::cout << "Alliance: " << alliance << std::endl;
  24.         std::cout << "Number built: " << numberBuilt << std::endl;
  25.         std::cout << "Plane seats: " << planeSeats << std::endl;
  26.     }; //display
  27. protected:
  28.     std::string alliance;
  29.     int numberBuilt;
  30.     int planeSeats;
  31. };
  32. class ww1Planes : public mainClassPlanes { //WW1 Class derived from Main Class
  33. public:
  34.     ww1Planes(const std::string& planeName, const std::string& originCountry, int originYear, const std::string& alliance, int numberBuilt, float size)
  35.         : alliance(alliance), numberBuilt(numberBuilt), size(size), mainClassPlanes(planeName, originCountry, originYear){}
  36.     virtual void displayInfo() const override {
  37.         std::cout << "Plane name: " << planeName << std::endl;
  38.         std::cout << "Country of origin " << originCountry << std::endl;
  39.         std::cout << "Year of origin: " << originYear << std::endl;
  40.         std::cout << "Alliance: " << alliance << std::endl;
  41.         std::cout << "Number built: " << numberBuilt << std::endl;
  42.         std::cout << "Plane size: " << size << " meters" << std::endl;
  43.     }; //display
  44. protected:
  45.     std::string alliance;
  46.     int numberBuilt;
  47.     float size;
  48. };
  49.  
  50. int main()
  51. {
  52.     ww2Planes bf109("Messerschmitt Bf 109", "Nazi Germany", 1937, "Axis", 34248, 2);
  53.     ww2Planes yak9("Yakovlev Yak-9", "Soviet Union", 1942, "Allies", 16769, 1);
  54.     ww1Planes fokkerDVII("Fokker D.VII", "Germany", 1918, "Central Powers", 3300, 6.95);
  55.     ww1Planes bristolF2("Bristol F.2", "United Kingdom", 1916, "Entente Powers", 5329, 7.87);
  56.     std::vector<std::shared_ptr<mainClassPlanes>> planes;
  57.     planes.push_back(std::make_shared<ww2Planes>(bf109));
  58.     planes.push_back(std::make_shared<ww2Planes>(yak9));
  59.     planes.push_back(std::make_shared<ww1Planes>(fokkerDVII));
  60.     planes.push_back(std::make_shared<ww1Planes>(bristolF2));
  61.  
  62.     for (const auto& mainClassPlanes : planes)
  63.     {
  64.         mainClassPlanes->displayInfo();
  65.         std::cout << "-------------------------------------" << std::endl;
  66.     }
  67.     return 0;
  68. }
Add Comment
Please, Sign In to add comment