Advertisement
Guest User

polimorfizm-osbluga-sytuacji

a guest
Jun 2nd, 2015
257
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.67 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4. class Urzadzenie
  5. {
  6.     string Nazwa;
  7.     protected:
  8.         Urzadzenie(string _Nazwa){ Nazwa = _Nazwa; }
  9.  
  10.     public:
  11.         virtual int Moc()=0;
  12.         virtual float Cena()=0;
  13.         void Informacja()
  14.         {
  15.             if (Nazwa.length() > 8)
  16.                 throw 1;
  17.  
  18.             cout << Nazwa << endl;
  19.             cout << "moc:" << Moc() << endl;
  20.             cout << "cena: " << Cena() << endl;
  21.             //Moc();
  22.             //Cena();
  23.         }
  24. };
  25.  
  26. class Gofry
  27. {
  28. public:
  29.     Gofry(int x)
  30.     {
  31.         if (x < 0)
  32.             throw 1;
  33.     }
  34.  
  35. };
  36.  
  37. class Zasilacz :public Urzadzenie
  38. {
  39.     int MocZasilacza;
  40.     int IloscWentylatorow;
  41.     Gofry gofry;
  42.  
  43.     public:
  44.         Zasilacz(int _MocZasilacza, int _IloscWentylatorow, string _Nazwa)
  45.             try : gofry(-1), IloscWentylatorow(_IloscWentylatorow), Urzadzenie(_Nazwa)
  46.         {
  47.             if (_MocZasilacza >= 300)
  48.             {
  49.                 //throw "Nie udalo sie zainicjalizowac obiektu";
  50.                 throw 1;
  51.             }
  52.             else
  53.             {
  54.                 MocZasilacza = _MocZasilacza;
  55.             }
  56.         }
  57.         catch (...)
  58.         {
  59.             printf("jak to ujemne gofry\n");
  60.         }
  61.  
  62.         virtual int Moc()
  63.         {
  64.             return MocZasilacza;
  65.         }
  66.  
  67.         virtual float Cena()
  68.         {
  69.             int wartosc1 = (MocZasilacza / 50) * 45;
  70.             int wartosc2_i = IloscWentylatorow * 25;
  71.             return wartosc1 + wartosc2_i;
  72.         }
  73. };
  74.  
  75. class PlytaGlowna :public Urzadzenie
  76. {
  77.     int LiczbaProcesorow;
  78.     int IloscSATA;
  79.     int IloscPCI;
  80.  
  81.     public:
  82.         PlytaGlowna(int _LiczbaProcesowrow, int _LiczbaSATA, int _LiczbaPCI, string _Nazwa)
  83.             :LiczbaProcesorow(_LiczbaProcesowrow), IloscSATA(_LiczbaSATA), IloscPCI(_LiczbaPCI), Urzadzenie(_Nazwa){}
  84.  
  85.         virtual int Moc()
  86.         {
  87.             return ((LiczbaProcesorow * 100) + (IloscPCI * 20) + (IloscSATA * 10));
  88.         }
  89.         virtual float Cena()
  90.         {
  91.             return (300 + (LiczbaProcesorow * 400) + (IloscPCI * 50) + (IloscSATA * 20));
  92.         }
  93. };
  94.  
  95. class DyskTwardy :public Urzadzenie
  96. {
  97.     int Pojemnosc;
  98.     public:
  99.         DyskTwardy(int _Pojemnosc, string _Nazwa) : Pojemnosc(_Pojemnosc), Urzadzenie(_Nazwa){}
  100.  
  101.  
  102.         virtual int Moc()
  103.         {
  104.             return 70;
  105.         }
  106.  
  107.         virtual float Cena()
  108.         {
  109.             return (Pojemnosc < 200) ? 200 : Pojemnosc;;
  110.         }
  111. };
  112. int main()
  113. {
  114.     Urzadzenie **urzadzenia = NULL;
  115.  
  116.     char buffor[64];
  117.     unsigned int size = 1;
  118.  
  119.     try
  120.     {
  121.         FILE *Plik = fopen("Text.txt", "r");
  122.  
  123.         if (Plik == NULL)
  124.             throw "Nie udalo sie otworzyc pliku!";
  125.  
  126.         while (!feof(Plik))
  127.         {
  128.             fscanf(Plik, "%s", &buffor);
  129.             urzadzenia = (Urzadzenie**)realloc(urzadzenia, sizeof(Urzadzenie*)*size);
  130.  
  131.             try
  132.             {
  133.                 if (strcmp(buffor, "DyskTwardy") == 0)
  134.                 {
  135.                     int wartos1;
  136.                     fscanf(Plik, "%s %d", &buffor, &wartos1);
  137.                     urzadzenia[size - 1] = new DyskTwardy(wartos1, buffor);
  138.                 }
  139.                 else if (strcmp(buffor, "Zasilacz") == 0)
  140.                 {  
  141.                     int wartos1, wartos2;
  142.                     fscanf(Plik, "%s %d %d", &buffor, &wartos1, &wartos2);
  143.                     urzadzenia[size - 1] = new Zasilacz(wartos1, wartos2, buffor);
  144.                 }
  145.                 else if (strcmp(buffor, "PlytaGlowna") == 0)
  146.                 {
  147.                     int wartos1, wartos2, wartos3;
  148.                     fscanf(Plik, "%s %d %d %d", &buffor, &wartos1, &wartos2, &wartos3);
  149.                     urzadzenia[size - 1] = new PlytaGlowna(wartos1, wartos2, wartos3, buffor);
  150.                 }
  151.                 size++;
  152.             }
  153.             catch (bad_alloc)
  154.             {
  155.                 urzadzenia[size - 1] = NULL;
  156.             }
  157.             catch (int komunikat)
  158.             {
  159.                 printf("Blad typu - %i\n", komunikat);
  160.             }
  161.         }
  162.         fclose(Plik);
  163.     }
  164.     catch (char *komunikat)
  165.     {
  166.         printf("%s\n", komunikat);
  167.     }
  168.  
  169.     if (urzadzenia != NULL)
  170.     {
  171.         for (unsigned int i = 0; i < size - 1; i++)
  172.         {
  173.             try
  174.             {
  175.                 urzadzenia[i]->Informacja();
  176.             }
  177.             catch (...)
  178.             {
  179.                 printf("Cos tam jest. \n");
  180.             }
  181.         }
  182.  
  183.         //Zwolnij pamiec
  184.         for (unsigned int i = 0; i < size - 1; i++)
  185.         {
  186.             if(urzadzenia[i] != NULL) delete urzadzenia[i];
  187.         }
  188.         free(urzadzenia);
  189.     }
  190.     system("pause");
  191.     return 0;
  192. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement