I just need help finishing the class section and implementing the function calls
ID: 3635123 • Letter: I
Question
I just need help finishing the class section and implementing the function calls/definitions. I also included the directions for a specific function at the end.#include <fstream>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
class Song
{
private:
string artist;
string title;
int minutes;
int seconds;
int rating;
public:
// default constructor
Song();
// return artist attribute
string getArtist();
// return total duration in seconds
int getDuration();
// return minutes attribute
int getMinutes();
// return rating attribute from 1-5
int getRating();
// return rating as a string of * (star) characters
string getRatingAsStars();
// return seconds attribute
int getSeconds();
// return title attribute
string getTitle();
// determine if song a comes before song b in the ordering by comparing artists and titles
// return true if song a comes before and false otherwise
friend bool operator <(Song a, Song b);
// read a song from an input stream
friend istream& operator >>(istream& in, Song& song);
// write a song to an output stream in the following output-"artist – title – minutes:seconds – rating (in stars)"
friend ostream& operator <<(ostream& out, Song song);
};
class Collection
{
private:
vector<Song> songs;
// recursive selection sort function that sorts the vector starting with the startIndex
void selectionSort(vector<Song>& songs, int startIndex);
public:
Collection();
// return the average rating of all songs
double getAverageRating();
// return the average rating of all songs as a string of * (star) characters
string getAverageRatingAsStars();
// return total duration of all songs in seconds
int getDuration();
// return total minutes of all songs
int getMinutes();
// return the number of songs
int getNumberOfSongs();
// return the song at position number of the collection
Song getSong(int number);
// return total seconds of all songs as the remainder of the duration - minutes
int getSeconds();
// sort all songs according to artist and title
void sort();
// read a collection from an input stream
friend istream& operator >>(istream& in, Collection& collection);
// write a collection to an output stream. One song per line
friend ostream& operator <<(ostream& out, Collection collection);
};
int main()
{
// open song file
ifstream infile;
infile.open("songs.txt");
// check for errors
if (infile.fail())
{
cout << "could not open song file" << endl;
return -1;
}
// create a new collection
Collection collection;
// read collection from file
infile >> collection;
// get a song from the collection and display info
Song song = collection.getSong(2);
cout << "Artist: " << song.getArtist() << endl;
cout << "Title: " << song.getTitle() << endl;
cout << "Duration in total seconds: " << song.getDuration() << endl;
cout << song.getMinutes() << " minutes " << song.getSeconds() << " seconds" << endl;
cout << "Rating: " << song.getRating() << " as stars: " << song.getRatingAsStars() << endl;
cout << endl;
// use output operator
cout << song << endl;
cout << endl;
// display collection
cout << collection;
cout << endl;
cout << "Number of songs in collection: " << collection.getNumberOfSongs() << endl;
cout << "Duration in total seconds: " << collection.getDuration() << endl;
cout << collection.getMinutes() << " minutes " << collection.getSeconds() << " seconds" << endl;
cout << "Rating: " << collection.getAverageRating() << " as stars: " << collection.getAverageRatingAsStars() << endl;
cout << endl;
// display sorted collection
collection.sort();
cout << "Sorted collection:" << endl;
cout << collection;
infile.close();
return 0;
}
istream& operator >>(istream& in, Song& song)
{
getline(in, song.artist, '-');
getline(in, song.title, '-');
char delimiter;
in >> song.minutes;
in >> delimiter;
in >> song.seconds;
in >> delimiter;
string rating;
in >> rating;
song.rating = rating.length();
string newline;
getline(in, newline);
return in;
}
istream& operator >>(istream& in, Collection& collection)
{
while (!in.eof())
{
Song song;
in >> song;
if (song.getMinutes() > 0 && song.getSeconds() > 0)
collection.songs.push_back(song);
}
return in;
}
"
? void selectionSort(vector<Song>& songs, int startIndex)
? This function is private and performs a recursive selection sort that sorts the vector starting with the startIndex
? Remember that selection sort works by continuously finding the smallest element in a list and moving it to the
beginning of the list. Hence:
? The recursive version needs base case to determine when we should stop sorting
? Then we need to find the smallest item in the list
? Swap the smallest element with the one in the beginning of the list
? Call selectionSort recursively with the same list but startIndex + 1 to sort the remainder of the list"
Explanation / Answer
Leave that, Just try this.
//Song Class
#ifndef SONG_CLASS
#define SONG_CLASS
using namespace std;
class Song
{
private:
string title; //dynamic allocation
string album;
string genre;
string artist;
double durationn;
public:
Song();
void setTitle(string t);
void setDuration(double d);
void setAlbumName(string a);
void setGenre(string g);
void setArtist(string a);
string getTitle();
string getAlbum()const;
string getGenre()const;
string getArtist()const;
double getDuration)(); //accessor
};
//constructor
Song::Song() //constructor
{
title="";
album="";
genre="";
artist="";
duration=0;
}
//accessor for name
string Song::getTitle()
{
return title;
}
//mutator
void Song::setTitle(string t)
{
title=t;
}
//accessor for name
string Song::getAlbum()
{
return album;
}
//mutator
void Song::setAlbumName(string a)
{
album=a;
}
//accessor for name
string Song::getGenre()
{
return genre;
}
//mutator
void Song::setGenre(string g)
{
genre=g;
}
//accessor for name
string Song::getArtist()
{
return artist;
}
//mutator
void Song::setArtist(string s)
{
artist=s;
}
void Song::setDuration(double d)
{
duration=d;
}
double Song::getDuration()
{
return duration;
}