1 / 5

Case Study

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:

keene
Download Presentation

Case Study

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. B Smith: Class required 30 to 40 minutes to do. More practice needed Case Study Math 130Lecture # 34

  2. Overview

  3. 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 };

  4. 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; }

  5. 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; }

More Related