Question

In: Computer Science

C++ MAIN remains same Given the complete main() function, partial playlist class header playlist.h, and playlist.cpp,...

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:

  • constructor
  • copy constructor
  • destructor
  • addSong(song tune)
    • adds a single node to the front of the linked list
    • no return value
  • displayList()
    • displays the linked list as formatted in the example below
    • no return value
  • overloaded assignment operator

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

Solutions

Expert Solution

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

---------------------------------------------------


Related Solutions

Complete the provided partial C++ Linked List program. Main.cpp is given and Link list header file...
Complete the provided partial C++ Linked List program. Main.cpp is given and Link list header file is also given. The given testfile listmain.cpp is given for demonstration of unsorted list functionality. The functions header file is also given. Complete the functions of the header file linked_list.h below. ========================================================= // listmain.cpp #include "Linked_List.h" int main(int argc, char **argv) {      float           f;      Linked_List *theList;      cout << "Simple List Demonstration\n";      cout << "(List implemented as an Array - Do...
**** IN C++ ***** 1.Given the class alpha and the main function, modify the class alpha...
**** IN C++ ***** 1.Given the class alpha and the main function, modify the class alpha so the main function is working properly. #include <iostream> using namespace std; //////////////////////////////////////////////////////////////// class alpha { private: int data; public: //YOUR CODE }; //////////////////////////////////////////////////////////////// int main() { alpha a1(37); alpha a2; a2 = a1; cout << "\na2="; a2.display(); //display a2 alpha a3(a1); //invoke copy constructor cout << "\na3="; a3.display(); //display a3 alpha a4 = a1; cout << "\na4="; a4.display(); cout << endl; return 0;...
Objective You are given a partial implementation of one header file, GildedRose.h. Item is a class...
Objective You are given a partial implementation of one header file, GildedRose.h. Item is a class that holds the information for each item for the inn. GildedRose is a class that holds an internal listing of many Item objects. This inventory should hold at least 10 items. For this you can use arrays, the std::array class, or even the vector class. Complete the implementation of these classes, adding public/private member variables and functions as needed. You should choose an appropriate...
Complete the following task in C++. Separate your class into header and cpp files. You can...
Complete the following task in C++. Separate your class into header and cpp files. You can only useiostream, string and sstream. Create a main.cpp file to test your code thoroughly. Given : commodity.h and commodity.cpp here -> https://www.chegg.com/homework-help/questions-and-answers/31-commodity-request-presented-two-diagrams-depicting-commodity-request-classes-form-basic-q39578118?trackid=uF_YZqoK Create : locomotive.h, locomotive.cpp, main.cpp Locomotive Class Hierarchy Presented here will be a class diagram depicting the nature of the class hierarchy formed between a parent locomotive class and its children, the two kinds of specic trains operated. The relationship is a...
Complete the following task in C++. Separate your class into header and cpp files. You can...
Complete the following task in C++. Separate your class into header and cpp files. You can only use iostream, string and sstream. Create a main.cpp file to test your code thoroughly. Given : commodity.h and commodity.cpp here -> https://www.chegg.com/homework-help/questions-and-answers/31-commodity-request-presented-two-diagrams-depicting-commodity-request-classes-form-basic-q39578118?trackid=uF_YZqoK Given : locomotive.h, locomotive.cpp, main.cpp -> https://www.chegg.com/homework-help/questions-and-answers/complete-following-task-c--separate-class-header-cpp-files-useiostream-string-sstream-crea-q39733428 Create : DieselElectric.cpp DieselElectric.h DieselElectric dieselElectric -fuelSupply:int --------------------------- +getSupply():int +setSupply(s:int):void +dieselElectric(f:int) +~dieselElectric() +generateID():string +calculateRange():double The class variables are as follows: fuelSuppply: The fuel supply of the train in terms of a number of kilolitres...
A header file contains a class template, and in that class there is a C++ string...
A header file contains a class template, and in that class there is a C++ string object. Group of answer choices(Pick one) 1)There should be a #include for the string library AND a using namespace std; in the header file. 2)There should be a #include for the string library. 3)There should be a #include for the string library AND a using namespace std; in the main program's CPP file, written before the H file's include.
please complete the header file that contains a class template for ADT Queue and complete all...
please complete the header file that contains a class template for ADT Queue and complete all the member functions in the class template. Submit the header file only, but please write a source file that tests all the member functions to make sure they are working correctly. queue.h #ifndef _QUEUE #define _QUEUE #include"Node.h" template<class ItemType> class Queue { private:    Node<ItemType> *backPtr;    Node<ItemType> *frontPtr; public:    Queue(); //Default constructor    Queue(const Queue<ItemType> &aQueue);    bool isEmpty() const;    bool...
The question is about C++ The main should test all functionalities of the playlist implement a...
The question is about C++ The main should test all functionalities of the playlist implement a linked list to store songs of a playlist. A song (which is your node) is going to include the following data: - int Song ID - string Song Title - string Author Title - double Length Don't forget to include the next pointer that will lead to the next song. Your playlist (which is your linkedlist) is going to include the following functionalities: -...
Write a complete C++ program that at least consists of the main() function and at least...
Write a complete C++ program that at least consists of the main() function and at least two recursive functions. The first function has no return value and can be named printPrime(). It prints first n prime numbers with proper prompt. Note that number 1 is not regarded as a prime number. We assume the first prime number is 2. The printout should start from 2. The prototype of the recursive function should be void printPrime(int n); The algorithm of printPrime()...
**** IN C++ **** 1) Modify the class pointerDataClass so the main function below is working...
**** IN C++ **** 1) Modify the class pointerDataClass so the main function below is working properly. Use deep copy. int main() { pointerDataClass list1(10); list1.insertAt(0, 50); list1.insertAt(4, 30); list1.insertAt(8, 60); cout<<"List1: " < list1.displayData(); cout<<"List 2: "< pointerDataClass list2(list1); list2.displayData(); list1.insertAt(4,100); cout<<"List1: (after insert 100 at indext 4) " < list1.displayData(); cout<<"List 2: "< list2.displayData(); return 0; } Code: #include using namespace std; class pointerDataClass { int maxSize; int length; int *p; public: pointerDataClass(int size); ~pointerDataClass(); void insertAt(int index,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT