/* Name: song.cpp * Purpose: Implementation file for Song Class * Author: Dr. Michael A. Redmond * Date Written: 03/06/2k * Date Revised: 03/27/2k - improved << and added == to allow use of list remove function * NOTES: */ // in song.cpp #include // allows string handling #include #include "song.h" using namespace std; Song::Song() { title = "unknown"; artist = "unknown"; lenMins = -1; lenSecs = -1; } // default length is 3:00 Song::Song(const string& titl, const string &art, int min, int sec) { title = titl; artist = art; if (min < 0) { lenMins = -1; } else { lenMins = min; } if ((sec < 0) || (sec > 59)) { lenSecs = -1; } else { lenSecs = sec; } } // following were in-lined // inspectors // string GetTitle() const; // string GetArtist() const; // int GetMins() const; // int GetSecs() const; // int GetLength() const; // mutators // void SetTitle(const string & titl); // void SetArtist(const string & art); // set minutes and seconds // if either bad, dont set either one and return false // if both ok, set and return true // default secs is 0 bool Song::SetLen(const int min, const int sec) { bool ok = true; if (min < 0) { ok = false; } if ((sec < 0) || (sec > 59)) { ok = false; } if (ok) { lenMins = min; lenSecs = sec; } return ok; } // allows outputting all info about song ostream& operator << (ostream & strOut, const Song & mySong) { strOut << mySong.GetTitle() << " by " << mySong.GetArtist() << " lasts " << mySong.GetMins() << ":"; int secs = mySong.GetSecs(); // force seconds to look right if in single digits if (secs < 10) { strOut << "0"; } strOut << secs; return strOut; } // allows checking equality between songs bool operator == (const Song & lhs, const Song & rhs) { if ( (lhs.GetTitle() == rhs.GetTitle() ) && (lhs.GetArtist() == rhs.GetArtist() ) && (lhs.GetMins() == rhs.GetMins() ) && (lhs.GetSecs() == rhs.GetSecs() ) ) { // all the same then the song is the same return true; } else return false; }