Home PC Games Linux Windows Database Network Programming Server Mobile  
           
  Home \ Programming \ C ++ class implementation date operator overloading     - DRBD + Heartbeat solve NFS single point of failure (Server)

- Port Telnet command to detect the remote host is turned on (Linux)

- Java coding conventions (Programming)

- Binary search and modification (Programming)

- Oracle 11g creates virtual private directory RMAN-06004 ORA-00942 error handling (Database)

- numpy and SciPy installation under Python for scientific computing package (Linux)

- Setting up Linux machine through a proxy firewall (Linux)

- Linux Log Clear (Linux)

- PHP interview questions of design patterns (Programming)

- EXP-00091: Exporting questionable statistics Processing Method (Database)

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

- Nginx introduced Dynamic Module Architecture (Server)

- FPM quickly create packages with RPM (Linux)

- How to create a cloud encrypted file system in Linux systems (Linux)

- Oracle 11g RAC installation considerations Miscellany (Database)

- On Android running ClojureScript (Linux)

- Create the best introductory tutorial REST API (Linux)

- MySQL Study of --Percona Server 5.5 Upgrade 5.6 (Database)

- Linux find command detailing (Linux)

- Safety testing Unix and Linux server entry succinctly (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 Oracle environment variable is invalid Problem Solving (Database)
- 10 easy to use Linux utilities Recommended (Linux)
- Installation JDK 1.8 under CentOS7 (Linux)
- Python Socket Network Programming (Programming)
- MySQL composite partition (Database)
- Zabbix monitoring platform to extract a single server graphical Python implementation of concurrent (Server)
- To setup a ftp server under Linux (Server)
- Linux kernel boot to retain large memory method summary (Linux)
- Oracle restrict certain IP, the malicious user actions on important table (Database)
- MySQL Slave synchronization problem solving (Database)
- Why not use the ifconfig command under RedHat Linux 5 (Linux)
- C / C ++ language variable scope: local variables, global variables, file-level variables (Programming)
- Deploy the project to the server from GitHub (Server)
- Java filter (Programming)
- Security basics: simple analytical framework for Linux system firewall (Linux)
- TWiki LDAP error appears the problem is solved (Linux)
- RedHat yum configuration and the prompt (Linux)
- linux network security experience (Linux)
- iOS development -Launch Image and Launchscreen (Programming)
- Wireless LAN security solutions (Linux)
     
           
     
  CopyRight 2002-2022 newfreesoft.com, All Rights Reserved.