modify that structure by adding a constructor SongCollection in place of the ini
ID: 3680465 • Letter: M
Question
modify that structure by adding a constructor SongCollection in place of the initialize function and converting the remaining functions in songcollection.h from standalone functions to member functions.
Your final version of SongCollection should compile and run with the other code provided for this assignment. That code has already been altered to work with the desired member functions for SongCollection. You may not change any code except what is in songcollection.h and songcollection.cpp.
Your modifications should not alter the output of the program in any way.
songcollection.cpp
#include <iostream>
#include <string>
#include "songcollection.h"
using namespace std;
/**
* Set up a new collection;
*/
void initialize (SongCollection& collection)
{
collection.numSongs = 0;
collection.totalTime = time(0,0,0);
}
/**
* Add a single song to a collection.
*/
void addSong (SongCollection& toCollection,
Song song)
{
int n = toCollection.numSongs;
toCollection.songs[n] = song;
++toCollection.numSongs;
add (toCollection.totalTime, song.length);
}
/**
* Add into one collection all songs from another collection that
* match a given artist and/or title.
*/
void addMatchesFor (const SongCollection& fromCollection,
SongCollection& toCollection,
std::string artist,
std::string title)
{
for (int i = 0; i < fromCollection.numSongs; ++i)
{
Song s = fromCollection.songs[i];
if (matches(artist, title, s.artist, s.title))
{
addSong (toCollection, s);
}
}
}
void print (std::ostream& out, const SongCollection& collection)
{
for (int i = 0; i < collection.numSongs; ++i)
{
Song s = collection.songs[i];
print (out, s);
out << endl;
}
out << "Total: ";
print(out, collection.totalTime);
out << endl;
}
/**
* Attempt to read a song from an input stream, adding it
* to the collection if possible,
*/
bool read (std::istream& in, SongCollection& collection)
{
initialize(collection);
string title, artist, album;
int track;
Time length;
Song s;
while (read(in, s))
{
addSong(collection, s);
}
return in;
}
songcollection.h
modify that structure by adding a constructor SongCollection in place of the initialize function and converting the remaining functions in songcollection.h from standalone functions to member functions.
Your final version of SongCollection should compile and run with the other code provided for this assignment. That code has already been altered to work with the desired member functions for SongCollection. You may not change any code except what is in songcollection.h and songcollection.cpp.
Your modifications should not alter the output of the program in any way.
songcollection.cpp
#include <iostream>
#include <string>
#include "songcollection.h"
using namespace std;
/**
* Set up a new collection;
*/
void initialize (SongCollection& collection)
{
collection.numSongs = 0;
collection.totalTime = time(0,0,0);
}
/**
* Add a single song to a collection.
*/
void addSong (SongCollection& toCollection,
Song song)
{
int n = toCollection.numSongs;
toCollection.songs[n] = song;
++toCollection.numSongs;
add (toCollection.totalTime, song.length);
}
/**
* Add into one collection all songs from another collection that
* match a given artist and/or title.
*/
void addMatchesFor (const SongCollection& fromCollection,
SongCollection& toCollection,
std::string artist,
std::string title)
{
for (int i = 0; i < fromCollection.numSongs; ++i)
{
Song s = fromCollection.songs[i];
if (matches(artist, title, s.artist, s.title))
{
addSong (toCollection, s);
}
}
}
void print (std::ostream& out, const SongCollection& collection)
{
for (int i = 0; i < collection.numSongs; ++i)
{
Song s = collection.songs[i];
print (out, s);
out << endl;
}
out << "Total: ";
print(out, collection.totalTime);
out << endl;
}
/**
* Attempt to read a song from an input stream, adding it
* to the collection if possible,
*/
bool read (std::istream& in, SongCollection& collection)
{
initialize(collection);
string title, artist, album;
int track;
Time length;
Song s;
while (read(in, s))
{
addSong(collection, s);
}
return in;
}
songcollection.h
Explanation / Answer
The songcollection struct has been modified to have all the functions related to it to be member functions. The modified files are given below. Please use it with your other files.
songcollection.h
#ifndef SONGCOLLECTION_H
#define SONGCOLLECTION_H
#include <iostream>
#include <string>
#include "playlistitem.h"
#include "song.h"
#include "time.h"
const int MAXSONGS = 1000;
struct SongCollection {
int numSongs;
Song songs[MAXSONGS];
Time totalTime;
/**
* Set up a new collection;
*/
SongCollection ();
/**
* Add a single song to a collection.
*/
void addSong (Song song);
/**
* Add into one collection all songs from another collection that
* match a given artist and/or title.
*/
void addMatchesFor (SongCollection& toCollection,
std::string artist,
std::string title);
/**
* Print a collection to the indicated output stream
*/
void print (std::ostream& out);
/**
* Attempt to read a collection of songs from an input stream,
* adding it to the collection if possible,
*/
bool read (std::istream& in);
};
#endif
songcollection.cpp
#include <iostream>
#include <string>
#include "songcollection.h"
using namespace std;
/**
* Set up a new collection;
*/
SongCollection::SongCollection ()
{
numSongs = 0;
totalTime = time(0,0,0);
}
/**
* Add a single song to a collection.
*/
void SongCollection::addSong(Song song)
{
songs[numSongs] = song;
++numSongs;
add(toCollection.totalTime, song.length);
}
/**
* Add into one collection all songs from another collection that
* match a given artist and/or title.
*/
void SongCollection::addMatchesFor (
SongCollection& toCollection,
std::string artist,
std::string title)
{
for (int i = 0; i < numSongs; ++i)
{
Song s = songs[i];
if (matches(artist, title, s.artist, s.title))
{
toCollection.addSong (s);
}
}
}
void SongCollection::print (std::ostream& out)
{
for (int i = 0; i < numSongs; ++i)
{
Song s = songs[i];
print (out, s);
out << endl;
}
out << "Total: ";
print(out, totalTime);
out << endl;
}
/**
* Attempt to read a song from an input stream, adding it
* to the collection if possible,
*/
bool SongCollection::read (std::istream& in)
{
Song s;
while (read(in, s))
{
addSong(s);
}
return in;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.