Home PC Games Linux Windows Database Network Programming Server Mobile  
           
  Home \ Programming \ C ++ class implementation date operator overloading     - OpenDaylight Helium version installed (Linux)

- Using IntelliJ IDEA Import Spark Spark latest source code and compile the source code (Linux)

- Build ftp server under CentOS 6.5 (Server)

- Linux log management tools Logrotate (Linux)

- The difference between equals and == in Java (Programming)

- Ubuntu 14.04 VirtualBox can not start solution (Linux)

- iOS persistence of data archiving NSKeyedArchiver (Programming)

- Easily create RPM packages using the FPM (Linux)

- Linux check disk parameters trapping lack amendments (Linux)

- Debian 8.1 (amd64) deployed Memcached (Server)

- ORA-04091 and Compound Trigger (Oracle 11g) (Database)

- VMware virtual machine operating system log Error in the RPC receive loop resolve (Linux)

- CentOS7 minimized installation can not find the 'ifconfig' command - Repair Tips (Linux)

- GDB remote connections RX Probe online debug program (Programming)

- JavaScript prototype and prototype chain and project combat (Programming)

- RHEL5 / 6 Installation Notes (Linux)

- CentOS 7 - use cgroups limit process resource (Linux)

- Python basis: os module on the file / directory using methods commonly used functions (Programming)

- Install Ubuntu text editor KKEdit 0.2.10 (Linux)

- Six Ways to view slides can be implemented Android (Programming)

 
         
  C ++ class implementation date operator overloading
     
  Add Date : 2018-11-21      
         
         
         
  Today wrote a simple C ++ date classes, who are interested can look at

Its declaration Date.h:

#define _CRT_SECURE_NO_WARNINGS 1
#ifndef __ONCEDATE__
#define __ONCEDATE__
#include
using namespace std;
class Date
{
public:
Date (int year = 1900, int mouth = 1, int day = 1); // constructor
bool operator == (const Date & d); // date comparison overloaded symbol
bool operator <(const Date & d);
bool operator <= (const Date & d);
bool operator> (const Date & d);
bool operator> = (const Date & d);
! Bool operator = (const Date & d);
Date operator + (int day); // operator overloading
Date operator + = (int day);
Date operator- (int day);
Date operator- = (int day);
Date operator ++ ();
Date operator ++ (int);
Date operator - ();
Date operator - (int);
void display (); // print function, you can override cout wild
int operator- (const Date & d); // not implemented the code, we are interested can try
private:
bool IsLeapYear (int year); // private class function judgment leap year
int GetMonthDay (int year, int month); // get the month of private class function
int _year;
int _mouth;
int _day;
};
#endif

It is defined as follows:
#include
#include "Date.h"
using namespace std;
 void Date :: display ()
{
cout << _year << "" << _mouth << ""
<< _day << Endl;
}
 Date :: Date (int year, int mouth, int day)
 {
 if ((year> = 0) &&
 (Mouth> 0 && mouth <13) &&
 (Day> 0 && day <= GetMonthDay (year, mouth)))
 {
 _year = year;
 _mouth = mouth;
 _day = day;
 }
 else
 {
 cout << "date error, change the default date" << endl;
 _year = 1900;
 _mouth = 1;
 _day = 1;
 }
 }
bool Date :: operator == (const Date & d)
 {
 return this -> _ year == d._year
 && This -> _ mouth == d._mouth
 && This -> _ day == d._day;
 }
bool Date :: operator <(const Date & d)
{
if (_year return true;
if (_year == d._year)
{
if (_mouth == d._mouth)
{
if (_day == d._day)
{
return false;
}
else if (_day return true;
}
else if (_mouth return true;
}
return false;
}
bool Date :: operator <= (const Date & d)
{
if (* this return true;
return false;
}
bool Date :: operator> (const Date & d)
{
if (! (* this <= d))
return true;
return false;
}
bool Date :: operator> = (const Date & d)
{
if (! (* this return true;
return false;
}
bool Date :: operator! = (const Date & d)
{
return (* this == d)!;
}
Date Date :: operator + (int day) // Date plus the number of days, as the number of days defined as int type, it may add a negative number of days
{// It is added in the loop determination process, if the Save operation responsible for the implementation, on the contrary performs its operations
Date DestDate (* this);
DestDate._day + = day;
while (DestDate._day> GetMonthDay (DestDate._year, DestDate._mouth) ||
DestDate._day <= 0)
{
if (DestDate._day> 0)
{
DestDate._day - = GetMonthDay (DestDate._year, DestDate._mouth);
if (DestDate._mouth == 12)
{
DestDate._mouth = 0;
DestDate._year ++;
}
DestDate._mouth ++;
}
else
{
DestDate._day + = GetMonthDay (DestDate._year, DestDate._mouth);
if (DestDate._mouth == 1)
{
DestDate._mouth = 13;
DestDate._year--;
}
DestDate._mouth--;
}
}
return DestDate;
}
Date Date :: operator + = (int day)
{
* This = * this + day;
return * this;
}
Date Date :: operator- (int day)
{
day = -day;
return * this + day;
}
Date Date :: operator- = (int day)
{
* This = * this - day;
return * this;
}
Date Date :: operator ++ ()
{
Date DestDate (* this);
* This + = 1;
return DestDate;
}
Date Date :: operator ++ (int)
{
* This + = 1;
return * this;
}
Date Date :: operator - ()
{
Date Destdate (* this);
- * This;
return Destdate;
}
Date Date :: operator - (int)
{
* This - = 1;
return * this;
}
int Date :: operator- (const Date & d) // date - the date for the first major comparative size of the date, and then counter
{// Statistics Date plus a small number of days month date into the same date, the final output
Date high (d);
Date low;
int count = 0;
int flag = 1;
if (* this> d)
{
low = high;
high = * this;
}
else
{
flag = 0;
low = * this;
}
while (low! = high)
{
low ++;
count ++;
}
if (! flag)
count = -count;
return count;
}
bool Date :: IsLeapYear (int year)
{
if (((year% 4 == 0) && (year% 100)) ||
(Year% 400 == 0))
{
return true;
}
return false;
}
int Date :: GetMonthDay (int year, int month) // 1 with an array Storage - share a few days in December
{
int monthArray [13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int day = monthArray [month];
if (month == 2 && IsLeapYear (year))
{
day + = 1;
}
return day;
}
     
         
         
         
  More:      
 
- Spark and Hadoop comparison (Server)
- Create and modify Oracle temporary table space (Database)
- CentOS7 installation performance monitoring system (Server)
- CentOS 6.6 install JDK7 (Linux)
- Rely on IP chain established Linux firewall (Linux)
- DRBD + Heartbeat solve NFS single point of failure (Server)
- Git large file storage will help handle large binary files (Linux)
- tcpdump Linux system security tools (Linux)
- Linux (Debian) install software, missing dynamic link libraries .so (Linux)
- Install snort intrusion detection system on Debian (Linux)
- Linux environment variable configuration (Linux)
- Linux at command (Linux)
- Shell scripts quickly deploy Tomcat project (Server)
- Java Set and List in the relationship and difference (Programming)
- Recycle Bin function realization in Linux (Linux)
- Nonstandard IMP-00010 error processing one case (Database)
- CentOS / Linux kernel upgrade (Linux)
- Intel Graphics Installer 1.0.3 released, support for Ubuntu (Linux)
- Ubuntu 14.10 PPA installed Android Studio (Linux)
- IntelliJ IDEA run in Mac10.9 and JDK7 environment (Linux)
     
           
     
  CopyRight 2002-2022 newfreesoft.com, All Rights Reserved.