Advertisement
Holek

Untitled

May 6th, 2017
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.15 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. using namespace std;
  5. class Student{
  6. protected:
  7.     string imie;
  8.     string nazwisko;
  9. public:
  10.     Student();
  11.     Student(string,string);
  12.     string getImie(){
  13.         return this->imie;
  14.     }
  15.     string getNazwisko(){
  16.         return this->nazwisko;
  17.     }
  18.     void zapisz(string src){
  19.         fstream file;
  20.         file.open(src.c_str(),ios::out); // .c_str() zwraca łańcuch tylko do odczytu
  21.         file<<this->imie<<endl;
  22.         file<<this->nazwisko<<endl;
  23.         file.close();
  24.     }
  25.     void wczytaj(string src){
  26.         fstream file;
  27.         file.open(src.c_str(),ios::in);
  28.         getline(file,this->imie);
  29.         getline(file,this->nazwisko);
  30.         file.close();
  31.     }
  32.  
  33. };
  34. Student::Student(){
  35.     nazwisko="";
  36.     imie="";
  37. }
  38. Student::Student(string imie,string nazwisko){
  39.     this->nazwisko=nazwisko;
  40.     this->imie=imie;
  41. }
  42. class StudentKierunek : public Student {
  43. protected:
  44.     string kierunek;
  45. public:
  46.     StudentKierunek(string,string,string);
  47.     StudentKierunek();
  48.     void wczytaj(string src){
  49.         fstream file;
  50.         file.open(src.c_str(),ios::in);
  51.         getline(file,this->imie);
  52.         getline(file,this->nazwisko);
  53.         getline(file,this->kierunek);
  54.         file.close();
  55.     }
  56.     void zapisz(string src){
  57.         fstream file;
  58.         file.open(src.c_str(),ios::out); // .c_str() zwraca łańcuch tylko do odczytu
  59.         file<<this->imie<<endl;
  60.         file<<this->nazwisko<<endl;
  61.         file<<this->kierunek<<endl;
  62.         file.close();
  63.  
  64.     }
  65.     string getKierunek(){
  66.         return this->kierunek;
  67.     }
  68.  
  69.  
  70. };
  71. StudentKierunek::StudentKierunek(string imie,string nazwisko,string kierunek){
  72.     this->nazwisko=nazwisko;
  73.     this->imie=imie;
  74.     this->kierunek=kierunek;
  75. }
  76. StudentKierunek::StudentKierunek(){
  77.     imie="";
  78.     nazwisko="";
  79.     kierunek="";
  80. }
  81. int main()
  82. {
  83.     string plik = "plik.txt";
  84.     StudentKierunek s1("Imie","Nazwisko","Kierunek");
  85.     s1.zapisz(plik);
  86.     StudentKierunek s2;
  87.     s2.wczytaj(plik);
  88.     cout<< s2.getImie() << endl << s2.getNazwisko()<< endl << s2.getKierunek();
  89.  
  90.     return 0;
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement