Advertisement
Smudla

Cpp CV4

Oct 29th, 2015
27
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.17 KB | None | 0 0
  1. #include <string>
  2. #include <iostream>
  3.  
  4. namespace Baze
  5. {
  6.     unsigned long pocetObjektu=0;
  7.     class Objekt
  8.     {
  9.     private:
  10.         unsigned short _inventarnicislo;
  11.     public:
  12.         unsigned short VratInventarniCislo() const
  13.         {
  14.             return _inventarnicislo;
  15.         }
  16.         virtual void VypisInformace() const = 0;
  17.         friend bool operator>(const Objekt& a, const Objekt& b);
  18.         Objekt();
  19.         ~Objekt();
  20.     };
  21.     bool operator>(const Objekt& a, const Objekt& b){
  22.         return a.VratInventarniCislo() > b.VratInventarniCislo();
  23.     }
  24. }
  25. Baze::Objekt::Objekt()
  26. {
  27.     pocetObjektu++;
  28.     _inventarnicislo = pocetObjektu;
  29.     ;
  30. }
  31.  
  32. Baze::Objekt::~Objekt()
  33. {
  34.     pocetObjektu--;
  35. }
  36.  
  37. namespace Objekty
  38. {
  39.     namespace Nabytek
  40.     {
  41.         class Stul : public Baze::Objekt
  42.         {
  43.         private:
  44.             std::string barva;
  45.             std::string material;
  46.         public:
  47.             Stul(std::string b, std::string m);
  48.             ~Stul();
  49.             void VypisInformace() const override;
  50.         };
  51.  
  52.         Stul::Stul(std::string b, std::string m)
  53.         {
  54.             material = m, barva = b;
  55.  
  56.         }
  57.  
  58.         Stul::~Stul()
  59.         {
  60.         }
  61.  
  62.         void Stul::VypisInformace() const
  63.         {
  64.             std::cout << "Objekt s inventarnim cislem: " << VratInventarniCislo()
  65.                 << " je typu Stul, barvy " << barva << " z materialu " << material << std::endl;
  66.         }
  67.  
  68.         class Zidle : public Baze::Objekt
  69.         {
  70.         private:
  71.             bool otocna;
  72.             bool ergonomicka;
  73.         public:
  74.             Zidle(bool otacise,bool ergonomi);
  75.             ~Zidle();
  76.             void Zidle::VypisInformace() const override
  77.             {
  78.                 std::cout << "Objekt s inventarnim cislem: " << VratInventarniCislo()
  79.                     << " je typu Zidle, otaci se <" << otocna << ">, Je ergonomicka <" << ergonomicka<<">"<< std::endl;
  80.             }
  81.         };
  82.  
  83.         Zidle::Zidle(bool otacise,bool ergonomi)
  84.         {
  85.             otocna = otacise;
  86.             ergonomicka = ergonomi;
  87.         }
  88.  
  89.         Zidle::~Zidle()
  90.         {
  91.         }
  92.  
  93.         class Skrin : public Baze::Objekt
  94.         {
  95.             typedef size_t rozmer;
  96.         private:
  97.             std::string material;
  98.             rozmer vyska;
  99.             rozmer sirka;
  100.             rozmer hloubka;
  101.         public:
  102.             Skrin(std::string m, rozmer v, rozmer s, rozmer h);
  103.             ~Skrin();
  104.             void Skrin::VypisInformace() const override
  105.             {
  106.                 std::cout << "Objekt s inventarnim cislem: " << VratInventarniCislo()
  107.                     << " je typu Skrin, vysky " << vyska << " sirky " << sirka << " hloubky" << std::endl
  108.                     << "z materialu " << material << std::endl;
  109.             }
  110.         };
  111.  
  112.         Skrin::Skrin(std::string m, rozmer v, rozmer s, rozmer h)
  113.         {
  114.             material = m;
  115.             vyska = v;
  116.             sirka = s;
  117.             hloubka = h;
  118.         }
  119.  
  120.         Skrin::~Skrin()
  121.         {
  122.         }
  123.     }
  124.  
  125.     class Inventar
  126.     {
  127.     private:
  128.     const Baze::Objekt * *pole;
  129.     int pocet=0;
  130.     public:
  131.         unsigned short NajdiLimit() const;
  132.         void Pridej(const Baze::Objekt* objekt);
  133.         Inventar(size_t pocet);
  134.         ~Inventar();
  135.         void VypisInventar() const;
  136.     };
  137.  
  138.     Inventar::Inventar(size_t  pocet){
  139.         pole = new const Baze::Objekt*[pocet];
  140.         for (int i = 0; i < pocet; i++){
  141.             pole[i] = nullptr;
  142.         }
  143.     }
  144.  
  145.     Inventar::~Inventar(){
  146.         delete[] pole;
  147.     }
  148.  
  149.     void Inventar::Pridej(const Baze::Objekt* const objekt){
  150.         for (int i = 0; i < sizeof(pole); i++){
  151.             if (pole[i] == nullptr){
  152.                 pole[i] = objekt;
  153.                 pocet++;
  154.                 break;
  155.             }
  156.         }
  157.     }
  158.     void Inventar::VypisInventar() const{
  159.         std::cout << "Pocet polozek v Inventari: " << pocet << std::endl<<"Inventar: "<<std::endl;
  160.         for (int i = 0; i < sizeof(pole); i++){
  161.             if (pole[i] != nullptr){
  162.                 pole[i]->VypisInformace();
  163.             }
  164.             else{
  165.                 break;
  166.             }
  167.         }
  168.     }
  169.  
  170.     unsigned short Inventar::NajdiLimit() const
  171.     {
  172.         short maximum = 0;
  173.         for (size_t i = 1; i < pocet; i++)
  174.         {
  175.             if (pole[i]>pole[i-1])
  176.             {
  177.                 maximum = pole[i]->VratInventarniCislo();
  178.             }
  179.         }
  180.         return maximum;
  181.     }
  182. }
  183.  
  184. int main()
  185. {
  186.     using namespace std;
  187.     Objekty::Nabytek::Stul* stul;
  188.     stul = new Objekty::Nabytek::Stul("cervena", "drevo");
  189.     stul->VypisInformace();
  190.     Objekty::Nabytek::Zidle* zidle;
  191.     zidle = new Objekty::Nabytek::Zidle(true, "drevo");
  192.     zidle->VypisInformace();
  193.     Objekty::Nabytek::Skrin* skrin;
  194.     skrin=new Objekty::Nabytek::Skrin("ocel", 200, 100, 50);
  195.     skrin->VypisInformace();
  196.  
  197.     Objekty::Inventar inventory(10);
  198.     inventory.Pridej(stul);
  199.     inventory.Pridej(zidle);
  200.     inventory.Pridej(skrin);
  201.     inventory.VypisInventar();
  202.     cout << "Limit je: " << inventory.NajdiLimit() << endl;
  203.     system("pause");
  204.     return 0;
  205. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement