0% found this document useful (0 votes)
5 views3 pages

MT

The document is a C++ program that defines a class hierarchy for managing products, specifically a base class 'HANGHOA' for general products and a derived class 'MAYTINH' for computers. It includes functionality for inputting, outputting, and comparing products, as well as calculating shipping costs based on weight. The main function allows users to input product details, display them, and manage a list of computers, including adding new entries and searching by specific criteria.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views3 pages

MT

The document is a C++ program that defines a class hierarchy for managing products, specifically a base class 'HANGHOA' for general products and a derived class 'MAYTINH' for computers. It includes functionality for inputting, outputting, and comparing products, as well as calculating shipping costs based on weight. The main function allows users to input product details, display them, and manage a list of computers, including adding new entries and searching by specific criteria.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

#include <iostream>

#include <cstring>
using namespace std;

// Lớp HANGHOA
class HANGHOA {
protected:
char maHang[20];
char tenHangSX[50];
float donGia;
public:
HANGHOA() {
strcpy(maHang, "");
strcpy(tenHangSX, "");
donGia = 0;
}

HANGHOA(const char* ma, const char* ten, float gia) {


strcpy(maHang, ma);
strcpy(tenHangSX, ten);
donGia = gia;
}

~HANGHOA() {}

virtual void nhap() {


cout << "Nhap ma hang: ";
cin >> maHang;
cout << "Nhap ten hang san xuat: ";
cin.ignore();
cin.getline(tenHangSX, 50);
cout << "Nhap don gia: ";
cin >> donGia;
}

virtual void xuat() {


cout << "Ma hang: " << maHang << endl;
cout << "Hang san xuat: " << tenHangSX << endl;
cout << "Don gia: " << donGia << endl;
}
};

// Lớp MAYTINH kế thừa từ HANGHOA


class MAYTINH : public HANGHOA {
private:
float ram;
float trongLuong;
public:
MAYTINH() : HANGHOA() {
ram = 0;
trongLuong = 0;
}

MAYTINH(const char* ma, const char* ten, float gia, float r, float tl)
: HANGHOA(ma, ten, gia), ram(r), trongLuong(tl) {}

~MAYTINH() {}

// Toán tử nhập
friend istream& operator>>(istream& is, MAYTINH& mt) {
cout << "Nhap ma hang: ";
is >> mt.maHang;
cout << "Nhap ten hang san xuat: ";
is.ignore();
is.getline(mt.tenHangSX, 50);
cout << "Nhap don gia: ";
is >> mt.donGia;
cout << "Nhap dung luong RAM: ";
is >> mt.ram;
cout << "Nhap trong luong: ";
is >> mt.trongLuong;
return is;
}

// Toán tử xuất
friend ostream& operator<<(ostream& os, MAYTINH mt) {
os << "Ma hang: " << mt.maHang << endl;
os << "Hang san xuat: " << mt.tenHangSX << endl;
os << "Don gia: " << mt.donGia << endl;
os << "RAM: " << mt.ram << " GB" << endl;
os << "Trong luong: " << mt.trongLuong << " kg" << endl;
os << "Tien van chuyen: " << mt.tinhTienVanChuyen() << " VND" << endl;
return os;
}

// Hàm tính tiền vận chuyển


float tinhTienVanChuyen() {
if (trongLuong > 2) return 100000;
else if (trongLuong > 1) return 50000;
else return 30000;
}

// Toán tử so sánh RAM


bool operator==(float value) {
return ram == value;
}

// Hàm get hãng sản xuất để tìm kiếm


const char* getHang() {
return tenHangSX;
}
};

// === CHƯƠNG TRÌNH CHÍNH ===


int main() {
int n;
cout << "Nhap so luong hang hoa: ";
cin >> n;

HANGHOA* dsHH = new HANGHOA[n];


for (int i = 0; i < n; i++) {
cout << "--- Nhap hang hoa thu " << i + 1 << " ---" << endl;
dsHH[i].nhap();
}

cout << "\n--- DANH SACH HANG HOA ---\n";


for (int i = 0; i < n; i++) {
cout << "\nHang hoa thu " << i + 1 << ":" << endl;
dsHH[i].xuat();
}

// Nhap danh sach may tinh


cout << "\nNhap so luong may tinh: ";
cin >> n;
MAYTINH* dsMT = new MAYTINH[n];
for (int i = 0; i < n; i++) {
cout << "--- Nhap may tinh thu " << i + 1 << " ---" << endl;
cin >> dsMT[i];
}

cout << "\n--- MAY TINH CO RAM = 16 GB ---\n";


for (int i = 0; i < n; i++) {
if (dsMT[i] == 16)
cout << dsMT[i] << endl;
}

// Tim may tinh hang "Dell"


cout << "\n--- MAY TINH HANG 'Dell' ---\n";
for (int i = 0; i < n; i++) {
if (strcmp(dsMT[i].getHang(), "Dell") == 0)
cout << dsMT[i] << endl;
}

// Them may tinh vao vi tri k


int k;
cout << "\nNhap vi tri k muon chen may tinh moi (0 <= k <= " << n << "): ";
cin >> k;

MAYTINH newMT;
cout << "\nNhap thong tin may tinh moi:\n";
cin >> newMT;

MAYTINH* temp = new MAYTINH[n + 1];


for (int i = 0; i < k; i++)
temp[i] = dsMT[i];
temp[k] = newMT;
for (int i = k; i < n; i++)
temp[i + 1] = dsMT[i];

delete[] dsMT;
dsMT = temp;
n++;

cout << "\n--- DANH SACH MAY TINH SAU KHI THEM ---\n";
for (int i = 0; i < n; i++) {
cout << "\nMay tinh thu " << i + 1 << ":\n";
cout << dsMT[i];
}

// Giải phóng bộ nhớ


delete[] dsHH;
delete[] dsMT;

return 0;
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy