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