#include <iostream>
using namespace std;
class Mydate{
int dd, mm, yy, max;
void reassign(){
switch(mm){
case 1:;
case 3:;
case 5:;
case 7:;
case 8:;
case 10:;
case 12: max = 31; break;
case 2:max = 29; break;
case 4:;
case 6:;
case 9:;
case 11: max = 30; break;
default: break;
}
}
public:
Mydate(){
cout<<"Enter year: ";
cin>>yy;
do{
cout<<"Enter month: ";
cin>>mm;
}while(mm > 12 || mm < 0);
do{
cout<<"Enter day: ";
cin>>dd;
reassign();
}while(dd > max);
}
void display(){
cout<<dd<<"/"<<mm<<"/"<<yy;
}
Mydate operator++(int){
++dd;
if(dd > max){
dd = 1;
mm++;
if(mm > 12){
mm = 1;
reassign();
yy++;
}
}
return *this;
}
Mydate operator--(int){
--dd;
if(dd < 1){
dd = max;
mm--;
if(mm < 1){
mm = 12;
reassign();
yy--;
}
}
return *this;
}
};
int main(){
Mydate date;
int choice;
do{
cout<<"\n1. Increment date by 1 day.\n2. Subtract 2 days from date.\n3. Input new date\n";
cin>>choice;
switch(choice){
case 1: date++; date.display(); break;
case 2: date--; date--; date.display(); break;
case 3: date = Mydate(); break;
default: break;
}
}while(choice <= 3 && choice >= 1);
return 0;
}
Comments
Leave a comment