Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Написать программу, в которой описана иерархия классов: ошибка в программе
- //(«ошибка доступа к памяти», «математическая», «деление на ноль», «переполнение»).
- //Наследники должны иметь поля, содержащие дополнительные сведения об ошибке, если такие
- //имеются. Продемонстрировать работу всех методов классов , предоставив пользователю выбор
- //типа объекта для демонстрации.
- #include "stdafx.h"
- #include <stdio.h>
- #include <climits>
- class EBaseError{
- public:
- virtual void Print() =0;
- virtual void Read() =0;
- };
- class EAccessViolation: public EBaseError{//ошибка доступа
- void* m_badAddr;
- public:
- void Print();
- void Read();
- EAccessViolation(void* badAddr);
- };
- class EMathError: public EBaseError{};//мат ошибка
- class EZeroDivide: public EMathError{//деление на ноль
- double m_divident;
- public:
- void Print();
- void Read();
- EZeroDivide(const double ÷nt);
- };
- class EOverflow: public EMathError{//переполнение
- int m_operand1, m_operand2;
- public:
- void Print();
- void Read();
- EOverflow(const int &operand1, const int &operand2);
- };
- EAccessViolation::EAccessViolation(void* badAddr){
- m_badAddr = badAddr;
- }
- void EAccessViolation::Print(){
- printf("Access violation read of address %p!", m_badAddr);
- }
- void EAccessViolation::Read(){
- }
- EZeroDivide::EZeroDivide(const double ÷nt){
- m_divident = divident;
- }
- void EZeroDivide::Print(){
- printf("There was a try to divide %lf by zero!", m_divident);
- }
- void EZeroDivide::Read(){
- }
- EOverflow::EOverflow(const int &operand1, const int &operand2){
- m_operand1 = operand1;
- m_operand2 = operand2;
- }
- void EOverflow::Print(){
- printf("There was an overflow during some operation between %d and %d!", m_operand1, m_operand2);
- }
- void EOverflow::Read(){
- }
- void disposeError(EBaseError** e){
- if(*e!=NULL) delete *e;
- *e = NULL;
- }
- int main(){
- EBaseError* e = NULL;
- char c = 0, tmp;
- while(c!=27){
- printf("\n deistvie:\n 1 - oshibka dostupa\n");
- printf(" 2 - podelit' 2 4isla\n 3 - umno 2 4isla\n");
- printf(" 0 - exit\n");
- scanf("%c", &c);
- switch(c) {
- case '1': e = new EAccessViolation((void*) &c); break;
- case '2':
- double a, b;
- printf("\nvvedite delimoe");
- scanf("%lf", &a);
- printf("\ndelitel'");
- scanf("%lf", &b);
- if (b==0.0) e = new EZeroDivide(a);
- else printf("\n otvet = %lf", a/b);
- break;
- case '3':
- int i, j;
- long long res;
- printf("\n 1mn");
- scanf("%d", &i);
- printf("\n 2mn");
- scanf("%d", &j);
- res = i;
- res = i*j;
- if (res>INT_MAX || res<INT_MIN) e = new
- EOverflow(i, j);
- else printf("\n otvet = %d", res);
- break;
- case '0' :c = 27;
- }
- scanf("%c", &tmp);//считываем Enter оставшийся в буфере ввода после предыдущего scanf
- if(e != NULL) {
- printf("\n");
- e->Print();
- printf("\n");
- }
- disposeError(&e);
- }
- return 0;
- };
Add Comment
Please, Sign In to add comment