Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2020
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.56 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3.  
  4. using namespace std;
  5.  
  6. struct Data_produkcji
  7. {
  8.     int dzien, miesiac, rok;
  9. };
  10.  
  11. struct Przedmiot
  12. {
  13.     static int ilosc;
  14.     int id;
  15.     string nazwa;
  16.     double cena;
  17.     void wypisz();
  18.  
  19.     Data_produkcji dp;
  20.  
  21.     Przedmiot() { ; }
  22.  
  23.     Przedmiot(int _id, string _nazwa, double _cena, int _dzien, int _miesiac, int _rok) //KONSTRUKTOR to co sie wykonuje podczas tworzenia obiektu
  24.     {
  25.         ilosc++;
  26.         id = _id;
  27.         nazwa = _nazwa;
  28.         cena = _cena;
  29.         dp.dzien = _dzien;
  30.         dp.miesiac = _miesiac;
  31.         dp.rok = _rok;
  32.     }
  33.  
  34.     ~Przedmiot() //DESTRYKTOR to co się wykonuje po usunięciu obiektu
  35.     {
  36.         ilosc--;
  37.     }
  38. };
  39.  
  40. int Przedmiot::ilosc = 0; //tak trzeba bo "static" i w sumie nie wiem czemu tak
  41.  
  42. /*
  43. Przedmiot::~Przedmiot() //mozna też tak definicja destruktora i konstruktora
  44. {
  45.     ilosc--;
  46. }
  47. */
  48.  
  49.  
  50. void Przedmiot::wypisz()
  51. {
  52.     cout << "---- " << nazwa << " ----" << endl;
  53.     cout << ilosc       << endl;
  54.     cout << id          << endl;
  55.     cout << cena        << endl;
  56.     cout << dp.dzien    << endl;
  57.     cout << dp.miesiac  << endl;
  58.     cout << dp.rok      << endl;
  59.     cout << "-----------" << endl;
  60. }
  61.  
  62. int main()
  63. {
  64.     Przedmiot p1(1, "myszka", 133, 01,01,2020);
  65.     Przedmiot p2(2, "laptop", 2110, 01,02,1999);
  66.     Przedmiot p3 = p2;
  67.     p3.cena = 3000;
  68.     p3.dp.rok = 2010;
  69.     p3.id = 3;
  70.  
  71.     Przedmiot* p4 = &p3;
  72.     p4->nazwa = "komputer";
  73.     p4->id = 4;
  74.  
  75.     Przedmiot* p5 = new Przedmiot;
  76.     *p5 = *p4;
  77.     p5->id = 5;
  78.     p5->nazwa = "ekran";
  79.     p5->cena = 720;
  80.  
  81.     Przedmiot magazyn[5] = { p1,p2,p3,*p4,*p5 };
  82.  
  83.     for (int i = 0; i < 5; i++)
  84.     {
  85.         magazyn[i].wypisz();
  86.     }
  87.  
  88.     return 0;
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement