Hello I’m wondering why is const used at the “get” functions in this class?
class Album {
public:
Album(string c, Artist* a, string t, int y, int no):barCode(c), artist(a), title(t), year(y), noCopiesSold(no){}
const string& getBarCode() const {return barCode;}
Artist* getArtist() const {return artist;}
const string& getTitle() const {return title;}
int getYear() const {return year;}
int getNoCopiesSold() const {return noCopiesSold;}
void setBarCode(string value) {barCode = value;}
void setArtist(Artist* value) {artist = value;}
void setTitle(string value) {title = value;}
void setYear(int value) {year = value;}
void setNoCopiesSold(int value) {noCopiesSold = value;}
friend ostream& operator << (ostream& out, const Album& album)
{
out << album.artist->getName() << " " << album.barCode << " " << album.title << " " << album.year << " " << album.noCopiesSold << endl;
return out;
}
private:
string barCode;
Artist* artist;
string title;
int year;
int noCopiesSold;
};
For const-correctness.
const, it implies that the function is notchanging any non-static member variable inside it and no other non-
constfunction is called insidethis method
constobject also (otherwiseresults in compiler error)
constcorrect methods; so const-correctness make your method compatible with it (e.g. less than operator —bool <Class>::operator < (const Class&) const)