Untitled
user_4737693351
plain_text
3 years ago
5.5 kB
2
Indexable
#include <bits/stdc++.h> using namespace std; class HinhHoc { private: int a, b; public: HinhHoc() { a = 0; b = 0; }; HinhHoc(int a, int b) { this->a = a; this->b = b; } // Qua tai ++,--,*,/,+,- HinhHoc operator++() { ++a; ++b; return *this; } HinhHoc operator++(int) { HinhHoc temp = *this; ++a; ++b; return temp; } HinhHoc operator--() { --a; --b; return *this; } HinhHoc operator--(int) { HinhHoc temp = *this; --a; --b; return temp; } HinhHoc operator*(HinhHoc &obj) { HinhHoc temp; temp.a = a * obj.a; temp.b = b * obj.b; return temp; } HinhHoc operator/(HinhHoc &obj) { HinhHoc temp; temp.a = a / obj.a; temp.b = b / obj.b; return temp; } HinhHoc operator+(HinhHoc &obj) { HinhHoc temp; temp.a = a + obj.a; temp.b = b + obj.b; return temp; } HinhHoc operator-(HinhHoc &obj) { HinhHoc temp; temp.a = a - obj.a; temp.b = b - obj.b; return temp; } // Qua tai ==,!=,<,>,<=,>= bool operator==(HinhHoc &obj) { return (a == obj.a && b == obj.b); } bool operator!=(HinhHoc &obj) { return (a != obj.a || b != obj.b); } bool operator<(HinhHoc &obj) { return (a < obj.a && b < obj.b); } bool operator>(HinhHoc &obj) { return (a > obj.a && b > obj.b); } bool operator<=(HinhHoc &obj) { return (a <= obj.a && b <= obj.b); } bool operator>=(HinhHoc &obj) { return (a >= obj.a && b >= obj.b); } int getA() { return a; } int getB() { return b; } void setInfo(int a, int b) { this->a = a; this->b = b; } virtual void nhap() { cout << "Nhap a: "; cin >> a; cout << "Nhap b: "; cin >> b; } virtual void xuat() { cout << "a = " << a << endl; cout << "b = " << b << endl; } // Qua tai nhap xuat friend istream &operator>>(istream &is, HinhHoc &hinh) { cout << "Nhap a: "; is >> hinh.a; cout << "Nhap b: "; is >> hinh.b; return is; } friend ostream &operator<<(ostream &os, HinhHoc &hinh) { os << "a = " << hinh.a << endl; os << "b = " << hinh.b << endl; return os; } }; class HinhChuNhat : public HinhHoc { public: HinhChuNhat() : HinhHoc() { } HinhChuNhat(int a, int b) : HinhHoc(a, b) { } void nhap() { HinhHoc::nhap(); } void xuat() { HinhHoc::xuat(); cout << "s = " << getA() * getB() << endl; } }; int main() { HinhHoc *hinh[2] = {new HinhHoc(), new HinhChuNhat()}; for (int i = 0; i < 2; i++) { cout << "===== HINH " << i + 1 << " =====" << endl; hinh[i]->nhap(); } for (int i = 0; i < 2; i++) { cout << "===== HINH " << i + 1 << " =====" << endl; hinh[i]->xuat(); } // Xuat file txt fstream fout; fout.open("hinh.txt", ios::out); for (int i = 0; i < 2; i++) { fout << hinh[i]->getA() << " " << hinh[i]->getB() << endl; } fout.close(); // Doc file txt HinhHoc *hinh2[2]; fstream fin; fin.open("hinh.txt", ios::in); for (int i = 0; i < 2; i++) { int a, b; fin >> a >> b; hinh2[i] = new HinhChuNhat(a, b); } fin.close(); cout << "Doc file TXT" << endl; for (int i = 0; i < 2; i++) { cout << "===== HINH " << i + 1 << " =====" << endl; hinh2[i]->xuat(); } // xuat file dat fstream fout2; fout2.open("hinh.dat", ios::out | ios::binary); for (int i = 0; i < 2; i++) { fout2.write((char *)hinh[i], sizeof(HinhHoc)); } fout2.close(); // Doc file dat HinhHoc *hinh3[2]; fstream fin2; fin2.open("hinh.dat", ios::in | ios::binary); for (int i = 0; i < 2; i++) { fin2.read(reinterpret_cast<char *>(hinh3[i]), sizeof(HinhChuNhat)); } fin2.close(); cout << "Doc file DAT" << endl; for (int i = 0; i < 2; i++) { cout << "===== HINH " << i + 1 << " =====" << endl; hinh3[i]->xuat(); } // So sanh 2 hinh HinhHoc h3(1, 2), h4(1, 2), h5(3, 4); cout << "So sanh 2 hinh" << endl; cout << "h3 == h4: " << (h3 == h4) << endl; cout << "h3 != h4: " << (h3 != h4) << endl; cout << "h3 < h4: " << (h3 < h4) << endl; cout << "h3 > h4: " << (h3 > h4) << endl; cout << "h3 <= h4: " << (h3 <= h4) << endl; cout << "h3 >= h4: " << (h3 >= h4) << endl; cout << endl; cout << "h3 < h5: " << (h3 < h5) << endl; cout << "h3 > h5: " << (h3 > h5) << endl; cout << "h3 <= h5: " << (h3 <= h5) << endl; cout << "h3 >= h5: " << (h3 >= h5) << endl; cout << "h3 == h5: " << (h3 == h5) << endl; cout << "h3 != h5: " << (h3 != h5) << endl; return 0; }
Editor is loading...