Advertisement
MeehoweCK

Untitled

Oct 30th, 2020
2,054
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.28 KB | None | 0 0
  1. // main.cpp
  2. #include <iostream>
  3. #include <conio.h>
  4. #include <vector>
  5. #include "Rekord.h"
  6.  
  7. using namespace std;
  8.  
  9. int main()
  10. {
  11.     vector<Rekord*> tablica;
  12.  
  13.     unsigned n;
  14.     char znak;
  15.     cout << "Podaj liczbe rekordow: ";
  16.     cin >> n;
  17.  
  18.     string a, b;
  19.     int c, d;
  20.  
  21.     for(unsigned i = 0; i < n; ++i)
  22.     {
  23.         cout << "Wpisz K jesli chcesz dodac ksiazke i M jesli chcesz dodac album muzyczny: ";
  24.         do
  25.         {
  26.             znak = getch();
  27.         } while(znak != 'k' && znak != 'K' && znak != 'M' && znak != 'm');
  28.  
  29.         if(znak == 'k' || znak == 'K')
  30.         {
  31.             cout << "Podaj kolejno autora, tytul ksiazki, rok wydania i liczbe stron: ";
  32.             cin >> a >> b >> c >> d;
  33.             Rekord* nowy = new Ksiazka(c, a, b, d);
  34.             tablica.push_back(nowy);
  35.         }
  36.  
  37.         else
  38.         {
  39.             cout << "Podaj kolejno autora, tytul albumu, rok wydania i dlugosc: ";
  40.             cin >> a >> b >> c >> d;
  41.             Rekord* nowy = new AlbumMuzyczny(c, a, b, d);
  42.             tablica.push_back(nowy);
  43.         }
  44.     }
  45.  
  46.     for(unsigned i = 0; i < n; ++i)
  47.         tablica[i]->wypisz_dane();
  48.  
  49.     for(unsigned i = 0; i < n; ++i)
  50.         delete tablica[i];
  51.  
  52.     return 0;
  53. }
  54.  
  55. // Rekord.h
  56. #ifndef REKORD_H
  57. #define REKORD_H
  58. #include <iostream>
  59.  
  60. using namespace std;
  61.  
  62. enum class Typ
  63. {
  64.     ksiazka,
  65.     album
  66. };
  67.  
  68. class Rekord
  69. {
  70.     public:
  71.         Rekord(int, string, string, Typ);
  72.         virtual void wypisz_dane() = 0;     // metoda czysto wirtualna
  73.         Typ get_typ() const {return typ;}
  74.     protected:
  75.         Typ typ;
  76.         int rok_wydania;
  77.         string autor;
  78.         string tytul;
  79. };
  80.  
  81. class Ksiazka : public Rekord
  82. {
  83. public:
  84.     Ksiazka(int, string, string, int);
  85.     void wypisz_dane();
  86. private:
  87.     int liczba_stron;
  88. };
  89.  
  90. class AlbumMuzyczny : public Rekord
  91. {
  92. public:
  93.     AlbumMuzyczny(int, string, string, int);
  94.     void wypisz_dane();
  95. private:
  96.     int dlugosc_utworu;
  97. };
  98.  
  99. #endif
  100.  
  101. // Rekord.cpp
  102. #include "Rekord.h"
  103.  
  104. /*class Rekord
  105. {
  106.     public:
  107.         Rekord(int, string, string);
  108.         virtual void wypisz_dane() = 0;     // metoda czysto wirtualna
  109.     protected:
  110.         int rok_wydania;
  111.         string autor;
  112.         string tytul;
  113. };*/
  114.  
  115. Rekord::Rekord(int rok, string aut, string title, Typ rodzaj) : rok_wydania(rok), autor(aut), tytul(title), typ(rodzaj) {}
  116.  
  117. /*class Ksiazka : public Rekord
  118. {
  119. public:
  120.     Ksiazka(int, string, string, int);
  121.     void wypisz_dane();
  122. private:
  123.     int liczba_stron;
  124. };*/
  125.  
  126. Ksiazka::Ksiazka(int rok, string aut, string title, int strony) : Rekord(rok, aut, title, Typ::ksiazka), liczba_stron(strony) {}
  127.  
  128. void Ksiazka::wypisz_dane()
  129. {
  130.     cout << "Ksiazka pt. \"" << tytul << '\"' << endl;
  131.     cout << "Autor: " << autor << endl;
  132.     cout << "Rok wydania: " << rok_wydania << endl;
  133.     cout << "Liczba stron: " << liczba_stron << endl;
  134. }
  135.  
  136. AlbumMuzyczny::AlbumMuzyczny(int rok, string aut, string title, int dlugosc_utw) : Rekord(rok, aut, title, Typ::album), dlugosc_utworu(dlugosc_utw) {}
  137.  
  138. void AlbumMuzyczny::wypisz_dane()
  139. {
  140.     cout << "Album pt. \"" << tytul << '\"' << endl;
  141.     cout << "Autor: " << autor << endl;
  142.     cout << "Rok wydania: " << rok_wydania << endl;
  143.     cout << "Dlugosc utworu: " << dlugosc_utworu << endl;
  144. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement