In: Computer Science
C++
MAIN remains same
Given the complete main() function, partial playlist class header playlist.h, and playlist.cpp, you will complete the class declaration and class implementation. The following member functions are required:
A description of all of these functions is available in the textbook's chapter 16 (which you should have already studied).
NOTE: Your linked list class should NOT be templated.
Example: If the input is:
3 Linda Ronstadt You're no good 2.30 Rock Elton John Rocket Man 4.41 Rock Antonin Leopold Dvorak Songs my mother taught me 2.24 Classical
where 3 is the number of songs, and each subsequent four lines is an (artist, title, length, genre) record, the output is:
Antonin Leopold Dvorak, Songs my mother taught me, 2.24, Classical Elton John, Rocket Man, 4.41, Rock Linda Ronstadt, You're no good, 2.3, Rock
_______________________________________________
#include <iostream>
#include <string>
#include "playlist.h"
using namespace std;
int main()
{
song tune;
string genre;
playlist mySongs;
int num = 0;
cin >> num >> ws;
for (int i=0; i<num; i++)
{
getline(cin, tune.artist);
getline(cin, tune.title);
cin >> tune.length >> ws;
getline(cin, genre);
if (genre == "Rock")
tune.genre = genre_t::ROCK;
else if (genre == "Country")
tune.genre = genre_t::COUNTRY;
else if (genre == "Pop")
tune.genre = genre_t::POP;
else if (genre == "Classical")
tune.genre = genre_t::CLASSICAL;
else
tune.genre = genre_t::POLKA;
mySongs.addSong(tune);
}
mySongs.displayList();
return 0;
}
________________________________________________
#ifndef PLAYLIST_H
#define PLAYLIST_H
#include <iostream>
#include <string>
enum class genre_t {ROCK, COUNTRY, POP, CLASSICAL, POLKA};
struct song
{
std::string artist;
std::string title;
float length;
genre_t genre;
song* next;
};
class playlist
{
public:
// TODO: add the required member functions and operator
private:
song* head;
};
#endif
_________________________________________________________________
#include "playlist.h"
// TODO: implement the class member functions and overloaded operator
Program
playlist.h
#ifndef PLAYLIST_H
#define PLAYLIST_H
#include <iostream>
#include <string>
enum class genre_t { ROCK, COUNTRY, POP, CLASSICAL, POLKA };
struct song
{
std::string artist;
std::string title;
float length;
genre_t genre;
song* next;
};
class playlist
{
public:
//Default constructor
playlist();
//Copy constructor
playlist(playlist& p);
//Destructor
~playlist();
//Add a new song at the front of the list
void addSong(song tune);
//Display play list
void displayList();
//Assignment operator overload
void operator=(playlist& p);
private:
song* head;
};
#endif
playlist.cpp
#include "playlist.h"
//Default constructor
playlist::playlist() {
head = NULL;
}
//Copy constructor
playlist::playlist(playlist& p) {
if (p.head == NULL)
head = NULL;
else
{
song* temp = head;
while (p.head != NULL) {
temp =
p.head;
p.head =
p.head->next;
temp =
temp->next;
}
}
}
//Destructor
playlist::~playlist() {
delete head;
}
//Add a new song at the front of the list
void playlist::addSong(song tune) {
if (head == NULL) {
head = &tune;
return;
}
else {
tune.next = head;
head = &tune;
return;
}
}
//Display play list
void playlist::displayList() {
song* temp = head;
while (temp != NULL) {
std::cout << temp->artist
<< ", " << temp->title << ", " <<
temp->length << ", ";
if (temp->genre ==
genre_t::ROCK)
std::cout
<< "Rock\n";
else if (temp->genre ==
genre_t::COUNTRY)
std::cout
<< "Country\n";
else if (temp->genre ==
genre_t::POP)
std::cout
<< "Pop\n";
else if (temp->genre ==
genre_t::CLASSICAL)
std::cout
<< "Classical\n";
else
std::cout
<< "Polka\n";
temp = temp->next;
}
}
//Assignment operator overload
void playlist::operator=(playlist& p) {
if (p.head == NULL)
head = NULL;
else
{
song* temp = head;
while (p.head != NULL) {
temp =
p.head;
p.head =
p.head->next;
temp =
temp->next;
}
}
}
Output
3 Linda Ronstadt You're no good 2.30 Rock Elton John Rocket Man 4.41 Rock Antonin Leopold Dvorak Songs my mother taught me 2.24 Classical Antonin Leopold Dvorak, Songs my mother taught me, 2.24, Classical Elton John, Rocket Man, 4.41, Rock Linda Ronstadt, You're no good, 2.3, Rock
---------------------------------------------------