The following program has to be done in multifile. I have written out all the pr
ID: 3747775 • Letter: T
Question
The following program has to be done in multifile. I have written out all the program from my side, however, I am receiving errors. So please modify my code so that it works and please do tell me what you did to fix my code. However please note that all of it must be in separate implementation and interface file. Thank you.
The following are the given instructions for the program project.
Design a simple PlayList Program
Class Set You are given an interface: SetInterface.h (it does not allow duplicates)
This is a public interface, and completely specifies what the Set class operations must be.
SetInterface is an abstract class (it has no implementation), so your Set class must inherit from SetInterface and implement all of its methods.
Here is the SetInterface:
__________________________________________________________________________________
#ifndef SET_INTERFACE_H_
#define SET_INTERFACE_H_
#include <vector>
template<class ItemType>
class SetInterface
{
public:
/** Gets the current number of entries in this set.
@return The integer number of entries currently in the set. */
virtual int getCurrentSize() const = 0;
/** Checks whether this set is empty.
@return True if the set is empty, or false if not. */
virtual bool isEmpty() const = 0;
/** Adds a new entry to this set.
@post If successful, newEntry is stored in the set and
the count of items in the set has increased by 1.
@param newEntry The object to be added as a new entry.
@return True if addition was successful, or false if not. */
virtual bool add(const ItemType& newEntry) = 0;
/** Removes a given entry from this set,if possible.
@post If successful, anEntry has been removed from the set
and the count of items in the set has decreased by 1.
@param anEntry The entry to be removed.
@return True if removal was successful, or false if not. */
virtual bool remove(const ItemType& anEntry) = 0;
/** Removes all entries from this set.
@post set contains no items, and the count of items is 0. */
virtual void clear() = 0;
/** Tests whether this set contains a given entry.
@param anEntry The entry to locate.
@return True if set contains anEntry, or false otherwise. */
virtual bool contains(const ItemType& anEntry) const = 0;
/** Fills a vector with all entries that are in this set.
@return A vector containing all the entries in the set. */
virtual std::vector<ItemType> toVector() const = 0;
}; // end SetfInterface
#endif /* SET_INTERFACE_H_ */
______________________________________________________________________________________________________
Other than the methods specified by the SetInterface, your Set class will also have some private data members:
static const int DEFAULT_SET_SIZE = 4; // for testing purposes we will keep the set small
ItemType items_[DEFAULT_SET_SIZE]; // array of set items
int item_count_; // current count of set items
int max_items_; // max capacity of the set
Set also has a private member function (a helper function) other than the public methods specified by SetInterface.h
It is used by some of its' public methods to find out where a particular item is
// post: Either returns the index of target in the array items_
// or -1 if the array does not contain the target
int getIndexOf(const ItemType& target) const;
Remember, Set is an Abstract Data Type!
So Set is templated and can store an arbitrary ItemType.
Class Song
You will also code your own Song class, so you can add Songs to your playlist. A Song stores the following information (private member variables):
std::string title_;
std::string author_;
std::string album_;
And the following operations (public member functions)
Song();
Song(const std::string& title, const std::string& author = "", const std::string& album = "");
void setTitle(std::string title); //"set" in setTitle here means "give a value" and has nothing
// to do with the Set class. Similarly for setAuthor and setAlbum
void setAuthor(std::string author);
void setAlbum(std::string album);
std::string getTitle() const;
std::string getAuthor() const;
std::string getAlbum() const;
friend bool operator==(const Song& lhs, const Song& rhs);
Since you are the best programmer in town I trust you will do a great job at commenting these thoroughly!!!
Class PlayList
Your PlayList class will have a single data member (private member variable), a Set object called playlist_ which will store your Songs:
Set<Song> playlist_;
Your PlayList will have the following operations (public member functions)
PlayList();
PlayList(const Song& a_song);
int getNumberOfSongs() const;
bool isEmpty() const;
bool addSong(const Song& new_song);
bool removeSong(const Song& a_song);
void clearPlayList();
void displayPlayList() const;
You will notice that the Set ADT did serve you well. Most of the things you want to do to your PlayList are functionalities your Set ADT provides, so all of these functions will essentially be making calls to the corresponding Set public members... good design goes a long way!!!
Once again, since you are the best programmer in town I trust you will do a great job at commenting these thoroughly!!!
A few things to notice:
The parameterized constructor needs to add a_song to playlist_
Depending on your design/implementation, when removing an item, Set may or may not have preserved an order for the remaining items because in a Set THERE IS NO ORDER. In a PlayList, on the other hand, you may like to have Songs play in a certain order. Don't worry about that for now, although it may not be the most desirable behavior, you can simply remove a Song from the PlayList by calling the Set::remove() member function no matter how you implemented it. We will take care of this in Project 3.
displayPlayList() will take advantage of Set::toVector() to access and display to the console (cout) data for all the songs in playlist_ . For each song it will display: * Title: title * Author: author * Album: album * on a new line, where the text in red should be replaced by the value of the corresponding Song member variable. After displaying data for all the songs it will display: End of playlist on a new line.
Usage:
Make sure your code compiles and runs correctly with this main() function:
int main() {
//**********Test Song************//
//instantiate 5 songs
Song song1;
song1.setTitle("title 1");
song1.setAuthor("author 1");
song1.setAlbum("album 1");
Song song2("title 2", "author 2", "album 2");
Song song3("title 3", "author 3", "album 3");
Song song4("title 4", "author 4", "album 4");
Song song5("title 5", "author 5", "album 5");
//output song information
cout << "The first song is: " << song1.getTitle() << ", " << song1.getAuthor() << ", " << song1.getAlbum() << endl;
cout << "The second song is: " << song2.getTitle() << ", " << song2.getAuthor() << ", " << song2.getAlbum() << endl;
cout << "The third song is: " << song3.getTitle() << ", " << song3.getAuthor() << ", " << song3.getAlbum() << endl;
cout << "The fourth song is: " << song4.getTitle() << ", " << song4.getAuthor() << ", " << song4.getAlbum() << endl << endl;
//************* Test PlayList*************//
//instantiate PlayList and add Songs to it
PlayList myPlayList(song1);
myPlayList.addSong(song2);
myPlayList.addSong(song3);
myPlayList.displayPlayList();
cout << "Playlist now holds " << myPlayList.getNumberOfSongs() << " songs ";
myPlayList.addSong(song1);
myPlayList.displayPlayList();
cout << endl;
myPlayList.addSong(song4);
myPlayList.displayPlayList();
cout << endl;
myPlayList.addSong(song5);
myPlayList.displayPlayList();
cout << endl;
myPlayList.removeSong(song2);
myPlayList.displayPlayList();
cout << endl;
myPlayList.removeSong(song3);
myPlayList.displayPlayList();
cout << endl;
myPlayList.clearPlayList();
myPlayList.displayPlayList();
cout << myPlayList.isEmpty() << endl;
return 0;
}
The task:
For this project you are given the full class interface SetInterface.h that your Set class will inherit from. You are also given as a sample usage main() function. This is the function that will be used to test your code. You need towrite and submit 3 classes (6 files: Set.h, Set.cpp, Song.h, Song.cpp, PlayList.h, PlayList.cpp)
Here is my program (all the six files). It is not working properly. so please as with the instructions please fix my code so it can output with the given main function.
Set.h is following
#ifndef SET_H_
#define SET_H_
#include "SetInterface.h"
template<class ItemType>
class Set: public SetInterface<ItemType>{
private:
static const int DEFAULT_SET_SIZE = 4; // for testing purposes we will keep the set small
ItemType items_[DEFAULT_SET_SIZE]; // array of set items
int item_count_; // current count of set items
int max_items_;
int getIndexOf(const ItemType& target) const;
public:
Set();
int getCurrentSize() const; // {
bool isEmpty() const; // {
bool add(const ItemType& newEntry);
bool remove(const ItemType& anEntry);
void clear();
bool contains(const ItemType& anEntry) const;
std::vector<ItemType> toVector() const;
};
#include "Set.cpp"
#endif //comment
Set.cpp
#include "Set.h"
#include <cstddef>
template<class ItemType>
Set<ItemType>::Set(): item_count_(0), max_items_(DEFAULT_SET_SIZE)
{
} // end default constructor
template<class ItemType>
int Set<ItemType>::getCurrentSize() const {
return item_count_;
}
template<class ItemType>
bool Set<ItemType>::isEmpty() const{
return !item_count_==0;
}
template<class ItemType>
bool Set<ItemType>::add(const ItemType& newEntry){
bool has_room_to_add = (item_count_ < max_items_);
if (has_room_to_add){
items_[item_count_] = newEntry;
item_count_++;
} // end if
return has_room_to_add;
} // end add
template<class ItemType>
bool Set<ItemType>::remove(const ItemType& anEntry){
int located_index = getIndexOf(anEntry);
bool can_remove_item = !isEmpty() && (located_index > -1);
if (can_remove_item){
item_count_--;
items_[located_index] = items_[item_count_];
} // end if
return can_remove_item;
} // end remove
template<class ItemType>
void Set<ItemType>::clear(){
item_count_ = 0;
} // end clear
template<class ItemType>
bool Set<ItemType>::contains(const ItemType& anEntry) const
{
return getIndexOf(anEntry) > -1;
} // end contains
template <class ItemType>
std::vector<ItemType> toVector() const{
std::vector<ItemType> bag_contents;
for (int i = 0; i < item_count_; i++)
bag_contents.push_back(items_[i]);
return bag_contents;
} // end toVector
// private
template<class ItemType>
int Set<ItemType>::getIndexOf(const ItemType& target) const
{
bool found = false;
int result = -1;
int search_index = 0;
// If the bag is empty, item_count_ is zero, so loop is skipped
while (!found && (search_index < item_count_))
{
if (items_[search_index] == target)
{
found = true;
result = search_index;
}
else
{
search_index++;
} // end if
} // end while
return result;
} // end getIndexOf
Song.cpp
#include "Song.h"
#include <cstddef>
Song::Song():author_(""), album_(""){
}
Song::Song(const string& title, const string& author, const string& album){
title_ = title;
author_ = author;
album_ = album;
}
void Song::setTitle(string title){ //"set" in setTitle here means "give a value" and has nothing
title_ = title ;
}
void Song::setAuthor(string author){
author_ = author;
}
void Song::setAlbum(string album){
album_ = album;
}
string Song::getTitle() const{
return title_;
}
string Song::getAuthor() const{
return author_;
}
string Song::getAlbum() const{
return album_;
}
bool operator==(const Song& lhs, const Song& rhs){
if(lhs.getTitle()==rhs.getTitle() && lhs.getAlbum()==rhs.getAlbum() && lhs.getAuthor()==rhs.getAuthor()){
return true;
}
return false;
}
Song.h
#ifndef SONG_H_
#define SONG_H_
#include <iostream>
#include "SetInterface.h"
using namespace std;
class Song{
private:
string title_;
string author_;
string album_;
public:
Song();
Song(const string& title, const string& author = "", const string& album = "");
void setTitle(string title); //"set" in setTitle here means "give a value" and has nothing
// to do with the Set class. Similarly for setAuthor and setAlbum
void setAuthor(string author);
void setAlbum(string album);
string getTitle() const;
string getAuthor() const;
string getAlbum() const;
friend bool operator==(const Song& lhs, const Song& rhs);
bool operator< (const Song& x) const;
};
#include "Song.cpp"
#endif //comment
PlayList.h
#ifndef PLAY_LIST_
#define PLAY_LIST_
#include <iostream>
#include "Set.h"
class PlayList:Set{
private:
Set<Song> playlist_;
public:
PlayList();
PlayList(const Song& a_song);
int getNumberOfSongs() const;
bool isEmpty() const;
bool addSong(const Song& new_song);
bool removeSong(const Song& a_song);
void clearPlayList();
void displayPlayList() const;
};
#include "PlayList.cpp"
#endif //comment
PlayList.cpp
#include "PlayList.h"
#include <cstddef>
PlayList::PlayList(){
}
PlayList::PlayList(const Song& a_song){
playlist_.insert(a_song);
}
int PlayList::getNumberOfSongs() const{
return playlist_.size();
}
bool PlayList::isEmpty() const{
return (playlist_.size() == 0);
}
bool PlayList::addSong(const Song& new_song){
playlist_.insert(new_song);
return true;
}
bool PlayList::removeSong(const Song& a_song){
playlist_.erase(a_song);
return true;
}
void PlayList::clearPlayList(){
playlist_.clear();
}
void PlayList::displayPlayList() const{
cout << "Playlist contains:" <<endl;
for (set<Song>::iterator it=playlist_.begin(); it!=playlist_.end(); ++it)
cout << "* Title" << (*it).getTitle() << " * Author" << (*it).getAuthor() << " * Album" << (*it).getAlbum() << " *" <<endl;
cout << "End of playlist" <<endl;
}
Explanation / Answer
Here are a few things that you need to keep in mind:
Now, coming to your question, there were many #includes<> missing in files like #include <vector> in SetInterface.h. There were many cyclic #includes<> - File A includes File B which in turn includes C and File C includes File A, this forms a cycle of include statements. We solve these issues by carefully removing include statements or by using forward-declaration.
The program compiles and works on my machine now using C++11 compiler. I'm pasting the modified code below.
<code> PlayList.h
#ifndef PLAY_LIST_
#define PLAY_LIST_
#include <iostream>
#include "Song.h"
#include "SetInterface.h"
class PlayList: public Set{
private:
Set<Song> playlist_;
public:
PlayList();
PlayList(const Song& a_song);
int getNumberOfSongs() const;
bool isEmpty() const;
bool addSong(const Song& new_song);
bool removeSong(const Song& a_song);
void clearPlayList();
void displayPlayList() const;
};
#endif //comment
</code>
<code> PlayList.cpp
#include "SetInterface.h"
#include "PlayList.h"
#include <cstddef>
#include <iostream>
PlayList::PlayList(){
}
PlayList::PlayList(const Song& a_song){
playlist_.add(a_song);
}
int PlayList::getNumberOfSongs() const{
return playlist_.getCurrentSize();
}
bool PlayList::isEmpty() const{
return (playlist_.isEmpty() == 0);
}
bool PlayList::addSong(const Song& new_song){
playlist_.add(new_song);
return true;
}
bool PlayList::removeSong(const Song& a_song){
playlist_.remove(a_song);
return true;
}
void PlayList::clearPlayList(){
playlist_.clear();
}
void PlayList::displayPlayList() const{
cout << "Playlist contains:" <<endl;
std::vector<Song> v = playlist_.toVector();
for (vector<Song>::iterator it=v.begin(); it!=v.end(); ++it)
cout << "* Title" << (*it).getTitle() << " * Author" << (*it).getAuthor() << " * Album" << (*it).getAlbum() << " *" <<endl;
cout << "End of playlist" <<endl;
}
</code>
<code> SetInterface.h
#ifndef SET_H_
#define SET_H_
#include <vector>
template<class T>
class Set {
private:
static const int DEFAULT_SET_SIZE = 4; // for testing purposes we will keep the set small
T items_[DEFAULT_SET_SIZE]; // array of set items
int item_count_; // current count of set items
int max_items_;
int getIndexOf(const T& target) const;
public:
Set();
int getCurrentSize() const; // {
bool isEmpty() const; // {
bool add(const T& newEntry);
bool remove(const T& anEntry);
void clear();
bool contains(const T& anEntry) const;
std::vector<T> toVector() const;
};
#endif //comment
</code>
<code> Set.cpp
#include "SetInterface.h"
#include <cstddef>
#include <vector>
template<class ItemType>
Set<ItemType>::Set(): item_count_(0), max_items_(DEFAULT_SET_SIZE)
{
} // end default constructor
template<class ItemType>
int Set<ItemType>::getCurrentSize() const {
return item_count_;
}
template<class ItemType>
bool Set<ItemType>::isEmpty() const{
return !item_count_==0;
}
template<class ItemType>
bool Set<ItemType>::add(const ItemType& newEntry){
bool has_room_to_add = (item_count_ < max_items_);
if (has_room_to_add){
items_[item_count_] = newEntry;
item_count_++;
} // end if
return has_room_to_add;
} // end add
template<class ItemType>
bool Set<ItemType>::remove(const ItemType& anEntry){
int located_index = getIndexOf(anEntry);
bool can_remove_item = !isEmpty() && (located_index > -1);
if (can_remove_item){
item_count_--;
items_[located_index] = items_[item_count_];
} // end if
return can_remove_item;
} // end remove
template<class ItemType>
void Set<ItemType>::clear(){
item_count_ = 0;
} // end clear
template<class ItemType>
bool Set<ItemType>::contains(const ItemType& anEntry) const
{
return getIndexOf(anEntry) > -1;
} // end contains
template <class ItemType>
std::vector<ItemType> Set<ItemType>::toVector() const{
std::vector<ItemType> bag_contents;
for (int i = 0; i < item_count_; i++)
bag_contents.push_back(items_[i]);
return bag_contents;
} // end toVector
// private
template<class ItemType>
int Set<ItemType>::getIndexOf(const ItemType& target) const
{
bool found = false;
int result = -1;
int search_index = 0;
// If the bag is empty, item_count_ is zero, so loop is skipped
while (!found && (search_index < item_count_))
{
if (items_[search_index] == target)
{
found = true;
result = search_index;
}
else
{
search_index++;
} // end if
} // end while
return result;
} // end getIndexOf
</code>
<code> Song.h
#ifndef SONG_H_
#define SONG_H_
#include <iostream>
using namespace std;
class Song{
private:
string title_;
string author_;
string album_;
public:
Song();
Song(const string& title, const string& author = "", const string& album = "");
void setTitle(string title); //"set" in setTitle here means "give a value" and has nothing
// to do with the Set class. Similarly for setAuthor and setAlbum
void setAuthor(string author);
void setAlbum(string album);
string getTitle() const;
string getAuthor() const;
string getAlbum() const;
friend bool operator==(const Song& lhs, const Song& rhs);
bool operator< (const Song& x) const;
};
#endif //comment
</code>
<code> Song.cpp
#include "Song.h"
#include <cstddef>
Song::Song():author_(""), album_(""){
}
Song::Song(const string& title, const string& author, const string& album){
title_ = title;
author_ = author;
album_ = album;
}
void Song::setTitle(string title){ //"set" in setTitle here means "give a value" and has nothing
title_ = title ;
}
void Song::setAuthor(string author){
author_ = author;
}
void Song::setAlbum(string album){
album_ = album;
}
string Song::getTitle() const{
return title_;
}
string Song::getAuthor() const{
return author_;
}
string Song::getAlbum() const{
return album_;
}
bool operator==(const Song& lhs, const Song& rhs){
if(lhs.getTitle()==rhs.getTitle() && lhs.getAlbum()==rhs.getAlbum() && lhs.getAuthor()==rhs.getAuthor()){
return true;
}
return false;
}
</code>
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.