class Date {
private:
int m_year, m_month, m_day;
public:
Date(int year, int month, int day)
{ setDate(year, month, day); }
void setDate(int year, int month, int day)
{ m_year = year;
m_month = month;
m_day = day;
}
int getYear() { return m_year; }
int getMonth() { return m_month; }
int getDay() { return m_day; }
};
// Pass date by const reference to avoid making a copy of date
void printDate(const Date &date)
{ cout<<date.getYear()<<"/"<<date.getMonth()<<"/"<<date.getDay(); }
int main()
{ Date date(2016, 10, 16);
printDate(date);
return 0;
}
I cant really find anything except the constructor looks funky. But can someone catch something and why is it wrong? thank you my friends!
getYear(), getMonth() and getDate() must be declared const if you want to use them with a const object.
That's what you try to do here: cout<<date.getYear()<<"/"<<date.getMonth()<<"/"<<date.getDay();
date is declared const (const Date &date)