Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@toanalien
Created June 4, 2015 07:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save toanalien/39c67d86f5c1eecedd4f to your computer and use it in GitHub Desktop.
Save toanalien/39c67d86f5c1eecedd4f to your computer and use it in GitHub Desktop.
OOP 2rd Term test
#include <iostream>
#include <string>
using namespace std;
class Ve
{
protected:
string MaVe, HoTen;
int NamSinh, SoTroChoi;
public:
Ve(string MaVe = "", string HoTen = "", int NamSinh = 0, int SoTroChoi = 0)
{
this->HoTen = HoTen;
this->MaVe = MaVe;
this->NamSinh = NamSinh;
this->SoTroChoi = SoTroChoi;
}
virtual void Nhap()
{
fflush(stdin);
cout << "Nhap ma ve: ";
getline(cin, this->MaVe);
cout << "Nhap ten: ";
getline(cin, this->HoTen);
cout << "Nhap nam sinh: ";
cin >> this->NamSinh;
}
virtual int TienVe() = 0;
virtual int LayLoaiVe() = 0;
virtual ~Ve(){}
};
class VeTronGoi : public Ve
{
public:
VeTronGoi(string MaVe = "", string HoTen = "", int NamSinh = 0) : Ve(MaVe, HoTen, NamSinh, 0)
{
}
~VeTronGoi(){}
void Nhap()
{
Ve::Nhap();
}
int TienVe()
{
return 200000;
}
int LayLoaiVe()
{
return 0;
}
};
class VeTungPhan : public Ve
{
public:
VeTungPhan(string MaVe = "", string HoTen = "", int NamSinh = 0, int SoTroChoi = 0) : Ve(MaVe, HoTen, NamSinh, SoTroChoi)
{
}
~VeTungPhan(){}
void Nhap()
{
Ve::Nhap();
cout << "Nhap so tro choi: ";
cin >> this->SoTroChoi;
}
int TienVe()
{
return (70000 + 20000 * this->SoTroChoi);
}
int LayLoaiVe()
{
return 1;
}
};
class DSVe
{
Ve **dsVe;
int SoVe;
int TongTien;
public:
DSVe()
{
do
{
cout << "So ve can nhap: ";
cin >> this->SoVe;
} while (this->SoVe < 0);
TongTien = 0;
dsVe = new Ve*[this->SoVe];
}
~DSVe()
{
delete[] dsVe;
}
int NhapLoaiVe()
{
int a;
do
{
cout << "1. Ve tron goi\n2. Ve tung phan\n\tNhap so tuong ung: ";
cin >> a;
if (a == 1 || a == 2) return (a - 1);
} while (1);
}
void Nhap()
{
for (int i = 0; i < SoVe; i++)
{
cout << "Ve thu " << i + 1 << ": \n";
switch (NhapLoaiVe())
{
case 0:
dsVe[i] = new VeTronGoi;
break;
case 1:
dsVe[i] = new VeTungPhan;
break;
}
dsVe[i]->Nhap();
}
}
int TinhTongTien()
{
for (int i = 0; i < this->SoVe; i++)
TongTien += dsVe[i]->TienVe();
return this->TongTien;
}
int LayTongTien()
{
return this->TongTien;
}
int SoVeTungPhan()
{
int s = 0;
for (int i = 0; i < this->SoVe; i++)
{
if (dsVe[i]->LayLoaiVe() == 1)
s++;
}
return s;
}
};
void main()
{
DSVe DS;
DS.Nhap();
cout << DS.TinhTongTien() << endl;
cout << DS.SoVeTungPhan();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment