Advertisement
MeehoweCK

Untitled

Jan 7th, 2021
630
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.33 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <ctime>
  4.  
  5. using namespace std;
  6.  
  7. class Rekord        // klasa abstrakcyjna (sama nie posiada swoich obiektów, bo posiada przynajmniej jedną metodę czysto wirtualną)
  8. {
  9. protected:
  10.     string nazwa;
  11. public:
  12.     Rekord(string tytul) : nazwa(tytul) {}
  13.     virtual void wypisz() = 0;          // metoda czysto wirtualna
  14. };
  15.  
  16. class film : public Rekord      // public Rekord informuje kompilator, że klasa ta dziedziczy od klasy Rekord (jest jej klasą pochodną)
  17. {
  18.     string rezyser, gatunek;
  19.     int rok_produkcji;
  20. public:
  21.     film(string director, string tytul, string rodzaj, int rok) : Rekord(tytul), rezyser(director), gatunek(rodzaj), rok_produkcji(rok) {}
  22.     void wypisz()
  23.     {
  24.         cout << "Film o tytule " << nazwa << " (" << gatunek << ") z roku " << rok_produkcji << ", wyrezyserowany przez " << rezyser << endl;
  25.     }
  26. };
  27.  
  28. class czasopismo : public Rekord
  29. {
  30.     int numer;
  31. public:
  32.     czasopismo(int number, string tytul) : Rekord(tytul), numer(number) {}
  33.     void wypisz()
  34.     {
  35.         cout << "Czasopismo o nazwie " << nazwa << ", nr " << numer << endl;
  36.     }
  37. };
  38.  
  39. class ksiazka : public Rekord
  40. {
  41.     string wydawnictwo, autor, miasto_wydania;
  42.     int ilosc_stron, rok_wydania;
  43. public:
  44.     ksiazka(string publisher, string writer, string city, string tytul, int pages, int year) : Rekord(tytul), wydawnictwo(publisher), autor(writer), miasto_wydania(city), ilosc_stron(pages), rok_wydania(year) {}
  45.     void wypisz()
  46.     {
  47.         cout << "Ksiazka o tytule " << nazwa << " autorstwa " << autor << ", " << miasto_wydania << ' ' << rok_wydania << " (" << wydawnictwo << ") liczba stron: " << ilosc_stron << endl;
  48.     }
  49. };
  50.  
  51. int main()
  52. {
  53.     Rekord* tablica[10];
  54.     srand(static_cast<unsigned>(time(nullptr)));
  55.     short losowanie, rok, dlugosc;
  56.     string tytul, autor, miasto, wydawca, rodzaj;
  57.  
  58.     for(int i = 0; i < 10; ++i)
  59.     {
  60.         losowanie = rand() % 3;
  61.         switch(losowanie)
  62.         {
  63.         case 0:
  64.             cout << "Podaj tytul filmu: ";
  65.             getline(cin, tytul);
  66.             cout << "Podaj nazwisko rezysera: ";
  67.             getline(cin, autor);
  68.             cout << "Podaj gatunek filmu: ";
  69.             cin >> rodzaj;
  70.             cout << "Podaj rok produkcji: ";
  71.             cin >> rok;
  72.             tablica[i] = new film(autor, tytul, rodzaj, rok);
  73.             break;
  74.         case 1:
  75.             cout << "Podaj nazwe czasopisma: ";
  76.             getline(cin, tytul);
  77.             cout << "Podaj nr egzemplarza: ";
  78.             cin >> rok;
  79.             tablica[i] = new czasopismo(rok, tytul);
  80.             break;
  81.         case 2:
  82.             cout << "Podaj tytul ksiazki: ";
  83.             getline(cin, tytul);
  84.             cout << "Podaj autora: ";
  85.             getline(cin, autor);
  86.             cout << "Podaj miasto wydania: ";
  87.             getline(cin, miasto);
  88.             cout << "Podaj nazwe wydawnictwa: ";
  89.             getline(cin, wydawca);
  90.             cout << "Podaj rok wydania: ";
  91.             cin >> rok;
  92.             cout << "Podaj liczbe stron: ";
  93.             cin >> dlugosc;
  94.             tablica[i] = new ksiazka(wydawca, autor, miasto, tytul, dlugosc, rok);
  95.         }
  96.     }
  97.  
  98.     for(int i = 0; i < 10; ++i)
  99.         tablica[i]->wypisz();
  100.  
  101.     for(int i = 0; i < 10; ++i)
  102.         delete tablica[i];
  103.  
  104.     return 0;
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement