Facebook
From Bsbs, 2 Weeks ago, written in Plain Text.
This paste is a reply to Jeehhe from Hwhh - view diff
Embed
Download Paste or View Raw
Hits: 118
  1. #include <iostream>
  2. #include <string>
  3.  
  4. using namespace std;
  5.  
  6. class Person
  7. {
  8. private:
  9.     string imie;
  10.     string nazwisko;
  11.     string pesel;
  12.  
  13. public:
  14.     // Konstruktor parametryczny z domyślnymi wartościami
  15.     Person(string imie = "", string nazwisko = "", string pesel = "")
  16.         : imie(imie), nazwisko(nazwisko), pesel(pesel) {}
  17.  
  18.     string get_imie() const {
  19.         return imie;
  20.     }
  21.  
  22.     string get_nazwisko() const {
  23.         return nazwisko;
  24.     }
  25.  
  26.     string get_pesel() const {
  27.         return pesel;
  28.     }
  29.  
  30.     string to_string() const {
  31.         return "Osoba: " + imie + " " + nazwisko + ", Pesel: " + pesel;
  32.     }
  33. };
  34.  
  35. class Student : public Person
  36. {
  37. private:
  38.     string nr_indeksu;
  39.     string kierunek_studiow;
  40.  
  41. public:
  42.     // Konstruktor parametryczny dla klasy Student, wywołujący konstruktor klasy bazowej Person
  43.     Student(string imie, string nazwisko, string pesel, string nr_indeksu, string kierunek_studiow)
  44.         : Person(imie, nazwisko, pesel), nr_indeksu(nr_indeksu), kierunek_studiow(kierunek_studiow) {}
  45.  
  46.     // Nadpisanie metody to_string() dla klasy Student
  47.     string to_string() const {
  48.         return "Student: " + get_imie() + " " + get_nazwisko() + ", Pesel: " + get_pesel() +
  49.                ", Nr indeksu: " + nr_indeksu + ", Kierunek studiow: " + kierunek_studiow;
  50.     }
  51. };
  52.  
  53. int main()
  54. {
  55.     // Przykładowe użycie klasy Student
  56.     Student student("Jan", "Kowalski", "12345678901", "S12345", "Informatyka");
  57.  
  58.     // Wyświetlenie informacji o studencie za pomocą metody to_string() klasy Student
  59.     cout << student.to_string() << endl;
  60.  
  61.     return 0;
  62. }
  63. W tej poprawionej wersji kodu:
  64.  
  65. Poprawiono konstruktor klasy Person, aby poprawnie inicjować pola imie, nazwisko i pesel.
  66. Metody get_imie(), get_nazwisko() i get_pesel() zostały poprawnie zdefiniowane jako const
  67.  
  68. Message ChatGPT…
  69.  
  70. ChatGPT