axeefectushka

Untitled

Jan 23rd, 2019
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.27 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <iostream>
  3. #include <windows.h>
  4.  
  5. using namespace std;
  6.  
  7. class first {
  8. protected:
  9.     int *arr;
  10.     int sizeArr;
  11. public:
  12.     first(int size) : sizeArr(size)
  13.     {
  14.         arr = new int[size];
  15.         cout << "Input elements: ";
  16.         for (int i = 0; i < sizeArr; i++)
  17.         {
  18.             cin >> arr[i];
  19.         }
  20.     }
  21.     first()
  22.     {
  23.         arr = NULL;
  24.         sizeArr = 0;
  25.     }
  26.     virtual int task()
  27.     {
  28.         int sum = 0;
  29.         for (int i = 0; i < sizeArr; i++) {
  30.             if (i >= sizeArr)
  31.             {
  32.                 RaiseException(EXCEPTION_ARRAY_BOUNDS_EXCEEDED, 0, 0, NULL);
  33.             }
  34.             sum += arr[i];
  35.         }
  36.         return sum;
  37.     }
  38.     ~first() {
  39.         delete[]arr;
  40.     }
  41. };
  42.  
  43. class second : public first
  44. {
  45. public:
  46.     second(int sizeArr) : first(sizeArr)
  47.     {}
  48.     int task()
  49.     {
  50.         int mult = 1;
  51.         __try
  52.         {  
  53.             for (int i = 0; i < sizeArr; i++)
  54.                 {
  55.                     if (mult > INT_MAX)
  56.                         RaiseException(EXCEPTION_PRIV_INSTRUCTION, 0, 0, NULL);
  57.                     mult *= arr[i];
  58.                 }
  59.            
  60.         }
  61.         __except (EXCEPTION_EXECUTE_HANDLER)
  62.             {
  63.                 if (GetExceptionCode() == EXCEPTION_ARRAY_BOUNDS_EXCEEDED)
  64.                     {
  65.                         cout << "ERROR!" << endl;
  66.                     }
  67.             }
  68.         return mult;
  69.     }
  70. };
  71.  
  72. int main()
  73. {
  74.     first mas1(5);
  75.     second mas2(5);
  76.     cout << "MULT OF 1 ARRAY:" << mas1.task() << endl;
  77.     cout << "MULT OF 2 ARRAY:" << mas2.task() << endl;
  78.     return 0;
  79. }
Add Comment
Please, Sign In to add comment