Create a song class. Each song will have: In addition the class should have the
ID: 3563627 • Letter: C
Question
Create a song class. Each song will have:
In addition the class should have the following functions:
void setArtistName(string name); // Sets name to the artist name in the object
void setMinutes(int min); // Sets the minutes in the object
void setSeconds(int secs); // Sets seconds for the song (minutes + seconds equals total length)
void setComposer(string comp);
void setSongTItle(string songName); // sets song title in the object
void setAlbumName(string albumName); // sets the title of the album.
void displaySong(); // displays the song's information
string getArtistName(); // Returns the artist information
int getMinutes(); // Returns the minutes in the object
int getSeconds(); // Returns the seconds for the song (minutes + seconds equals total length)
string getComposer(); // Returns the composer
string getSongTItle(); // Returns song title in the object
string getAlbumName(); // Returns the title of the album.
Your main function should then make 3 song objects and display their information from shortest song to longest song. Upload a zipped file with the .h, and 2 .cpp files (one for the implementation and one for the main()).
Explanation / Answer
Specification (Header) File
#include <iostream>
#include <string>
using namespace std;
class Song {
public:
Song();
Song(string newTitle, string newArtist, string newAlbum);
string GetSongTitle() const;
string GetArtistName() const;
string GetAlbumName() const;
void SetSongTitle(string newTitle);
void SetArtistName(string newArtist);
void SetAlbumName(string newAlbum);
string DisplaySongData() const;
private:
string name;
string artist;
string album;
};
Implementation File
/* Implementation file (song.cpp)
To be used in conjunction with song.h
*/
#include <iostream>
#include <string>
#include "Song.h"
using namespace std;
Song::Song() {
name = " ";
artist = " ";
album = " ";
}
Song::Song(string newTitle, string newArtist, string newAlbum) {
name = newTitle;
artist = newArtist;
album = newAlbum;
}
string Song::GetSongTitle() const {
return name;
}
string Song::GetArtistName() const {
return artist;
}
string Song::GetAlbumName() const {
return album;
}
void Song::SetSongTitle(string newTitle) {
name = newTitle;
}
void Song::SetArtistName(string newArtist) {
artist = newArtist;
}
void Song::SetAlbumName(string newAlbum) {
album = newAlbum;
}
string Song::DisplaySongData() const {
string displayString;
displayString = "Title: " + name + " " + "Artist: " + artist + " " + "Album: " + album;
return "Title: " + name + " " + "Artist: " + artist + " " + "Album: " + album;
}
Driver File
#include "Song.h"
#include <fstream>
#include <iostream>
#include <string>
using namespace std;
// Function Prototypes Declared
void whiteSpaceRemover(Song songList[], int lineCount); // Called to trim the whitespace from a string
string equalityTestPrep(string str); // Removes spaces and converts to lowercase to facilitate string comparisons
// Main function
int main() {
// Variable declarations
string artistSearch, songTitle, artistName, albumName;
int lineCount = 0, count = 0;
int token = 0, token2 = 0, token3 = 0;
// Opens input stream for the file
ifstream songFile;
songFile.open("song.in");
// Cycles through the file to evaluate how large an array is needed
while (getline(songFile, artistSearch)) {
lineCount++;
}
artistSearch = "";
// Closes file to be re-opened to reset the line reader
songFile.close();
songFile.open("song.in");
Song songList[lineCount];
string songs[lineCount];
// Stores each line of the file into an array of strings
while (songFile) {
getline(songFile, songs[count]);
count++;
}
// Breaks up and assigns the string values from songs[] to the songList type
for (int i = 0; i < lineCount; i++) {
token = songs[i].find(':');
if(token != string::npos)
songList[i].SetSongTitle(songs[i].substr(0, token));
token2 = songs[i].substr(token + 1, songs[i].length() - 1).find(':') + token;
if(token2 != string::npos)
songList[i].SetArtistName(songs[i].substr(token + 1, token2 - token));
token3 = songs[i].length() - 1;
if(token3 != string::npos)
songList[i].SetAlbumName(songs[i].substr(token2 + 1, token3 - token2));
}
songFile.close();
// Prompts the user for the artist by which to search the library for
cout << "Enter the artist to search for: ";
getline(cin, artistSearch);
cout << endl << endl;
// Calls the function to remove excess whitespace from the strings within the songList array
whiteSpaceRemover(songList, lineCount);
// Tests for equality and outputs all information related to the artist
for(int i = 0; i < lineCount; i++) {
songTitle = songList[i].GetArtistName();
if(equalityTestPrep(songTitle) == equalityTestPrep(artistSearch)) {
artistName = songList[i].GetArtistName();
albumName = songList[i].GetAlbumName();
cout << songTitle << " : " << artistName << " : " << albumName << endl;
}
}
}
// Trims the whitespace from the songList[] strings for prettier output and better organization
void whiteSpaceRemover(Song songList[], int lineCount) {
int token;
string songTitle, artistName, albumName;
for(int i = 0; i < lineCount; i++) {
songTitle = songList[i].GetSongTitle();
token = songTitle.find(' ');
if(token != string::npos)
songList[i].SetSongTitle(songTitle.substr(0, token));
artistName = songList[i].GetArtistName();
token = artistName.find_first_not_of(' ');
if(token != string::npos) {
songList[i].SetArtistName(artistName.substr(token, artistName.find(' ') - token));
songList[i].SetArtistName(artistName.substr(0, artistName.find(' ')));
}
albumName = songList[i].GetAlbumName();
token = albumName.find(' ');
if(token != string::npos)
songList[i].SetAlbumName(albumName.substr(1, token));
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.