A Question was posted:
Code:
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 :) }
We need more similar questions
ReplyDeleteclosing iostream tag khud HTML ney kiya hai, i have no power over it :D
ReplyDeletesir header files to app ko khud include kerni prey gi :P ;)
ReplyDeleteye do line cop ker lain
#include
using namespace std;
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
ReplyDeletekindly 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
ReplyDeleteWe 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
ReplyDeletevoid 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
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
ReplyDeletewhats the purpose of using bool?
ReplyDelete& from where we are getting the values from the user?
Here you go @Hassan Raza :)
ReplyDelete