Category : C++ Source Code
Archive   : TEACHCPP.ZIP
Filename : EX07019.CPP
// The this pointer
#include
#include
// ------- date class
class Date {
int mo, da, yr;
char *month;
public:
Date();
Date(int m, int d, int y);
~Date();
Date& operator=(Date&); //overloaded assignment operator
void display();
};
// constructor that is called for an uninitialized Date
Date::Date()
{
mo = 0; da = 0; yr = 0;
month = NULL;
}
// constructor that is called for an initialized Date
Date::Date(int m, int d, int y)
{
static char *mos[] = {
"January", "February", "March", "April", "May",
"June", "July", "August", "September", "October",
"November", "December"
};
mo = m; da = d; yr = y;
month = new char[strlen(mos[m-1])+1];
strcpy(month, mos[m-1]);
}
// Destructor for a Date
Date::~Date()
{
if (month != NULL)
delete month;
}
// ----------- display member function
void Date::display()
{
if (month != NULL)
cout << '\n' << month << ' ' << da << ", "
<< yr+1900;
}
// ---------- overloaded Date assignment
Date& Date::operator=(Date& dt)
{
mo = dt.mo;
da = dt.da;
yr = dt.yr;
if (month != NULL)
delete month;
if (dt.month != NULL) {
month = new char [strlen(dt.month)+1];
strcpy(month, dt.month);
}
else
month = NULL;
return *this;
}
main()
{
// ------ original date
Date birthday(6,24,40);
Date oldday, newday;
// ------ assign first to second to third
oldday = newday = birthday;
birthday.display();
oldday.display();
newday.display();
}
Very nice! Thank you for this wonderful archive. I wonder why I found it only now. Long live the BBS file archives!
This is so awesome! 😀 I’d be cool if you could download an entire archive of this at once, though.
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/