Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. operator- in Date Class

operator- in Date Class

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestion
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • F Offline
    F Offline
    foxele
    wrote on last edited by
    #1

    Hello: I am writting a Date Class, and I want to overload operator - that can take a Date object, I wrote the code, but I felt it is stupid, Please help me: *************************************************************************** int Date::days[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; int Isbigger(int year, int right_year) { int bigger; bigger = year > right_year?year:right_year; returen bigger; }//get the bigger value of two int Issmaller(int year, int right_year) { int smaller; smaller = year < right_year? year:right_year; return smaller; }//get the smaller value of two int Date::operator -(Date right) { int subday; int subyear; if (year == right.year) { subyear = 0; if(month == right.month) { subday = Isbigger(day, right.day) - Issmaller(day,right.day); //if the same year and same month, just subtract the date; } else if(month != right.month) //if same year different month, { subday = (days[month] - day) + right.day); //get the days in beginning and ending month, for(i = (Isbigger(day,right.day) - 1); i >issmaller(day,right.day) ; i++) { subday += days[i]; //then plus the intermedia month, } } } else //not in the same year { int yearDifference; yearDifference = Isbigger(year,right.year) - 1; subyear = yearDifference * 365; if(month == right.month) { subday = Isbigger(day, right.day) - Issmaller(day,right.day); //if the same year and same month, just subtract the date; } else if(month != right.month) //if same year different month, { subday = (days[month] - day) + right.day); //get the days in beginning and ending month, for(i = (Isbigger(day,right.day) - 1); i >issmaller(day,right.day) ; i++) { subday += days[i]; //then plus the intermedia month, } } subday += subyear; } return subday; } Thank You So Much

    V J 2 Replies Last reply
    0
    • F foxele

      Hello: I am writting a Date Class, and I want to overload operator - that can take a Date object, I wrote the code, but I felt it is stupid, Please help me: *************************************************************************** int Date::days[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; int Isbigger(int year, int right_year) { int bigger; bigger = year > right_year?year:right_year; returen bigger; }//get the bigger value of two int Issmaller(int year, int right_year) { int smaller; smaller = year < right_year? year:right_year; return smaller; }//get the smaller value of two int Date::operator -(Date right) { int subday; int subyear; if (year == right.year) { subyear = 0; if(month == right.month) { subday = Isbigger(day, right.day) - Issmaller(day,right.day); //if the same year and same month, just subtract the date; } else if(month != right.month) //if same year different month, { subday = (days[month] - day) + right.day); //get the days in beginning and ending month, for(i = (Isbigger(day,right.day) - 1); i >issmaller(day,right.day) ; i++) { subday += days[i]; //then plus the intermedia month, } } } else //not in the same year { int yearDifference; yearDifference = Isbigger(year,right.year) - 1; subyear = yearDifference * 365; if(month == right.month) { subday = Isbigger(day, right.day) - Issmaller(day,right.day); //if the same year and same month, just subtract the date; } else if(month != right.month) //if same year different month, { subday = (days[month] - day) + right.day); //get the days in beginning and ending month, for(i = (Isbigger(day,right.day) - 1); i >issmaller(day,right.day) ; i++) { subday += days[i]; //then plus the intermedia month, } } subday += subyear; } return subday; } Thank You So Much

      V Offline
      V Offline
      V 0
      wrote on last edited by
      #2

      In some programs they convert the date to a number. (eg. like CTime class) then you can detect real easy is bigger or smaller and just substract or add them. I would take in account that in the year 2004 february has 29 days... leap years. good luck. "If I don't see you in this world, I'll see you in the next one... and don't be late." ~ Jimmy Hendrix

      J 1 Reply Last reply
      0
      • F foxele

        Hello: I am writting a Date Class, and I want to overload operator - that can take a Date object, I wrote the code, but I felt it is stupid, Please help me: *************************************************************************** int Date::days[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; int Isbigger(int year, int right_year) { int bigger; bigger = year > right_year?year:right_year; returen bigger; }//get the bigger value of two int Issmaller(int year, int right_year) { int smaller; smaller = year < right_year? year:right_year; return smaller; }//get the smaller value of two int Date::operator -(Date right) { int subday; int subyear; if (year == right.year) { subyear = 0; if(month == right.month) { subday = Isbigger(day, right.day) - Issmaller(day,right.day); //if the same year and same month, just subtract the date; } else if(month != right.month) //if same year different month, { subday = (days[month] - day) + right.day); //get the days in beginning and ending month, for(i = (Isbigger(day,right.day) - 1); i >issmaller(day,right.day) ; i++) { subday += days[i]; //then plus the intermedia month, } } } else //not in the same year { int yearDifference; yearDifference = Isbigger(year,right.year) - 1; subyear = yearDifference * 365; if(month == right.month) { subday = Isbigger(day, right.day) - Issmaller(day,right.day); //if the same year and same month, just subtract the date; } else if(month != right.month) //if same year different month, { subday = (days[month] - day) + right.day); //get the days in beginning and ending month, for(i = (Isbigger(day,right.day) - 1); i >issmaller(day,right.day) ; i++) { subday += days[i]; //then plus the intermedia month, } } subday += subyear; } return subday; } Thank You So Much

        J Offline
        J Offline
        John Oliver
        wrote on last edited by
        #3

        Hello, This mightn't be exactly what your asking but you should make the '-' operator a 'friend' rather than a class member. It means you can use it like the built-in subtraction operator instead of call a method. See http://guinness.cs.stevens-tech.edu/~asatya/courses/cs765IT/C++\_overview/lect10\_1.pdf John

        1 Reply Last reply
        0
        • V V 0

          In some programs they convert the date to a number. (eg. like CTime class) then you can detect real easy is bigger or smaller and just substract or add them. I would take in account that in the year 2004 february has 29 days... leap years. good luck. "If I don't see you in this world, I'll see you in the next one... and don't be late." ~ Jimmy Hendrix

          J Offline
          J Offline
          John Oliver
          wrote on last edited by
          #4

          Hello, Think it goes something like this, but you should check on web ... if year is divisible by 4 then ....if year is not divisible by 100 then .........it's a leap year ....else .........if year is divisible by 400 then ...............it's a leap year .........endif ....endif endif John

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups