fork download
  1. #include<iostream>
  2. #include<fstream>
  3. #include<string>
  4. using namespace std;
  5.  
  6. class Entry {
  7. private:
  8. string title;
  9. string content;
  10. public:
  11. Entry(string t, string c) {
  12. title = t;
  13. content = c;
  14. }
  15. string getTitle() {
  16. return title;
  17. }
  18. string getContent() {
  19. return content;
  20. }
  21. };
  22.  
  23. class Diary {
  24. private:
  25. string fileName;
  26. public:
  27. Diary(string fName) {
  28. fileName = fName;
  29. }
  30. void writeEntry(Entry e) {
  31. ofstream file;
  32. file.open(fileName, ios::app);
  33. file << e.getTitle() << endl;
  34. file << e.getContent() << endl;
  35. file.close();
  36. }
  37. void displayEntries() {
  38. ifstream file;
  39. file.open(fileName);
  40. string line;
  41. while(getline(file, line)) {
  42. cout << line << endl;
  43. }
  44. file.close();
  45. }
  46. };
  47.  
  48. int main() {
  49. Diary diary("myDiary.txt");
  50. Entry entry1("First Entry", "This is my first entry in the diary.");
  51. diary.writeEntry(entry1);
  52. Entry entry2("Second Entry", "This is my second entry in the diary.");
  53. diary.writeEntry(entry2);
  54. diary.displayEntries();
  55. return 0;
  56. }
Success #stdin #stdout 0.01s 5528KB
stdin
Standard input is empty
stdout
Standard output is empty