Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cstdlib>
- #include <ctime>
- using namespace std;
- class Rekord // klasa abstrakcyjna (sama nie posiada swoich obiektów, bo posiada przynajmniej jedną metodę czysto wirtualną)
- {
- protected:
- string nazwa;
- public:
- Rekord(string tytul) : nazwa(tytul) {}
- virtual void wypisz() = 0; // metoda czysto wirtualna
- };
- class film : public Rekord // public Rekord informuje kompilator, że klasa ta dziedziczy od klasy Rekord (jest jej klasą pochodną)
- {
- string rezyser, gatunek;
- int rok_produkcji;
- public:
- film(string director, string tytul, string rodzaj, int rok) : Rekord(tytul), rezyser(director), gatunek(rodzaj), rok_produkcji(rok) {}
- void wypisz()
- {
- cout << "Film o tytule " << nazwa << " (" << gatunek << ") z roku " << rok_produkcji << ", wyrezyserowany przez " << rezyser << endl;
- }
- };
- class czasopismo : public Rekord
- {
- int numer;
- public:
- czasopismo(int number, string tytul) : Rekord(tytul), numer(number) {}
- void wypisz()
- {
- cout << "Czasopismo o nazwie " << nazwa << ", nr " << numer << endl;
- }
- };
- class ksiazka : public Rekord
- {
- string wydawnictwo, autor, miasto_wydania;
- int ilosc_stron, rok_wydania;
- public:
- 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) {}
- void wypisz()
- {
- cout << "Ksiazka o tytule " << nazwa << " autorstwa " << autor << ", " << miasto_wydania << ' ' << rok_wydania << " (" << wydawnictwo << ") liczba stron: " << ilosc_stron << endl;
- }
- };
- int main()
- {
- Rekord* tablica[10];
- srand(static_cast<unsigned>(time(nullptr)));
- short losowanie, rok, dlugosc;
- string tytul, autor, miasto, wydawca, rodzaj;
- for(int i = 0; i < 10; ++i)
- {
- losowanie = rand() % 3;
- switch(losowanie)
- {
- case 0:
- cout << "Podaj tytul filmu: ";
- getline(cin, tytul);
- cout << "Podaj nazwisko rezysera: ";
- getline(cin, autor);
- cout << "Podaj gatunek filmu: ";
- cin >> rodzaj;
- cout << "Podaj rok produkcji: ";
- cin >> rok;
- tablica[i] = new film(autor, tytul, rodzaj, rok);
- break;
- case 1:
- cout << "Podaj nazwe czasopisma: ";
- getline(cin, tytul);
- cout << "Podaj nr egzemplarza: ";
- cin >> rok;
- tablica[i] = new czasopismo(rok, tytul);
- break;
- case 2:
- cout << "Podaj tytul ksiazki: ";
- getline(cin, tytul);
- cout << "Podaj autora: ";
- getline(cin, autor);
- cout << "Podaj miasto wydania: ";
- getline(cin, miasto);
- cout << "Podaj nazwe wydawnictwa: ";
- getline(cin, wydawca);
- cout << "Podaj rok wydania: ";
- cin >> rok;
- cout << "Podaj liczbe stron: ";
- cin >> dlugosc;
- tablica[i] = new ksiazka(wydawca, autor, miasto, tytul, dlugosc, rok);
- }
- }
- for(int i = 0; i < 10; ++i)
- tablica[i]->wypisz();
- for(int i = 0; i < 10; ++i)
- delete tablica[i];
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement