C++ gurus and learners, we program ourselves :P all work is CC licensed

Saturday, November 2

Date Class Question

A Question was posted:
Q. Write a definition of Date class that contains three private data member month, day and year that provides the following functionality

Creating date with default values (zero for day, month and year)
Setting value of date at the time of creation
Changing values (day, month or year separately or combine) of any date
Getting values (day, month or year separately ) of any date
Displaying date value
Keep track of how many dates are created in a program and a function to access this value
Comparing two dates
One date is less than other ( < )
One date is greater than other ( > )
Two dates are equal ( == )
Two dates are not equal ( != )
Increment 1 in day value of date ( ++ ) both pre and post
Decrement 1 in day value of date ( - - ) both pre and post
Here is my solution though it can be improved by using function calls instead of direct calls but works fine as well as function calls slow the code ;)

Code:
 
class Date
{
 int month, day, year;
public:
 static int DatesCreated;
 Date(int mm = 0, int dd = 0, int yyyy = 0)// :month(mm), day(dd), year(yyyy)
 {
  setDate(mm, dd, yyyy);
  DatesCreated++;
 }

 void setMonth(int mm)
 {
  month = mm;
 }

 void setDay(int dd)
 {
  day = dd;
 }

 void setYear(int yyyy)
 {
  year = yyyy;
 }

 void setDate(int mm = 0, int dd = 0, int yyyy = 0)
 {
  setMonth(mm);
  setDay(dd);
  setYear(yyyy);
 }

 int getMonth()
 {
  return month;
 }

 int getDay()
 {
  return day;
 }

 int getYear()
 {
  return year;
 }

 void displayDate()
 {
  cout << "Date is" << month << '/' << day << '/' << year << endl;
 }

 int getDatesCount()
 {
  return DatesCreated;
 }

 bool operator<(Date b)
 {
  if (year < b.year || month < b.month || day(Date b)
 {
  if (year > b.year || month > b.month || day>b.day)return true;
  return false;
 }

 bool operator==(Date b)
 {
  if (b.day == day && b.month == month && b.year == year)return true;
  return false;
 }

 bool operator!=(Date b)
 {
  if (b.day != day && b.month != month && b.year != year)return true;
  return false;
 }

 Date& operator++()
 {
  month = (month + 1) % 12;
  day = (day + 1) % 31;
  year = (year + 1) % 9999;
  return *this;
 }

 Date& operator--()
 {
  month = (12 + (month - 1)) % 12;
  day = (31 + day - 1) % 31;
  year = (9999 + year - 1) % 9999;
  return *this;
 }
};

int Date::DatesCreated = 0;

void main()
{
    //The code is self explaining :)
}

9 comments :

  1. We need more similar questions

    ReplyDelete
  2. closing iostream tag khud HTML ney kiya hai, i have no power over it :D

    ReplyDelete
  3. sir header files to app ko khud include kerni prey gi :P ;)
    ye do line cop ker lain
    #include
    using namespace std;

    ReplyDelete
  4. ITS not running in dev c++ ? I am submitting as it is ,i will go through it tomrrow and will study in it detail..thankyou

    ReplyDelete
  5. kindly ignore the few HTML tags in code written, i can create another post with some functions being used but you must try to explore yourself, since you have to give viva one day :P

    ReplyDelete
  6. We can get values from user in main, main purpose of task was to write Date class definitions but we can write something in main as


    void main()
    {
    Date date1;
    int temp;

    cout<<"please enter day";
    cin>>temp;
    date.changeDay(temp);


    Date date2(0,30); //0 month, 30 day and 0 year value
    if(date2>date1)
    cout<<"Your Day value was less than 30"<
    cout<<"total dates created "<


    hence you can now use this class code in any way you like

    ReplyDelete
  7. actually when overloading operators it is better to return the actual data type of conditional operators i.e. condition is either false or true so we used bool in this situation

    ReplyDelete
  8. whats the purpose of using bool?
    & from where we are getting the values from the user?

    ReplyDelete