Home PC Games Linux Windows Database Network Programming Server Mobile  
           
  Home \ Programming \ C ++ class implementation date operator overloading     - Nginx + ownCloud + PHP + MySQL to build personal private cloud under CentOS7 (Server)

- Ubuntu Series Installation Docker (Linux)

- Fatal NI connect error 12170 error in Alert Log (Database)

- Hackers is how the invasion and control of Things devices? (Linux)

- How to make Linux a non-root user uses less than 1024 ports (Linux)

- Through Oracle to help restore MySQL data problems (Database)

- To install the iNode client on UbuntuKylin 13.10 (Linux)

- CentOS 7 How to connect to a wireless network (Linux)

- Linux Learning Notes: Users and Groups (Linux)

- Oracle Linux 7.1 install Oracle 12C RAC (Database)

- Linux System Getting Started Tutorial: how to find information on Linux-embedded module (Linux)

- MySQL 5.6.26 source install (Database)

- Linux Getting Started tutorial: Experience Xen Virtual Machine chapter (Linux)

- Linux Network Programming --TCP and UDP datagram type Explanation (Programming)

- A process of how to get the current traffic in GNU Linux (Linux)

- Cobbler Add custom YUM source (Linux)

- Java interface and inheritance (Programming)

- SQL MySQL query table duplicate data (Database)

- Help you make Git Bisect (Linux)

- Depth Java Singleton (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:      
 
- VMware virtual machine to use bridged mode fast Internet access (Linux)
- Eclipse, Tomcat configuration JNDI connection Oracle data source example (Server)
- Verify the character set on MyCAT (Database)
- Daemon under Linux (Linux)
- Ubuntu 12.04 installed OpenCV 2.3.1, binary image (Linux)
- Learning and Practice (Linux)
- Linux excellent text editor (Markdown, LaTeX, MathJax) (Linux)
- Ubuntu is not in the sudoers file problem solving (Linux)
- Three easy open source firewall on the Linux platform (Linux)
- VPN built on CentOS (Server)
- Linux hybrid character device (Linux)
- CentOS7 Kubernetes used on container management (Server)
- Android Action Compendium (Programming)
- Use Docker / LXC quickly launch a desktop system (Linux)
- Java Set and List in the relationship and difference (Programming)
- How to install Ubuntu strategy game Wesnoth 1.12.0 (Linux)
- To use Java arrays implement the order form (Programming)
- Java String and StringBuffer and StringBuilder Comments (Programming)
- CentOS install Memcached (Server)
- Tecplot Installation under Linux (Linux)
     
           
     
  CopyRight 2002-2022 newfreesoft.com, All Rights Reserved.