📄 pr11018.cpp
字号:
////////////////////////////////////////
// File Name: pr11018.cpp
////////////////////////////////////////
#include <iostream>
#include <cstring>
////////////////////////////////////////
// Date class.
////////////////////////////////////////
class Date
{
int mo, da, yr;
char *month;
public:
Date(int m = 0, int d = 0, int y = 0);
~Date();
// Overloaded assignment operator.
void operator=(const Date&);
void display() const;
};
// The constructor definition.
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;
if (m != 0)
{
month = new char[std::strlen(mos[m-1])+1];
std::strcpy(month, mos[m-1]);
}
else
month = 0;
}
// The destructor definition.
Date::~Date()
{
delete [] month;
}
// Display member function.
void Date::display() const
{
if (month != 0)
std::cout << month << ' ' << da
<< ", " << yr << std::endl;
}
// Overloaded Date assignment.
void Date::operator=(const Date& dt)
{
if (this != &dt)
{
mo = dt.mo;
da = dt.da;
yr = dt.yr;
delete [] month;
if (dt.month != 0)
{
month = new char [std::strlen(dt.month)+1];
std::strcpy(month, dt.month);
}
else
month = 0;
}
}
////////////////////////////////////////
// The main() function.
////////////////////////////////////////
int main()
{
// First date.
Date birthday(6,24,1940);
birthday.display();
// Second date.
Date newday(7,29,1941);
newday.display();
// Assign the first date to the second.
newday = birthday;
newday.display();
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -