usedate.cpp
来自「大于小于等于 操作符重载 对于日期的使用」· C++ 代码 · 共 63 行
CPP
63 行
#include <iostream>
#include "myDate.h"
using namespace std;
int main()
{
//自定义四个不同日期(日期 A 为默认构造日期)
Date A;
Date B(1980, 1, 1);
Date C(2007, 5, 20);
Date D(1980, 1, 1);
//打印四个日期,以作比较
cout << "Show all the years will be compared:" << endl;
cout << "The default constructor --> "<<"A = ";
A.Show();
cout << "B = ";
B.Show();
cout << "C = ";
C.Show();
cout << "D = ";
D.Show();
//使用重载后的‘ 〉’比较日期 B 和 C,显示其中较大的年份
cout<< "Compare the year of B and C with '>'" << endl;
if( B>C == true)
{
cout<< "The bigger year is: ";
B.Show();
}
else
{
cout<< "The bigger year is: ";
C.Show();
}
//使用重载后的‘〈 ’ 比较日期 B 和 C,显示其中较小的年份
cout<< "Compare the year of B and C with '<'" << endl;
if( B<C == true)
{
cout<< "The smaller year is: ";
B.Show();
}
else
{
cout<< "The smaller year is: ";
C.Show();
}
//使用重载后的‘ = ’比较日期 B 和 D,判断他们是否相等
cout<< "Compare the year of B and D with '='" << endl;
if( (B = D) == true)
{
cout<< "They are the same year, they are equal." << endl;
}
else
{
cout<< "They are not the same year, they are not equal." << endl;
}
return 0;
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?