Home PC Games Linux Windows Database Network Programming Server Mobile  
           
  Home \ Programming \ C ++ class implementation date operator overloading     - Oracle Shared Server Configuration (Database)

- OpenNMS separate database (Server)

- The FreeBSD zfs: failed with error 6 Error Resolution (Linux)

- Difference between TCP and UDP protocols (Linux)

- Java NIO1: I / O model overview (Programming)

- RHEL6.5 replace local YUM source (Linux)

- Simple configuration shell scripts virtual machine environment experiment (Linux)

- Switching Virtual Machine + Ubuntu graphical interface and terminal interface (Linux)

- Python2.7.7 source code analysis (Programming)

- DupeGuru- find and remove duplicate files (Linux)

- Samhain: Powerful intrusion detection system under Linux (Linux)

- Terminal Linux command prints - echo (Linux)

- iscsiadm command usage (Linux)

- Common Linux system performance monitoring command (Linux)

- Linux operating system security settings initial understanding (Linux)

- SQLite3 simple operation (Database)

- CentOS7 virtual machine settings, and bridging problems (Linux)

- Using RAID in Linux: Create a RAID 5 (Linux)

- PostgreSQL query result area is removed and precision (Database)

- 17 How to install the Ubuntu 14.04 and Linux Mint Kodi14 (XBMC) (Linux)

 
         
  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:      
 
- Linux kernel RCU (Read Copy Update) lock Brief - prequel (Linux)
- php for Linux the MySQL extension module installation and configuration (Database)
- pdf.js Tutorial (Linux)
- Unix average load average load calculation method (Server)
- Linux System Getting Started Learning: After starting in Ubuntu or Debian, enter the command line (Linux)
- Nagios plugin installation tutorial of Nrpe (Linux)
- Linux / Raspberry Pi using the following command-line based web browser (Linux)
- Batch kill processes using awk command (Linux)
- Copy and paste in Linux terminal and Vim (Linux)
- How to install Hadoop on CentOS7 (Server)
- Linux garbled file delete method (Linux)
- SLF4J Tutorial (Programming)
- Linux set to select the appropriate level of security of the network according to deployment (Linux)
- Automate deployment of Docker-based Rails applications (Server)
- Kali Linux resolve GPG error KEYEXPIRED 1425567400 (Linux)
- CentOS 6.3 compile and install LNMP environment (Server)
- Ubuntu 15.04 and Ubuntu 14.04 installed Cinnamon 2.6 (Linux)
- The specified user to execute commands under Linux (Linux)
- Ubuntu apt-mirror established local private sources (Linux)
- OpenGL Superb Learning Notes - GLSL language foundation (Programming)
     
           
     
  CopyRight 2002-2020 newfreesoft.com, All Rights Reserved.