Untitled
unknown
plain_text
a year ago
4.1 kB
7
Indexable
#include <iostream>
using namespace std;
class Date {
private:
int day;
int month;
int year;
static int count;
public:
Date() : day(0), month(0), year(0) {
count++;
}
Date(int d, int m, int y) : day(d), month(m), year(y) {
count++;
}
// ~Date() {
// count--;
// }
void setDay(int d) {
day = d;
}
void setMonth(int m) {
month = m;
}
void setYear(int y) {
year = y;
}
void setDate(int d, int m, int y) {
day = d;
month = m;
year = y;
}
int getDay() const {
return day;
}
int getMonth() const {
return month;
}
int getYear() const {
return year;
}
void display() const {
cout << day << "/" << month << "/" << year << endl;
}
static int getCount() {
return count;
}
bool operator<(const Date& other) const {
if (year != other.year)
return year < other.year;
else if (month != other.month)
return month < other.month;
else
return day < other.day;
}
bool operator>(const Date& other) const {
return other < *this;
}
bool operator==(const Date& other) const {
return (day == other.day && month == other.month && year == other.year);
}
bool operator!=(const Date& other) const {
return !(*this == other);
}
// Pre-increment operator
Date& operator++() {
day++;
if (month==1 || month==3 || month==5 || month==7 || month==8 || month==10 || month==12){
if (day > 31) {
day = 1;
month++;
if (month > 12) {
month = 1;
year++;
}
}
}
else if (month==2){
bool leap = false;
if(year % 4 == 0){
if(year % 100 == 0){
if(year % 400 == 0){
leap = true;
}
}
else
leap = true;
}
if(leap){
if (day > 29) {
day = 1;
month = 3;
}
}
else{
if (day > 28){
day = 1;
month = 3;
}
}
}
else {
if (day > 30) {
day = 1;
month++;
}
}
return *this;
}
// Post-increment operator
Date operator++(int) {
Date temp = *this;
++(*this);
return temp;
}
// Pre-decrement operator
Date& operator--() {
day--;
if (month==1 || month==5 || month==7 || month==8 || month==10 || month==12){
if (day < 1) {
day = 30;
month--;
if (month < 1) {
month = 12;
year--;
}
}
}
else if (month==3){
bool leap = false;
if(year % 4 == 0){
if(year % 100 == 0){
if(year % 400 == 0){
leap = true;
}
}
else
leap = true;
}
if(leap){
if (day < 1) {
day = 29;
month = 2;
}
}
else{
if (day < 1){
day = 28;
month = 2;
}
}
}
else {
if (day < 1) {
day = 31;
month--;
if (month < 1) {
month = 12;
year--;
}
}
}
return *this;
}
// Post-decrement operator
Date operator--(int) {
Date temp = *this;
--(*this);
return temp;
}
};
int Date::count = 0;
int main() {
Date date1;
Date date2(15, 8, 2021);
Date date3(5, 11, 2024);
date1.setDate(1, 1, 2020);
date1.display();
++date1;
date1.display();
date1--;
date1.display();
if (date1 < date2)
cout << "date1 is earlier than date2" << endl;
if (date3 > date2)
cout << "date3 is later than date2" << endl;
Date date4(31, 7, 2004);
Date date5(1, 3, 2020);
date4.display();
cout << "After incrementing\n";
++date4;
date4.display();
date5.display();
cout << "After decrementing\n";
--date5;
date5.display();
cout << "Total Date objects created: " << Date::getCount() << endl;
return 0;
}Editor is loading...
Leave a Comment