Programming Problem: Design, implement, and test a class that represents a song
ID: 3574443 • Letter: P
Question
Programming Problem:
Design, implement, and test a class that represents a song on a CD or in an MP3 library. If you did Programming Warm-Up Exercises 4 through 6, you already have a good start. 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 calledCategory; make up an enumeration of your favorite categories. The class should have a constructor 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.
Explanation / Answer
#include <windows.h>
#include <commctrl.h>
#include <stdio.h>
#include <iostream>
#include <string>
#include <cstring>
#include <sstream>
using namespace std;
enum Category { Rock, Theme, Rap, Lyricist};
class Song{
public:
string title;
string artist;
string album;
Category category;
double length;
Song(){
title="";
artist="";
album="";
length=0.0;
}
Song(string title, string artist, string album, Category category, double length){
this->title = title;
this->artist = artist;
this->album = album;
this->category = category;
this->length = length;
}
string getTitle() {
return title;
}
void setTitle(string title) {
this->title = title;
}
string getArtist() {
return artist;
}
void setArtist(string artist) {
this->artist = artist;
}
string getAlbum() {
return album;
}
void setAlbum(string album) {
this->album = album;
}
double getLength() {
return length;
}
void setLength(double length) {
this->length = length;
}
Category getCategory() {
return category;
}
void setCategory(Category category) {
this->category = category;
}
void songInfo(){
cout<<" Song [title=" << title << ", artist=" << artist << ", album="
<< album << ", category=" << category << ", length=" << length << "]";
}
int compareSong(Song obj) {
if (this->album.compare(obj.album) != 0)
return 0;
else if (this->artist.compare(obj.artist) != 0)
return 0;
else if (this->title.compare(obj.title) != 0)
return 0;
else if (this->category != category)
return 0;
else if (this->length != length)
return 0;
return 1;
}
};
int main()
{
Category s = Rock;
Song song();
Song song2("Bears Songs", "Bears1", "Bears1", s, 3.2);
Song song3("Bears Songs", "Bears1", "Bears1", s, 3.2);
song2.songInfo();
song3.songInfo();
if(song2.compareSong(song3) == 0)
cout << " song2 == song3 : false";
else
cout << " song2 == song3 : true";
return 0;
}
OUTPUT:
Song [title=Bears Songs, artist=Bears1, album=Bears1, category=0, length=3.2]
Song [title=Bears Songs, artist=Bears1, album=Bears1, category=0, length=3.2]
song2 == song3 : true
Process returned 0 (0x0) execution time : 0.013 s
Press any key to continue.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.