Question

The task is to write Song.cpp to complete the implementation of the Song class, as defined...

The task is to write Song.cpp to complete the implementation of the Song class, as defined in the provided header file, Song.h, and then to complete a program (called sales) that makes use of the class. As in Project 1, an input data file will be provided containing the song name and other information in the same format as in Program 1:

Artist    Song_Title    Year    Sales    Medium

As before, the program (sales) which are individual copies, will use this information to compute the gross revenue and the profit generated by each member using the same information as before. Again, the cost to produce a song on physical media is taken to be 75 cents while the cost for a digitally release song is 25 cents. The sale prices are $1.50 for the physical media and 99 cents for downloads. The completed sales.cpp program will perform the following tasks:

1) Prompt for the data file name. Read it, storing the information in the array of Song classes, one instance per Song. This should all be done in the readDatafile() function.

2) Output the list of song’s information. This should be done using the outputList() function prototyped in sales.cpp.

3) Sort the list alphabetically by the artist name (not the song name) using the sortAscend(). Output the list again.

4) Sort the list by profit, largest first, using the sortDescend() ‐ as in Project 1. Output the list again.

5) Test the +=  operator on the first Song in the list. Output that Song’s new information.   

Note: Do NOT make any changes to Song.h – it is not necessary!

In sales.cpp, complete the functions that are prototyped and invoke them as described above to reproduce the output seen in the provided executables.  

Note: You do NOT need to add any more functions or variables to sales.cpp.

In Song.cpp complete the following methods:

 numItemsSold - computes the total number of items sold by the student

 getGross - computes the gross revenue generated by the student

 getProfit - computes the gross profit generated by the student Note: call getGross to compute the gross revenue because it is NOT stored in the class

 operator> - compares based on the profit – again, you should use methods where defined, call getProfit!

 operator< - compares based on the artist (not song) name, i.e. an alphabetical comparison

 operator+= - +=, adds to the sales number (note this value is a float)

 operator>> o stream output – print out the Song’s information, refer to the executable (proj2) for formatting

Song.h-----------

#ifndef SONG_H
#define SONG_H

#include
using namespace std;

// struct definition for a child object
class Song {
public:

// constructor
Song(string n="", string a="", int y=0, float s=0, int m=0);

// Mutators (modifiers)
void setName(string n) { name= n; }
void setArtist(string v) { artist= v; }
void setYear(int v) { year= v; }
void setSales(float v) { sales= v; }
void setMedium(int v) { medID= v; }

// inspectors (accessors)
string getName() const { return name; }
string getArtist() const { return artist; }
string getMedium() const { return mediumTypes[medID]; }
int getYear() const { return year; }
float getSales() const { return sales; }

// methods for computing revenue and profit
float getGross() const;
float getProfit() const;

// overloads
// greater than - compares total sales
bool operator>(const Song &) const; // greater than
// less than - compares artist (not song!) names
bool operator<(const Song &) const; // less than
// += add more sales
Song &operator+=(float); // add some sales

private:
string name; // song name
string artist; // artist name
int year; // release year
float sales; // copies sold
int medID ; // medium ID

// static members
//static string mediumName(int m) { return mediumTypes[m]; }
static const string mediumTypes[]; // medium types
static const float mediumCost[]; // cost for medium
static const float mediumPrice[]; // price for medium
};

// overload of external stream operators
ostream &operator<<(ostream &, const Song &);
istream &operator>>(istream &, Song &);

#endif


//Song.h

----------------

sales.cpp

-------------------

#include
#include
#include
using namespace std;

#include "Song.h"

// read the data file, store in arrays
int readDatafile(Song[], int);
void outputList(Song[], int);

void sortAscend(/* ... */);
void sortDescend(/* ... */);
void swap(/* ... */);

main() {
static int arraysize= 10; // size of Song array
int numS=0; // the number of students read
Song slist[arraysize];

}
//sales.cpp

----------------

----------------

Song.cpp

#include
#include
using namespace std;

#include "Song.h"

const string Song::mediumTypes[]= { "physical", "digital" }; // medium types

// constructor
Song::Song(string n, string a, int y, float s, int m) {
name= n; artist=a; year= y; sales= s;
medID= (m >= 0 && m <= 1) ? m : 0; // default to "physical"
}

// istream operator overload
istream &operator>>(istream &input, Song &song) {

string a; string n; int y; float s; int m;

input >> a >> n >> y >> s >> m;
song.setArtist(a);
song.setName(n);
song.setYear(y);
song.setSales(s);
song.setMedium(m);

return input; // enables cascading
}

-------------------

data file- 0 is physical 1 is digital

Bing_Crosby White_Christmas 1942 50 0
Elton_John Candle_in_the_Wind 1997 33 0
Mungo_Jerry In_the_Summertime 1970 30 0
Bing_Crosby Silent_Night 1935 30 0
Bill_Haley Rock_Around_the_Clock 1954 25 0
Ed_Sheeran Shape_of_You 2017 41.5 1
Luis_Fonsi Despacito 2017 36.1 1
Kesha TiK_ToK 2009 25.2 1
Ed_Sheeran Perfect 2017 21.4 1
Wiz_Khalifa See_You_Again 2015 20.9 1
Chainsmokers Closer 2016 20.7 1
Bruno_Mars Uptown_Funk 2014 20 1

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Song.h

--------------------------------------------------------------------------

#ifndef SONG_H
#define SONG_H

using namespace std;

// struct definition for a child object
class Song
{
public:
// constructor
Song(string n = "", string a = "", int y = 0, float s = 0, int m = 0);

// Mutators (modifiers)
void setName(string n) { name = n; }
void setArtist(string v) { artist = v; }
void setYear(int v) { year = v; }
void setSales(float v) { sales = v; }
void setMedium(int v) { medID = v; }

// inspectors (accessors)
string getName() const { return name; }
string getArtist() const { return artist; }
string getMedium() const { return mediumTypes[medID]; }
int getYear() const { return year; }
float getSales() const { return sales; }

// methods for computing revenue and profit
float getGross() const;
float getProfit() const;
float numItemsSold(void) const;
// overloads
// greater than - compares total sales
bool operator>(const Song &) const; // greater than
// less than - compares artist (not song!) names
bool operator<(const Song &) const; // less than
// += add more sales
Song &operator+=(float); // add some sales
friend ostream &operator<<(ostream &, const Song &);

private:
string name; // song name
string artist; // artist name
int year; // release year
float sales; // copies sold
int medID; // medium ID

// static members
//static string mediumName(int m) { return mediumTypes[m]; }
static const string mediumTypes[]; // medium types
static const float mediumCost[]; // cost for medium
static const float mediumPrice[]; // price for medium
};

// overload of external stream operators
ostream &operator<<(ostream &, const Song &);
istream &operator>>(istream &, Song &);

#endif

________________________________________________________

Song.cpp :

#include <bits/stdc++.h>
#include "song.h"

// m = 0 // physical
// m = 1 // digital

float Song::numItemsSold(void) const
{
return sales;
}

float Song::getGross() const
{
return (!medID) ? (sales * 1.5) : (sales * 0.99);
}

float Song::getProfit() const
{
return (getGross() - ((!medID) ? (0.75 * sales) : (0.25 * sales)));
}

bool Song::operator>(const Song &S) const
{
return (getProfit() > S.getProfit());
}

bool Song::operator<(const Song &S) const
{
return (getProfit() < S.getProfit());
}

Song &Song::operator+=(float sales)
{
this->sales += sales;
return *this;
}

ostream &operator<<(ostream &output, const Song &S)
{
cout << S.artist << " " << S.name << " " << S.year << " " << S.sales << " " << S.medID << endl;
return output;
}

Add a comment
Know the answer?
Add Answer to:
The task is to write Song.cpp to complete the implementation of the Song class, as defined...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • This is for my c++ class and im stuck. Complete the Multiplex.cpp file with definitions for a fun...

    This is for my c++ class and im stuck. Complete the Multiplex.cpp file with definitions for a function and three overloaded operators. You don't need to change anything in the Multiplex.h file or the main.cpp, though if you want to change the names of the movies or concession stands set up in main, that's fine. Project Description: A Multiplex is a complex with multiple movie theater screens and a variety of concession stands. It includes two vectors: screenings holds pointers...

  • Redesign your Array class from lab6 as a class template to work with the application below....

    Redesign your Array class from lab6 as a class template to work with the application below. write your overloaded output stream operator as an inline friend method in the class declaration. Include the class template header file in your application as below. #include "Array.h" main() {   Array<char> c(3);   c.setValue(0,'c');   c.setValue(1,'s');   c.setValue(2,'c');   cout << c;   Array<int> i(3);   i.setValue(0,1);   i.setValue(1,2);   i.setValue(2,5);   cout << i;   Array<int> j(3);   j.setValue(0,10);   j.setValue(1,20);   j.setValue(2,50);   cout << j;   Array<int> ij;   ij = i + j;   cout << ij;...

  • BACKGROUND Movie Review sites collect reviews and will often provide some sort of average review to...

    BACKGROUND Movie Review sites collect reviews and will often provide some sort of average review to sort movies by their quality. In this assignment, you will collect a list of movies and then a list of reviews for each movie. You will then take a simple average (total of reviews divided by number of reviews) for each movie and output a sorted list of movies with their average review. Here you are provided the shell of a program and asked...

  • You are given a partial implementation of two classes. GroceryItem is a class that holds the...

    You are given a partial implementation of two classes. GroceryItem is a class that holds the information for each grocery item. GroceryInventory is a class that holds an internal listing of GroceryItems as well as the tax rate for the store. Your inventory could be 100 items it could be 9000 items. You won’t know until your program gets the shipment at runtime. For this you should use the vector class from the C++ standard library. I've completed the GroceryItem.h...

  • Write the functions needed to complete the following program as described in the comments. Use the...

    Write the functions needed to complete the following program as described in the comments. Use the input file course.txt and change the mark of student number 54812 to 80. /* File: course.cpp A student's mark in a certain course is stored as a structure (struct student as defined below) consisting of first and last name, student id and mark in the course.  The functions read() and write() are defined for the structure student.   Information about a course is stored as a...

  • Task #3 Arrays of Objects 1. Copy the files Song java (see Code Listing 7.1), Compact...

    Task #3 Arrays of Objects 1. Copy the files Song java (see Code Listing 7.1), Compact Disc.java (see Code Listing 7.2) and Classics.txt (see Code Listing 7.3) from the Student Files or as directed by your instructor. Song.java is complete and will not be edited. Classics.txt is the data file that will be used by Compact Disc.java, the file you will be editing. 2. In Compact Disc.java, there are comments indicating where the missing code is to be placed. Declare...

  • public class Song { private String title; private String artist; private int duration; public Song() {...

    public class Song { private String title; private String artist; private int duration; public Song() { this("", "", 0, 0); } public Song(String t, String a, int m, int s) { title = t; artist = a; duration = m * 60 + s; } public String getTitle() { return title; } public String getArtist() { return artist; } public int getDuration() { return duration; } public int getMinutes() { return duration / 60; } public int getSeconds() { return...

  • In this assignment you are required to complete a class for fractions. It stores the numerator...

    In this assignment you are required to complete a class for fractions. It stores the numerator and the denominator, with the lowest terms. It is able to communicate with iostreams by the operator << and >>. For more details, read the header file. //fraction.h #ifndef FRACTION_H #define FRACTION_H #include <iostream> class fraction { private: int _numerator, _denominator; int gcd(const int &, const int &) const; // If you don't need this method, just ignore it. void simp(); // To get...

  • Please do it carefully Using the header file ( MyArray.h ) Type the implementation file MyArray.cpp,...

    Please do it carefully Using the header file ( MyArray.h ) Type the implementation file MyArray.cpp, and a test file to test the functionality of the class. Hint: read all the explanations in the header with attention. MyArray.h : #ifndef MYARRAY_H #define MYARRAY_H #include <iostream> using namespace std; class MyArray { friend ostream& operator<<( ostream & output, const MyArray & rhs); // to output the values of the array friend istream& operator>>( istream & input, MyArray & rhs); // to...

  • Fix my code, if I the song or the artist name is not on the vector,...

    Fix my code, if I the song or the artist name is not on the vector, I want to user re-enter the correct song or artist name in the list, so no bug found in the program #include <iostream> #include <vector> #include <string> #include <algorithm> using namespace std; class musicList{ private: vector<string> songName; vector<string> artistName; public: void addSong(string sName, string aName){ songName.push_back(sName); artistName.push_back(aName); } void deleteSongName(string sName){ vector<string>::iterator result = find(songName.begin(), songName.end(), sName); if (result == songName.end()){ cout << "The...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT