Advertisement
MeehoweCK

Untitled

Jun 1st, 2023
729
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.08 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. class Samochod
  6. {
  7. private:        // pola prywatne (cechy obiektu)
  8.     float fuel;
  9.     int passengers;
  10.     float speed;
  11. public:         // metody publiczne (działania obiektu)
  12.     Samochod();         // konstruktor domyślny (bezargumentowy)
  13.     void accelerate();
  14.     void refuel(float amount);
  15.     void print();
  16. };
  17.  
  18. Samochod::Samochod()
  19. {
  20.     fuel = 0;
  21.     passengers = 5;
  22.     speed = 0;
  23.     cout << "Samochod zostal stworzony\n";
  24. }
  25.  
  26. void Samochod::accelerate()
  27. {
  28.     if (fuel == 0)
  29.     {
  30.         cout << "Nie mozna przyspieszyc!\n";
  31.         return;
  32.     }
  33.     speed += 10;
  34.     --fuel;
  35.     cout << "Samochod przyspieszyl, teraz jedzie " << speed << " km/h\n";
  36. }
  37.  
  38. void Samochod::refuel(float amount)
  39. {
  40.     fuel += amount;
  41.     cout << "Paliwo dotankowano, teraz jest " << fuel << " litrow\n";
  42. }
  43.  
  44. void Samochod::print()
  45. {
  46.     cout << "fuel: " << fuel << endl;
  47.     cout << "passengers: " << passengers << endl;
  48.     cout << "speed: " << speed << endl;
  49. }
  50.  
  51. int main()
  52. {
  53.     Samochod s1;        // wywołanie konstruktora domyślnego
  54.     s1.accelerate();
  55.     s1.refuel(20);
  56.     s1.accelerate();
  57.     s1.print();
  58.  
  59.     return 0;
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement