Design, implement, and test a class that represents a song on a CD or in an MP3
ID: 3873469 • Letter: D
Question
Design, implement, and test a class that represents a song on a CD or in an MP3 library. It should have members for title, album, artist, playing time in minutes and seconds, and music category. The music category is represented by an enumeration type called Category; make up an enumeration of your favorite categories. The class should have a contructor that allows all of the data members to be set, and a default constructor that sets all of the data members to appropriate empty values. It should have an observer operation for each data member, and an observer that returns all of the data for the song as a string. An observer that compares two songs for equality should be developed as well. C++ language
Explanation / Answer
Given below is the class and test program for the question. Please rate the answer if it helped. Thank you.
#include <iostream>
using namespace std;
struct Time
{
int minutes;
int seconds;
};
enum Category
{
POP, HIPHOP, JAZZ, DANCE, FOLK
};
class Song
{
private:
string title;
string album;
string artist;
struct Time playingTime; //the time is represented by stuct Time
Category category; //category will be one of the types defined in the enum Category
public:
//default constructor
Song()
{
title = "";
album = "";
artist = "";
playingTime = {0,0};
category = POP;
}
Song(string tit, string alb, string art, struct Time ptime, Category cat)
{
title = tit;
album = alb;
artist = art;
playingTime = ptime;
category = cat;
}
//observers for all data members
string getTitle()
{
return title;
}
string getAlbum()
{
return album;
}
string getArtist()
{
return artist;
}
struct Time getPlayingTime()
{
return playingTime;
}
Category getCategory()
{
return category;
}
//observer to compare 2 songs. The albums are compared first. If albums are same then titles are compared.
int compareTo(const Song &other)
{
if(album < other.album)
return -1;
else if(album > other.album)
return 1;
else
{
if(title < other.title)
return -1;
else if(title > other.title)
return 1;
else
return 0;//both album and title are same
}
}
//return all data members as a string
string toString()
{
string str = " Title: " + title + " " +
"Album: " + album + " " +
"Artist: " + artist + " " +
"Playing Time: " + to_string(playingTime.minutes) + ":" + to_string(playingTime.seconds) + " " +
"Category: ";
switch(category)
{
case POP: str += "Pop";
break;
case HIPHOP: str += "HipHop";
break;
case JAZZ: str += "Jazz" ;
break;
case FOLK: str += "Folk";
break;
case DANCE: str += "Dance";
break;
}
str += " ";
return str;
}
};
int main()
{
Song s1; //default constructor;
Song s2 = Song("Baby be mine", "Thriller", "Michael Jackson", {2,40}, POP); //pass playing time struct fields as {2, 40}
Song s3 = Song("On The Floor", "Love?", "Jennifer Lopez", {3, 51}, DANCE);
cout << "Song s1 using default constructor " << s1.toString() ;
cout << "Song s2 using parameterized constructor " << s2.toString() << endl;
cout << "Displaying s3 using observers " << endl;
cout << s3.getTitle() << endl;
cout << s3.getAlbum() << endl;
cout << s3.getArtist() << endl;
struct Time time = s3.getPlayingTime();
cout << time.minutes << ":" << time.seconds << endl;
}
output
Song s1 using default constructor
Title:
Album:
Artist:
Playing Time: 0:0
Category: Pop
Song s2 using parameterized constructor
Title: Baby be mine
Album: Thriller
Artist: Michael Jackson
Playing Time: 2:40
Category: Pop
Displaying s3 using observers
On The Floor
Love?
Jennifer Lopez
3:51
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.