Advertisement
huutho_96

Untitled

Jun 16th, 2016
262
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.14 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. class A
  5. {
  6. protected:
  7. int a;
  8. public:
  9. virtual void Nhap()
  10. {
  11. cout << "a = ";
  12. cin >> a;
  13. }
  14. virtual void Xuat()
  15. {
  16. cout << "Gia tri a = " << a << endl;
  17. }
  18. };
  19.  
  20. class B : public A
  21. {
  22. private:
  23. int b;
  24. public:
  25. void Nhap()
  26. {
  27. A::Nhap();
  28. cout << "b = ";
  29. cin >> b;
  30. }
  31. void Xuat()
  32. {
  33. A::Xuat();
  34. cout << "Gia tri b = " << b << endl;
  35. }
  36. };
  37.  
  38. class C : public A
  39. {
  40. private:
  41. int c;
  42. public:
  43. void Nhap()
  44. {
  45. A::Nhap();
  46. cout << "c = ";
  47. cin >> c;
  48. }
  49. void Xuat()
  50. {
  51. A::Xuat();
  52. cout << "Gia tri c = " << c << endl;
  53. }
  54. };
  55.  
  56.  
  57. class D
  58. {
  59. private:
  60. A* arr[1024];
  61. int n;
  62.  
  63. int menu()
  64. {
  65. int c;
  66. do
  67. {
  68. cout << "\n1. B\n2. C\n";
  69. cin >> c;
  70. } while (c != 1 && c != 2);
  71. return c;
  72. }
  73. public:
  74. void Nhap()
  75. {
  76. cout << "Nhap n = ";
  77. cin >> n;
  78. for (int i = 0; i < n; i++)
  79. {
  80. int c = menu();
  81. if (c == 1)
  82. arr[i] = new B();
  83. else if (c == 2)
  84. arr[i] = new C();
  85. arr[i]->Nhap();
  86. }
  87. }
  88. void Xuat()
  89. {
  90. for (int i = 0; i < n; i++)
  91. arr[i]->Xuat();
  92. }
  93.  
  94. };
  95.  
  96. void main()
  97. {
  98. D d;
  99. d.Nhap();
  100. d.Xuat();
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement