Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- class A
- {
- protected:
- int a;
- public:
- virtual void Nhap()
- {
- cout << "a = ";
- cin >> a;
- }
- virtual void Xuat()
- {
- cout << "Gia tri a = " << a << endl;
- }
- };
- class B : public A
- {
- private:
- int b;
- public:
- void Nhap()
- {
- A::Nhap();
- cout << "b = ";
- cin >> b;
- }
- void Xuat()
- {
- A::Xuat();
- cout << "Gia tri b = " << b << endl;
- }
- };
- class C : public A
- {
- private:
- int c;
- public:
- void Nhap()
- {
- A::Nhap();
- cout << "c = ";
- cin >> c;
- }
- void Xuat()
- {
- A::Xuat();
- cout << "Gia tri c = " << c << endl;
- }
- };
- class D
- {
- private:
- A* arr[1024];
- int n;
- int menu()
- {
- int c;
- do
- {
- cout << "\n1. B\n2. C\n";
- cin >> c;
- } while (c != 1 && c != 2);
- return c;
- }
- public:
- void Nhap()
- {
- cout << "Nhap n = ";
- cin >> n;
- for (int i = 0; i < n; i++)
- {
- int c = menu();
- if (c == 1)
- arr[i] = new B();
- else if (c == 2)
- arr[i] = new C();
- arr[i]->Nhap();
- }
- }
- void Xuat()
- {
- for (int i = 0; i < n; i++)
- arr[i]->Xuat();
- }
- };
- void main()
- {
- D d;
- d.Nhap();
- d.Xuat();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement