Fork Copy #include #include using namespace std; class Hang { private: int maHang; int loaiHang; char xuatSu[50]; long gia; public: Hang() { maHang = 0; loaiHang = 0; strcpy(xuatSu, ""); gia = 0; } Hang(int mh, int lh, char xs[], long g) { maHang = mh; loaiHang = lh; strcpy(xuatSu, xs); gia = g; } void nhap(); void xuat(); int layLoaiHang() { return loaiHang; } }; void Hang::nhap() { cout << "Ma hang: "; cin >> maHang; cout << "Loai hang: "; cin >> loaiHang; cout << "Xuat su: "; cin.ignore(1); cin.get(xuatSu, 50); cout << "Gia: "; cin >> gia; } void Hang::xuat() { cout << "Ma hang: " << maHang << endl; cout << "Loai hang: " << loaiHang << endl; cout << "Xuat su: " << xuatSu << endl; cout << "Gia: " << gia << endl << endl; } int main() { Hang *ds; int n; cout << "Nhap so mat hang: "; cin >> n; ds = new Hang[n + 1]; for (int i = 1; i <= n; i++) { cout << endl << "Nhap mat hang thu " << i << endl; ds[i].nhap(); } cout << endl << "Danh sach cac mat hang vua nhap" << endl << endl; for (int i = 1; i <= n; i++) { ds[i].xuat(); } //dem so luong int dem1, dem2, dem3, dem4; dem1 = dem2 = dem3 = dem4 = 0; for (int i = 1; i <= n; i++) { if (ds[i].layLoaiHang() == 1) dem1++; else if (ds[i].layLoaiHang() == 2) dem2++; else if (ds[i].layLoaiHang() == 3) dem3++; else dem4++; } cout << endl << "So mat hang cua moi loai:" << endl << endl; cout << "Loai 1: " << dem1 << endl; cout << "Loai 2: " << dem2 << endl; cout << "Loai 3: " << dem3 << endl; cout << "Loai 4: " << dem4 << endl; }