Advertisement
Guest User

LABORATORIUM 8 OBIEKTÓWKA

a guest
Dec 6th, 2019
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.43 KB | None | 0 0
  1. #include <iostream>
  2. #include <sstream>
  3. #include <typeinfo>
  4. #include <assert.h>
  5. #include <time.h>
  6.  
  7. using namespace std;
  8.  
  9. class Pies;
  10. class Czlowiek;
  11.  
  12. enum Plec {
  13.     PIES,
  14.     SUKA
  15. };
  16.  
  17. enum Plec_czlowieka {
  18.     MEZCZYZNA,
  19.     KOBIETA
  20. };
  21.  
  22. class Miska {
  23. private:
  24.     int porcja;
  25.  
  26. public:
  27.     Miska& operator()(int porcja) {
  28.         this->porcja = porcja;
  29.         return *this;
  30.     }
  31.  
  32.     int dajPorcja() {
  33.         return porcja;
  34.     }
  35. };
  36.  
  37. class FunkcjeZyciowe {
  38.  
  39. public:
  40.     virtual void jedz(int iloscPozywienia) = 0;
  41.     virtual void pij(int iloscWody) = 0;
  42.     virtual void spij(int czas) = 0;
  43.     virtual void bawSie(int czas) = 0;
  44.  
  45. };
  46.  
  47. class Zwierze {
  48. private:
  49.     string mImie;
  50.  
  51. protected:
  52.     int glod = 0;
  53.     int pragnienie = 0;
  54.     int zmeczenie = 0;
  55.     int humor = 0;
  56.  
  57. public:
  58.     Zwierze() {
  59.     }
  60.     Zwierze(string imie) {
  61.         mImie = imie;
  62.     }
  63.  
  64.     friend ostream& operator << (ostream & ss, Zwierze& z);
  65.  
  66.     string dajImie() const {
  67.         return mImie;
  68.     }
  69.  
  70.     void ustawImie(string imie) {
  71.         mImie = imie;
  72.     }
  73.  
  74.     const string naLancuch() const {
  75.         stringstream ss;
  76.  
  77.         ss << dajRodzajZwierzecia() << " " << mImie
  78.            << ", glod: " << glod
  79.            << ", pragnienie: " << pragnienie
  80.            << ", zmeczenie: " << zmeczenie;
  81.         return ss.str();
  82.     }
  83.  
  84.     virtual string dajRodzajZwierzecia() const {
  85.         return "Zwierzatko: ";
  86.     }
  87. };
  88.  
  89.  
  90. class Pies: public Zwierze, public FunkcjeZyciowe {
  91. private:
  92.     Plec plec;
  93.  
  94. public:
  95.  
  96.     Pies(string imie = "") : Zwierze(imie) {
  97.  
  98.     }
  99.  
  100.     Pies(string imie, Plec plec_psa) : Zwierze(imie) {
  101.         plec = plec_psa;
  102.     }
  103.  
  104.     Plec dajPlec() {
  105.         return plec;
  106.     }
  107.  
  108.     void ustawPlec(Plec new_plec) {
  109.         plec = new_plec;
  110.     }
  111.  
  112.     void szczekaj() {
  113.         cout << naLancuch() << ": Hau hau mister frau" << endl;
  114.     }
  115.  
  116.     void dajLape() {
  117.         cout << naLancuch() << " daje lape" << endl;
  118.     }
  119.  
  120.     string dajRodzajZwierzecia() const {
  121.         return "Pies: ";
  122.     }
  123.  
  124.  
  125.     const string naLancuch() const {
  126.         stringstream ss;
  127.  
  128.         ss << dajRodzajZwierzecia() << dajImie()
  129.            << ", glod: " << glod
  130.            << ", pragnienie: " << pragnienie
  131.            << ", zmeczenie: " << zmeczenie
  132.            << ", plec: " << (plec == 0 ? "PIES" : "SUKA");
  133.         return ss.str();
  134.     }
  135.  
  136.     void jedz(int iloscPozywienia) {
  137.         glod -= 10 * iloscPozywienia;
  138.         zmeczenie -= 5 * iloscPozywienia;
  139.     }
  140.  
  141.     void pij(int iloscWody) {
  142.         pragnienie -= 15 * iloscWody;
  143.         zmeczenie -= 8 * iloscWody;
  144.     }
  145.  
  146.     void spij(int czas) {
  147.         zmeczenie -= 14 * czas;
  148.     }
  149.  
  150.     void bawSie(int czas) {
  151.         pragnienie += 5 * czas;
  152.         zmeczenie += 7 * czas;
  153.         glod += 12 * czas;
  154.         humor += 40 * czas;
  155.     }
  156.  
  157.     string generujImie(string imie1, string imie2) {
  158.         string new_imie;
  159.  
  160.         int chars_from_imie = rand() % imie1.length();
  161.         new_imie = imie1.substr(0, chars_from_imie);
  162.         chars_from_imie = rand() % imie2.length();
  163.         new_imie += imie2.substr(0, chars_from_imie);
  164.  
  165.         new_imie[0] = toupper(new_imie[0]);
  166.         int new_imie_len = new_imie.length();
  167.         for(int i = 1; i < new_imie_len; i++) {
  168.             new_imie[i] = tolower(new_imie[i]);
  169.         }
  170.  
  171.         return new_imie;
  172.     }
  173.  
  174.     Plec generujLosowaPlec() {
  175.         Plec plec = (Plec)(rand() % 2);
  176.         return plec;
  177.     }
  178.  
  179.     Pies operator +(Pies& p2) {
  180.         assert(dajPlec() != p2.dajPlec());
  181.         Pies new_p;
  182.  
  183.         string noweImie = generujImie(dajImie(), p2.dajImie());
  184.         new_p.ustawImie(noweImie);
  185.  
  186.         Plec new_plec = generujLosowaPlec();
  187.         new_p.ustawPlec(new_plec);
  188.  
  189.         return new_p;
  190.     }
  191.  
  192.     void operator <<(Miska& miska) {
  193.         glod -= miska.dajPorcja();
  194.         miska(0);
  195.     }
  196. };
  197.  
  198. ostream& operator <<(ostream & ss, Zwierze& z) {
  199.     ss << z.dajRodzajZwierzecia() << " " << z.mImie
  200.         << ", glod: " << z.glod
  201.         << ", pragnienie: " << z.pragnienie
  202.         << ", zmeczenie: " << z.zmeczenie;
  203.     return ss;
  204. }
  205.  
  206. class Kot: public Zwierze, public FunkcjeZyciowe {
  207. public:
  208.  
  209.     Kot(string imie) : Zwierze(imie) {
  210.     }
  211.  
  212.     void miaucz() {
  213.         cout << dajImie() << " " << " miaaaaaauuuuu" << endl;
  214.     }
  215.  
  216.     void myjSie() {
  217.         cout << dajImie() << " " << " robi myju myju" << endl;
  218.     }
  219.  
  220.     string dajRodzajZwierzecia() const {
  221.         return "Kot: ";
  222.     }
  223.  
  224.     void jedz(int iloscPozywienia) {
  225.         glod -= 7 * iloscPozywienia;
  226.     }
  227.  
  228.     void pij(int iloscWody) {
  229.         pragnienie -= 8 * iloscWody;
  230.     }
  231.  
  232.     void spij(int czas) {
  233.         zmeczenie -= 6 * czas;
  234.     }
  235.  
  236.     void bawSie(int czas) {
  237.         pragnienie += 17 * czas;
  238.         zmeczenie += 6 * czas;
  239.         glod += 11 * czas;
  240.         humor += 33 * czas;
  241.     }
  242. };
  243.  
  244. class SchroniskoDlaZwierzat {
  245. private:
  246.     Zwierze* zwierzeta[100];
  247.  
  248. public:
  249.     SchroniskoDlaZwierzat() {
  250.  
  251.     }
  252.  
  253.     void dodajZwierze(Zwierze* z, int numer) {
  254.         zwierzeta[numer] = z;
  255.     }
  256.  
  257.     Zwierze* dajZwierze(int numer) {
  258.         return zwierzeta[numer];
  259.     }
  260. };
  261.  
  262.  
  263. class Czlowiek
  264. {
  265. private:
  266.     string imie;
  267.     Plec_czlowieka plec;
  268.     int wiek;
  269. public:
  270.     Czlowiek(string imie2, int wiek2, Plec_czlowieka plec2)
  271.     {
  272.         imie = imie2;
  273.         wiek = wiek2;
  274.         plec = plec2;
  275.     }
  276.  
  277.     const string wyswietlDane() const
  278.     {
  279.         stringstream napis;
  280.  
  281.         napis << endl << "IMIE: " << imie << endl << "PLEC: " << (plec == 0 ? "MEZCZYZNA" : "KOBIETA")
  282.         << endl << "WIEK: " << wiek << endl;
  283.  
  284.         return napis.str();
  285.     }
  286. };
  287.  
  288.  
  289. int main()
  290. {
  291.     /*
  292.     srand (time(NULL));
  293.     Pies p1("Stefan", PIES);
  294.     Pies p2("Zosiaa", SUKA);
  295.  
  296.     cout << p1.naLancuch() << endl;
  297.     cout << p2.naLancuch() << endl;
  298.  
  299.     Pies p3 = p1 + p2;
  300.     cout << p3.naLancuch() << endl;
  301.  
  302.     Pies p4 = p2 + p3;
  303.     cout << p4.naLancuch() << endl;
  304.     */
  305.     Pies p1("Stefan", PIES);
  306.     Miska miska;
  307.  
  308.     cout << p1.naLancuch() << endl;
  309.     //miska(100);
  310.     p1 << miska(200);
  311.     cout << p1.naLancuch() << endl;
  312.  
  313.     //miska(250);
  314.     //p1 << miska;
  315.     //cout << p1.naLancuch() << endl;
  316.  
  317.     cout << "Porcja w misce: " << miska.dajPorcja() << endl;
  318.     cout << p1;
  319.  
  320.     Czlowiek czlowiek1("Janusz",33,MEZCZYZNA);
  321.     cout << czlowiek1.wyswietlDane();
  322.     return 0;
  323. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement