Holek

Untitled

May 7th, 2017
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.23 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. #include <sstream>
  5. using namespace std;
  6. /*
  7. Napisz klasę Licznik, zawierającą:
  8. – prywatne pole liczba (typu int)
  9. – konstruktor przyjmujący pojedynczy parametr, inicjalizujący pole liczba wartością
  10. argumentu
  11. – akcesor (getter) pola liczba
  12. – metodę inkrementuj(), inkrementującą wartość pola liczba
  13. Następnie napisz klasę LicznikOdZera, dziedziczącą po klasie Licznik. Nowa klasa powinna
  14. posiadać wyłącznie konstruktor domyślny (bezargumentowy), inicjalizujący pole liczba wartością
  15. zero (należy zauważyć, że pole z klasy bazowej jest prywatne).
  16. */
  17. class Licznik{
  18. private:
  19.     int liczba;
  20. public:
  21.     Licznik(int l){
  22.         this->liczba=l;
  23.     }
  24.     int getLiczba(){
  25.         return this->liczba;
  26.     }
  27.     void inkrementuj(){
  28.         this->liczba++;
  29.     }
  30.     void setLiczba(int l){
  31.         this->liczba = l;
  32.     }
  33.  
  34. };
  35. class LicznikOdZera : public Licznik {
  36. public:
  37.     LicznikOdZera():Licznik(0){};
  38. };
  39. int main()
  40. {
  41.     Licznik a1(5);
  42.     cout << a1.getLiczba() << endl;
  43.     a1.inkrementuj();
  44.     cout << a1.getLiczba() << endl;
  45.     LicznikOdZera a2;
  46.     a1 = a2;
  47.     cout << a1.getLiczba() << endl;
  48.  
  49.     return 0;
  50. }
Advertisement
Add Comment
Please, Sign In to add comment