Answer to Question #193314 in C++ for Sankalp

Question #193314

Create a C++ class mydate with three members dd,mm,yy. Write a menu driven Program with the following options. -Increment date by 1 day. -Subtract 2 days from date. (Use function overloading ).

1
Expert's answer
2021-05-14T07:06:31-0400
#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;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment