Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // main.cpp
- #include <iostream>
- #include <conio.h>
- #include <vector>
- #include "Rekord.h"
- using namespace std;
- int main()
- {
- vector<Rekord*> tablica;
- unsigned n;
- char znak;
- cout << "Podaj liczbe rekordow: ";
- cin >> n;
- string a, b;
- int c, d;
- for(unsigned i = 0; i < n; ++i)
- {
- cout << "Wpisz K jesli chcesz dodac ksiazke i M jesli chcesz dodac album muzyczny: ";
- do
- {
- znak = getch();
- } while(znak != 'k' && znak != 'K' && znak != 'M' && znak != 'm');
- if(znak == 'k' || znak == 'K')
- {
- cout << "Podaj kolejno autora, tytul ksiazki, rok wydania i liczbe stron: ";
- cin >> a >> b >> c >> d;
- Rekord* nowy = new Ksiazka(c, a, b, d);
- tablica.push_back(nowy);
- }
- else
- {
- cout << "Podaj kolejno autora, tytul albumu, rok wydania i dlugosc: ";
- cin >> a >> b >> c >> d;
- Rekord* nowy = new AlbumMuzyczny(c, a, b, d);
- tablica.push_back(nowy);
- }
- }
- for(unsigned i = 0; i < n; ++i)
- tablica[i]->wypisz_dane();
- for(unsigned i = 0; i < n; ++i)
- delete tablica[i];
- return 0;
- }
- // Rekord.h
- #ifndef REKORD_H
- #define REKORD_H
- #include <iostream>
- using namespace std;
- enum class Typ
- {
- ksiazka,
- album
- };
- class Rekord
- {
- public:
- Rekord(int, string, string, Typ);
- virtual void wypisz_dane() = 0; // metoda czysto wirtualna
- Typ get_typ() const {return typ;}
- protected:
- Typ typ;
- int rok_wydania;
- string autor;
- string tytul;
- };
- class Ksiazka : public Rekord
- {
- public:
- Ksiazka(int, string, string, int);
- void wypisz_dane();
- private:
- int liczba_stron;
- };
- class AlbumMuzyczny : public Rekord
- {
- public:
- AlbumMuzyczny(int, string, string, int);
- void wypisz_dane();
- private:
- int dlugosc_utworu;
- };
- #endif
- // Rekord.cpp
- #include "Rekord.h"
- /*class Rekord
- {
- public:
- Rekord(int, string, string);
- virtual void wypisz_dane() = 0; // metoda czysto wirtualna
- protected:
- int rok_wydania;
- string autor;
- string tytul;
- };*/
- Rekord::Rekord(int rok, string aut, string title, Typ rodzaj) : rok_wydania(rok), autor(aut), tytul(title), typ(rodzaj) {}
- /*class Ksiazka : public Rekord
- {
- public:
- Ksiazka(int, string, string, int);
- void wypisz_dane();
- private:
- int liczba_stron;
- };*/
- Ksiazka::Ksiazka(int rok, string aut, string title, int strony) : Rekord(rok, aut, title, Typ::ksiazka), liczba_stron(strony) {}
- void Ksiazka::wypisz_dane()
- {
- cout << "Ksiazka pt. \"" << tytul << '\"' << endl;
- cout << "Autor: " << autor << endl;
- cout << "Rok wydania: " << rok_wydania << endl;
- cout << "Liczba stron: " << liczba_stron << endl;
- }
- AlbumMuzyczny::AlbumMuzyczny(int rok, string aut, string title, int dlugosc_utw) : Rekord(rok, aut, title, Typ::album), dlugosc_utworu(dlugosc_utw) {}
- void AlbumMuzyczny::wypisz_dane()
- {
- cout << "Album pt. \"" << tytul << '\"' << endl;
- cout << "Autor: " << autor << endl;
- cout << "Rok wydania: " << rok_wydania << endl;
- cout << "Dlugosc utworu: " << dlugosc_utworu << endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement