Advertisement
Vladislav_Bezruk

Lab 11 task 1

Oct 27th, 2021
922
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.20 KB | None | 0 0
  1. /*
  2. Створити абстрактний клас «корабель»і похідні від нього «ракетний катер» та «пасажирський лайнер».
  3. Функції введення та виведення даних в базовому класі зробити абстрактними і визначити їх конкретно в похідних класах.
  4. Продемонструвати їхню роботу.
  5. */
  6.  
  7. #include <iostream>
  8. #include <ctime>
  9. #include <stdlib.h>
  10.  
  11. using namespace std;
  12.  
  13. /*class Ship*/
  14. class Ship {
  15.     protected:
  16.         /*capacity*/
  17.         float capacity;
  18.  
  19.     public:
  20.         /*constructor*/
  21.         Ship() {}
  22.  
  23.         /*virtual function set*/
  24.         virtual void set() = 0;
  25.  
  26.         /*virtual function get*/
  27.         virtual void get() = 0;
  28.  
  29.         /*virtual function to get capacity*/
  30.         virtual void getCapacity() = 0;
  31. };
  32.  
  33. /*class RocketBoat based on Ship*/
  34. class RocketBoat : virtual public Ship {
  35.     private:
  36.         /*count of rockets*/
  37.         int rocketsCount;
  38.  
  39.         /*count of guns*/
  40.         int gunsCount;
  41.  
  42.     public:
  43.         /*constructor*/
  44.         RocketBoat() : Ship() {}
  45.  
  46.         /*function set*/
  47.         void set() {
  48.             cout << "===Enter info about rocket boat===" << endl;
  49.             cout << "\tcount of rockets: "; cin >> rocketsCount;
  50.             cout << "\tount of guns: "; cin >> gunsCount;
  51.             cout << endl;
  52.         }
  53.  
  54.         /*function get*/
  55.         void get() {
  56.             cout << "===Info about rocket boat===" << endl;
  57.             cout << "\tcount of rockets: " << rocketsCount << endl;
  58.             cout << "\tount of guns: " << gunsCount << endl;
  59.             cout << endl;
  60.         }
  61.  
  62.         /*function to get capacity*/
  63.         void getCapacity() {
  64.             capacity = (rocketsCount * 1.5 + gunsCount * 1.2) * 3;
  65.             cout << "capacity of rocket boat = " << capacity << endl;
  66.             cout << endl;
  67.         }
  68.  
  69. };
  70.  
  71. /*class PassengerLiner based on Ship*/
  72. class PassengerLiner : virtual public Ship {
  73.     private:
  74.         /*count of passengers*/
  75.         int passengersCount;
  76.  
  77.     public:
  78.         /*constructor*/
  79.         PassengerLiner() : Ship() {}
  80.  
  81.         /*function set*/
  82.         void set() {
  83.             cout << "===Enter info about passenger liner===" << endl;
  84.             cout << "\tcount of passengers: "; cin >> passengersCount;
  85.             cout << endl;
  86.         }
  87.  
  88.         /*function get*/
  89.         void get() {
  90.             cout << "===Info about passenger liner===" << endl;
  91.             cout << "\tcount of passengers: " << passengersCount << endl;
  92.             cout << endl;
  93.         }
  94.  
  95.         /*function to get capacity*/
  96.         void getCapacity() {
  97.             capacity = passengersCount * 6;
  98.             cout << "capacity of passenger liner = " << capacity << endl;
  99.             cout << endl;
  100.         }
  101. };
  102.  
  103. int main() {
  104.    
  105.     Ship* ship;
  106.  
  107.     /*class RocketBoat*/
  108.     ship = new RocketBoat;
  109.  
  110.     ship->set();
  111.     ship->get();
  112.     ship->getCapacity();
  113.  
  114.     /*class PassengerLiner*/
  115.     ship = new PassengerLiner;
  116.  
  117.     ship->set();
  118.     ship->get();
  119.     ship->getCapacity();
  120.  
  121.     return 0;
  122. }
  123.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement