Advertisement
Holek

Untitled

May 6th, 2017
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.13 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.  
  43. int main()
  44. {
  45.     string plik = "plik.txt";
  46.     Student s1("Imie","Nazwisko");
  47.     s1.zapisz(plik);
  48.     Student s2();
  49.     s2.wczytaj(plik);
  50.     cout<< s2.getImie() << endl << s2.getNazwisko();
  51.  
  52.     return 0;
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement