Advertisement
huutho_96

Giải Đề OOP 2

Jun 1st, 2015
1,051
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.55 KB | None | 0 0
  1. //https://www.facebook.com/pages/C%C3%B9ng-h%E1%BB%8Dc-l%E1%BA%ADp-tr%C3%ACnh/632038696941833
  2.  
  3. #include <iostream>
  4. #include <string>
  5. using namespace std;
  6.  
  7. class Ve
  8. {
  9.  
  10. protected:
  11.     string MaVe, HoTen;
  12.     int NamSinh, SoTroChoi;
  13. public:
  14.     Ve(string MaVe = "", string HoTen = "", int NamSinh = 0, int SoTroChoi = 0)
  15.     {
  16.         this->HoTen = HoTen;
  17.         this->MaVe = MaVe;
  18.         this->NamSinh = NamSinh;
  19.         this->SoTroChoi = SoTroChoi;
  20.     }
  21.     virtual void Nhap()
  22.     {
  23.         fflush(stdin);
  24.         cout << "Nhap ma ve: ";
  25.         getline(cin, this->MaVe);
  26.         cout << "Nhap ten: ";
  27.         getline(cin, this->HoTen);
  28.         cout << "Nhap nam sinh: ";
  29.         cin >> this->NamSinh;
  30.     }
  31.     virtual int TienVe() = 0;
  32.     virtual int LayLoaiVe() = 0;
  33.     virtual ~Ve(){}
  34. };
  35.  
  36. class VeTronGoi : public Ve
  37. {
  38. public:
  39.     VeTronGoi(string MaVe = "", string HoTen = "", int NamSinh = 0) : Ve(MaVe, HoTen, NamSinh, 0)
  40.     {
  41.     }
  42.     ~VeTronGoi(){}
  43.  
  44.     void Nhap()
  45.     {
  46.         Ve::Nhap();
  47.     }
  48.     int TienVe()
  49.     {
  50.         return 200000;
  51.     }
  52.     int LayLoaiVe()
  53.     {
  54.         return 0;
  55.     }
  56. };
  57.  
  58. class VeTungPhan : public Ve
  59. {
  60. public:
  61.     VeTungPhan(string MaVe = "", string HoTen = "", int NamSinh = 0, int SoTroChoi = 0) : Ve(MaVe, HoTen, NamSinh, SoTroChoi)
  62.     {
  63.     }
  64.     ~VeTungPhan(){}
  65.  
  66.     void Nhap()
  67.     {
  68.         Ve::Nhap();
  69.         cout << "Nhap so tro choi: ";
  70.         cin >> this->SoTroChoi;
  71.     }
  72.    
  73.     int TienVe()
  74.     {
  75.         return (70000 + 20000 * this->SoTroChoi);
  76.     }
  77.     int LayLoaiVe()
  78.     {
  79.         return 1;
  80.     }
  81. };
  82.  
  83. class DSVe
  84. {
  85.     Ve **dsVe;
  86.     int SoVe;
  87.     int TongTien;
  88. public:
  89.     DSVe()
  90.     {
  91.         do
  92.         {
  93.             cout << "So ve can nhap: ";
  94.             cin >> this->SoVe;
  95.         } while (this->SoVe < 0);
  96.  
  97.         TongTien = 0;
  98.         dsVe = new Ve*[this->SoVe];
  99.     }
  100.     ~DSVe()
  101.     {
  102.         delete[] dsVe;
  103.     }
  104.     int NhapLoaiVe()
  105.     {
  106.         int a;
  107.         do
  108.         {
  109.             cout << "1. Ve tron goi\n2. Ve tung phan\n\tNhap so tuong ung: ";
  110.             cin >> a;
  111.             if (a == 1 || a == 2) return (a - 1);
  112.         } while (1);
  113.     }
  114.     void Nhap()
  115.     {
  116.         for (int i = 0; i < SoVe; i++)
  117.         {
  118.             cout << "Ve thu " << i + 1 << ": \n";
  119.             switch (NhapLoaiVe())
  120.             {
  121.             case 0:
  122.                 dsVe[i] = new VeTronGoi;
  123.                 break;
  124.             case 1:
  125.                 dsVe[i] = new VeTungPhan;
  126.                 break;
  127.             }
  128.             dsVe[i]->Nhap();
  129.         }
  130.     }
  131.     int TinhTongTien()
  132.     {
  133.         for (int i = 0; i < this->SoVe; i++)
  134.             TongTien += dsVe[i]->TienVe();
  135.         return this->TongTien;
  136.     }
  137.     int LayTongTien()
  138.     {
  139.         return this->TongTien;
  140.     }
  141.     int SoVeTungPhan()
  142.     {
  143.         int s = 0;
  144.         for (int i = 0; i < this->SoVe; i++)
  145.         {
  146.             if (dsVe[i]->LayLoaiVe() == 1)
  147.                 s++;
  148.         }
  149.         return s;
  150.     }
  151. };
  152.  
  153. void main()
  154. {
  155.     DSVe DS;
  156.     DS.Nhap();
  157.     cout << DS.TinhTongTien() << endl;
  158.     cout << DS.SoVeTungPhan();
  159. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement