50 likes | 212 Views
B Smith: Class required 30 to 40 minutes to do. More practice needed. Case Study. Math 130 Lecture # 34. Overview. Class Implementation. M130 Pop Quiz Friday, May 13, 2005 (Not collected for grade.) Implement the functions for the following Date class. class Date { private:
E N D
B Smith: Class required 30 to 40 minutes to do. More practice needed Case Study Math 130Lecture # 34
Class Implementation M130 Pop Quiz Friday, May 13, 2005 (Not collected for grade.) Implement the functions for the following Date class. class Date { private: int _month; int _day; int _year; public: Date(int = 7, int =4, int = 2001); // constructor int equals( Date& ); //test for equality void getDate(void); //member function to display a date };
B Smith: Better practice is to use (const Date& d) to avoid sending a complete object, but simply an invariant object reference. Also, if we send Date d as a parameter, the copy constructor will get called! Implementations Date::Date(int m, int d, int y) { _month = m; _day = d; _year = y; } bool Date::equals(Date& d) { bool eq = false; if (_month == d._month && _day == d._day && _year == d._year) eq = true; return eq; } void Date::getDate() { cout << "month = " << _month << "\nday = " << _day << "\nyear = " << _year << endl; }
B Smith: Also, discuss how we could overload the “=“ operator. Driver Code int main() { Date a(7,4,1776), b, c(7,4,2001); cout << "\na: " << endl; a.getDate(); cout << "\nb: " << endl; b.getDate(); cout << "\nc: " << endl; c.getDate(); if(a.equals(b)) cout << "a == b" << endl; if(!a.equals(b)) cout << "a != b" << endl; if(b.equals(c)) cout << "a == c" << endl; system("pause"); return 0; }