In: Computer Science
Create a class that will store all the information needed for a song. Your class will store the following data:
The class will have the following methods:
Your program should have a main that creates three song objects, places them into a vector and uses a for loop to "play" each of the songs using your play method.
Answer:-
Hello, I have written the required code in CPP. Please find it below.
CPP CODE:-
#include <iostream>
#include <string>
#include <vector>
using namespace std;
/* song class starts here*/
class song
{
/* Data members of song class*/
string title;
string artist;
string album;
string genre;
bool liked;
public:
/* Constructors for song class*/
song(string title, string artist,
string album)
{
this->title=title;
this->artist=artist;
this->album=album;
}
song(string title,string artist)
{
this->title=title;
this->artist=artist;
}
/* Implementation of to_string
function*/
string to_string()
{
return "Artist Name = "+artist+"
Album Name = "+album;
}
/* Implementation of play function*/
void play()
{
cout<<"Song Title = "<<title<<" Artist =
"<<artist<<" Album Name =
"<<album<<endl;
}
/* getters and setters*/
void set_liked(bool
song_vote)
{
liked=song_vote;
}
bool get_liked()
{
return
liked;
}
string get_artist()
{
return
artist;
}
void set_artist(string name)
{
artist=name;
}
string
get_title()
{
return
title;
}
void set_title(string title)
{
this->title=title;
}
string
get_album()
{
return
album;
}
void set_album(string album)
{
this->album=album;
}
string
get_genre()
{
return
genre;
}
void set_genre(string genre)
{
this->genre=genre;
}
};
/* main function starts here*/
int main()
{
vector < song > a ;
/* Declaration of 3 song objects*/
song song1("AAA","Artist1","Album1");
song song2("BBB","Artist2","Album2");
song song3("CCC","Artist2","Album3");
a.push_back(song1);
a.push_back(song2);
a.push_back(song3);
/* Using for loop for calling play() function for all
songs*/
for(int i=0; i < a.size(); i++)
a.at(i).play() ;
}
Screenshot of the code:-
Please refer to the screenshot of the code for proper understanding the indentation of the code.
Screenshot 1:-
Screenshot 2:-
Screenshot 3:-
OUTPUT:-
I hope, it would help.
Thanks!