Question

In: Computer Science

I need C++ code Given the complete main() function, partial playlist class header playlist.h, and playlist.cpp,...

I need C++ code

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
  • Accessors- search(), delSong()
  • The insertion operator

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

Edit main to demonstrate all implemented functions!

_______________________________________________

#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.cpp

#include <iostream>
#include "playlist.h"
using namespace std;

//copy constructor
playlist::playlist(const playlist&other)
{
head = nullptr;
song *temp = other.head, *p;

while(temp!=nullptr)
{
song *newsong = new song;
newsong->artist = temp->artist;
newsong->title = temp->title;
newsong->length = temp->length;
newsong->genre = temp->genre;

if(head == nullptr)
{
head = newsong;
head->next = nullptr;
p = head;
}
else
{
p->next = newsong;
p = p->next;
}

temp = temp->next;
}
p->next = nullptr;
}

//destructor
playlist::~playlist()
{
song *temp = head;
while(temp!=nullptr)
{
temp = temp->next;
delete head;
head = temp;
}
}

//adds a single node to the front of the linked list
void playlist::addSong(song tune)
{
song *newsong = new song;
newsong->artist = tune.artist;
newsong->title = tune.title;
newsong->length = tune.length;
newsong->genre = tune.genre;

if(head == nullptr)
{
head = newsong;
head->next = nullptr;
}
else
{
newsong->next = head;
head = newsong;
}
}


//displays the linked list
void playlist::displayList()
{
song *temp = head;
while(temp!=nullptr)
{
cout<<temp->artist<<", "<<temp->title<<", "<<temp->length<<", ";
if(temp->genre==genre_t::ROCK)
cout<<"ROCK"<<endl;
else if(temp->genre==genre_t::COUNTRY)
cout<<"COUNTRY"<<endl;
else if(temp->genre==genre_t::POP)
cout<<"POP"<<endl;
else if(temp->genre==genre_t::CLASSICAL)
cout<<"CLASSICAL"<<endl;
else if(temp->genre==genre_t::POLKA)
cout<<"POLKA"<<endl;

temp = temp->next;
}
}

//overloaded assignment operator
playlist& playlist::operator=(const playlist&other)
{
head = nullptr;
song *temp = other.head, *p;

while(temp!=nullptr)
{
song *newsong = new song;
newsong->artist = temp->artist;
newsong->title = temp->title;
newsong->length = temp->length;
newsong->genre = temp->genre;

if(head == nullptr)
{
head = newsong;
head->next = nullptr;
p = head;
}
else
{
p->next = newsong;
p = p->next;
}

temp = temp->next;
}
p->next = nullptr;

return *this;
}

//search a song
void playlist::search(string title)
{
song *temp = head;
while(temp!=nullptr)
{
if(temp->title==title)
{
cout<<temp->artist<<", "<<temp->title<<", "<<temp->length<<endl;
return;
}
temp = temp->next;
}
cout<<"Not found"<<endl;
}

//delete a song
void playlist::delSong(string title)
{
song *temp = head, *p = nullptr;
while(temp!=nullptr && temp->title!=title)
{
p = temp;
temp = temp->next;
}
if(temp->title==title)
{
p->next = temp->next;
delete temp;
}
}

//insertion operator overloading
ostream& operator<<(ostream&out, const playlist&plist)
{
song *temp = plist.head;
while(temp!=nullptr)
{
out<<temp->artist<<", "<<temp->title<<", "<<temp->length<<", ";
if(temp->genre==genre_t::ROCK)
cout<<"ROCK"<<endl;
else if(temp->genre==genre_t::COUNTRY)
cout<<"COUNTRY"<<endl;
else if(temp->genre==genre_t::POP)
cout<<"POP"<<endl;
else if(temp->genre==genre_t::CLASSICAL)
cout<<"CLASSICAL"<<endl;
else if(temp->genre==genre_t::POLKA)
cout<<"POLKA"<<endl;

temp = temp->next;
}
return out;
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

//playlist.h

#ifndef PLAYLIST_H
#define PLAYLIST_H

#include <iostream>
#include <string>
using namespace std;

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:
//constructor
playlist():head(nullptr){}
//copy constructor
playlist(const playlist&other);
//destructor
~playlist();
//adds a single node to the front of the linked list
void addSong(song tune);
//displays the linked list
void displayList();
//overloaded assignment operator
playlist& operator=(const playlist&other);
//search a song
void search(string title);
//delete a song
void delSong(string title);
//insertion operator overloading
friend ostream& operator<<(ostream&, const playlist&);

private:
song* head;
};

#endif

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

//main.cpp

#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();

cout<<"\nDemonstrate copy constructor:- "<<endl;
playlist p2(mySongs);
p2.displayList();

cout<<"\nDemonstrate assignment and insertion operator overloading :- "<<endl;
playlist p3(mySongs);
cout<<p3;

cout<<"\nDemonstrate searching and delete song :- "<<endl;
p2.search("You're no good");
p2.delSong("You're no good");
cout<<p2;


return 0;
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

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

Demonstrate copy constructor:-
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

Demonstrate assignment and insertion operator overloading :-
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

Demonstrate searching and delete song :-
Linda Ronstadt, You're no good, 2.3
Antonin Leopold Dvorak, Songs my mother taught me, 2.24, CLASSICAL
Elton John, Rocket Man, 4.41, ROCK

N.B  Whether you face any problem or need any modification then share with me in the comment section, I'll happy to help you.


Related Solutions

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...
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;...
I need to complete this C++ program. The instructions are in the comments inside the code...
I need to complete this C++ program. The instructions are in the comments inside the code below: ------------------------------------------------------------------------- Original string is: this is a secret! Encypted string is: uijt!jt!b!tfdsfu" Decrypted string is: this is a secret! //Encoding program //Pre-_____? //other necessary stuff here int main() { //create a string to encrypt using a char array cout<< "Original string is: "<<string<<endl; encrypt(string); cout<< "Encrypted string is: "<<string<<endl; decrypt(string); cout<<"Decrypted string is: "<<string<<endl; return 0; } void encrypt(char e[]) { //Write implementation...
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...
C++ Code (I just need the dieselLocomotive Class) Vehicle Class The vehicle class is the parent...
C++ Code (I just need the dieselLocomotive Class) Vehicle Class The vehicle class is the parent class of the derived class: dieselLocomotive. Their inheritance will be public inheritance so reflect that appropriately in their .h files. The description of the vehicle class is given in the simple UML diagram below: vehicle -map: char** -name: string -size:int -------------------------- +vehicle() +getSize():int +setName(s:string):void +getName():string +getMap():char** +setMap(s: string):void +getMapAt(x:int, y:int):char +~vehicle() +operator--():void +determineRouteStatistics()=0:void The class variables are as follows: map: A 2D array of...
C++ Recursion, Change the delete_node function in the header file to a recursive delete_node function, main...
C++ Recursion, Change the delete_node function in the header file to a recursive delete_node function, main is already set. Hint: It might be helpful to modify the function so that it uses a separate recursive function to perform whatever processing is needed. //////////////////////////////////////////////////////////////header.h////////////////////////////////////////////////////////////////////////////////////////////// #ifndef HEADER_H_ #define HEADER_H_ #include <iostream> using namespace std; template <class T> class LL { private:    struct LLnode    {        LLnode* fwdPtr;        T theData;    };    LLnode* head; public:    LL();...
I need this Java code transform to Python Code PROGRAM: import java.util.Scanner; public class Main {...
I need this Java code transform to Python Code PROGRAM: import java.util.Scanner; public class Main { static int count=0; int calculate(int row, int column) { count++; if(row==1&&column==1) { return 0; } else if(column==1) { return ((200+calculate(row-1,column))/2); } else if(column==row) { return (200+calculate(row-1,column-1))/2; } else { return ((200+calculate(row-1,column-1))/2)+((200+calculate(row-1,column))/2); }    } public static void main(String[] args) { int row,column,weight; Main m=new Main(); System.out.println("Welcome to the Human pyramid. Select a row column combination and i will tell you how much weight the...
I need this Java code translated into C Code. Thank you. //Logical is the main public...
I need this Java code translated into C Code. Thank you. //Logical is the main public class public class Logical { public static void main(String args[]) { char [][] table= {{'F','F','F'},{'F','F','T'},{'F','T','F'},{'F','T','T'},{'T','F','F'},{'T','F','T'},{'T','T','F'},{'T','T','T'}}; // table contains total combinations of p,q,& r int totalTrue, totalFalse, proposition;    //proposition 1: proposition=1; start(proposition); totalTrue=0; totalFalse=0; for(int i=0;i<8;i++) {    { char o= conjuctive(implecation(negation(table[i][0]),table[i][1]),implecation(table[i][2],table[i][0])); System.out.println(" "+table[i][0]+" "+table[i][1]+" "+table[i][2]+" "+o); if(o=='T') totalTrue++; else totalFalse++;    } } finalOutput(totalTrue,totalFalse,proposition); System.out.println(" "); System.out.println(" ");    //proposition 2: proposition=2; start(proposition);...
I need a java flowchart diagram for the following code: import java.util.*; public class Main {...
I need a java flowchart diagram for the following code: import java.util.*; public class Main {    public static void main(String[] args) {    Scanner sc=new Scanner(System.in);           System.out.print("Enter the input size: ");        int n=sc.nextInt();        int arr[]=new int[n];        System.out.print("Enter the sequence: ");        for(int i=0;i<n;i++)        arr[i]=sc.nextInt();        if(isConsecutiveFour(arr))        {        System.out.print("yes the array contain consecutive number:");        for(int i=0;i<n;i++)        System.out.print(arr[i]+" ");   ...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT