Category : C Source Code
Archive   : ANSICPP.ZIP
Filename : EX08002.CPP

 
Output of file : EX08002.CPP contained in archive : ANSICPP.ZIP
// ex08002.cpp
// Overloading the + operator with a friend
#include

class Date {
int mo, da, yr;
public:
Date() {}
Date(int m, int d, int y) { mo = m; da = d; yr = y; }
void display() { cout << mo << '/' << da << '/' << yr; }
// ----- overloaded + operators
Date operator+(int);
friend Date operator+(int n, Date& dt) { return dt + n;}
};

static int dys[] = {31,28,31,30,31,30,31,31,30,31,30,31};
// -------- overloaded + operator
Date Date::operator+(int n)
{
Date dt = *this;
n += dt.da;
while (n > dys[dt.mo-1]) {
n -= dys[dt.mo-1];
if (++dt.mo == 13) {
dt.mo = 1;
dt.yr++;
}
}
dt.da = n;
return dt;
}

main()
{
Date olddate(2,20,90);
Date newdate;
newdate = 21 + olddate; // three weeks hence
newdate.display();
}


  3 Responses to “Category : C Source Code
Archive   : ANSICPP.ZIP
Filename : EX08002.CPP

  1. Very nice! Thank you for this wonderful archive. I wonder why I found it only now. Long live the BBS file archives!

  2. This is so awesome! 😀 I’d be cool if you could download an entire archive of this at once, though.

  3. But one thing that puzzles me is the “mtswslnkmcjklsdlsbdmMICROSOFT” string. There is an article about it here. It is definitely worth a read: http://www.os2museum.com/wp/mtswslnk/