fork download
  1. // Attached: Lab 8 Song Class
  2. // ===========================================================
  3. // File: Song_Combined.cpp
  4. // ===========================================================
  5. // Programmer: Elaine Torrez
  6. // Class: CMPR 121
  7. // ===========================================================
  8.  
  9. #include <iostream>
  10. #include <string>
  11. using namespace std;
  12.  
  13. class Song
  14. {
  15. private:
  16. string title;
  17. string artist;
  18.  
  19. public:
  20. Song();
  21. Song(string songTitle, string songArtist);
  22. ~Song();
  23.  
  24. void setTitle(string songTitle);
  25. void setArtist(string songArtist);
  26. void displaySong();
  27. };
  28.  
  29. // ===========================================================
  30. // Default Constructor
  31. // ===========================================================
  32. Song::Song()
  33. {
  34. title = "";
  35. artist = "";
  36. }
  37.  
  38. // ===========================================================
  39. // Overloaded Constructor
  40. // ===========================================================
  41. Song::Song(string songTitle, string songArtist)
  42. {
  43. title = songTitle;
  44. artist = songArtist;
  45. }
  46.  
  47. // ===========================================================
  48. // Destructor
  49. // ===========================================================
  50. Song::~Song()
  51. {
  52. }
  53.  
  54. // ===========================================================
  55. // setTitle
  56. // ===========================================================
  57. void Song::setTitle(string songTitle)
  58. {
  59. title = songTitle;
  60. }
  61.  
  62. // ===========================================================
  63. // setArtist
  64. // ===========================================================
  65. void Song::setArtist(string songArtist)
  66. {
  67. artist = songArtist;
  68. }
  69.  
  70. // ===========================================================
  71. // displaySong
  72. // ===========================================================
  73. void Song::displaySong()
  74. {
  75. cout << "Title: " << title << endl;
  76. cout << "Artist: " << artist << endl;
  77. }
  78.  
  79. // ===========================================================
  80. // main
  81. // ===========================================================
  82. int main()
  83. {
  84. Song yourSong;
  85. Song anotherSong("All of Me", "Billie Holliday");
  86.  
  87. string userTitle;
  88. string userArtist;
  89.  
  90. cout << "Here are the songs:" << endl;
  91. cout << endl;
  92.  
  93. cout << "Your Song:" << endl;
  94. yourSong.displaySong();
  95. cout << endl;
  96.  
  97. cout << "Another Song:" << endl;
  98. anotherSong.displaySong();
  99. cout << endl;
  100.  
  101. cout << "Enter information about your song:" << endl;
  102. cout << "Title: ";
  103. getline(cin, userTitle);
  104. cout << "Artist: ";
  105. getline(cin, userArtist);
  106.  
  107. yourSong.setTitle(userTitle);
  108. yourSong.setArtist(userArtist);
  109.  
  110. cout << endl;
  111. cout << "Here is your song (after the set functions):" << endl;
  112. yourSong.displaySong();
  113.  
  114. return 0;
  115. }
Success #stdin #stdout 0.01s 5320KB
stdin
Like a Virgin
Madonna
stdout
Here are the songs:

Your Song:
Title:   
Artist:  

Another Song:
Title:   All of Me
Artist:  Billie Holliday

Enter information about your song:
Title:   Artist:  
Here is your song (after the set functions):
Title:   Like a Virgin
Artist:  Madonna