Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

e3tgn and Tmp lement a c+ class called oate that has the folowing private member

ID: 3870599 • Letter: E

Question

e3tgn and Tmp lement a c+ class called oate that has the folowing private member variables month (int) day (int) int) the following public member functions to the class. pefault Constructor with al1 default parameters: The constructors should use the values of the month, day, and s passed by the client program to set the month member variables. The constructor should check the values the onstrucs oof sthe garse derauiraes cthat1daois between 1-31. sonth is between 1 -12 and year is >o). f not, ccessors (getters): write one for each member variable month, day, and year Mutators (setters): write one for each member variable month, and year. The mutators should check if the values passed are valid (that is day is between 1-31, month is between 1-12 and year is >0) and if not, do not update the member variable with an invalid value. rint1: this function should print the date in For example, "1/2/2012 rint1a this unct on should print the date in "aonth/day/year" format to the screen, where each sonth, day and year are int's. s a str ing fone tion houlda print the 2dage in sonth day, year" format to the screen shere day and year are int 's and sonth For example, "anuary 2, 2012" b) Demonstrate the use of pate class by writing code in your main function. Make sure to create at least two different Date objects and call both print1 and print2 functions, etc.

Explanation / Answer

#include <iostream>

using namespace std;

class Date{

                int day,month,year;

                public:

                Date(){

                                day   = 1;

        month = 1;

        year = 2000;

                }

                Date(int m,int d,int y) {

                   

                    if((d>=1 && d<=31)&& (m>=1 && m<=12) && (y>0))

                                {   

                                    day = d;

                        month = m;

                                    year = y;

                    }else{

                    day   = 1;

            month = 1;

            year = 2000;

                    }

                }

                int getDay() {

                                return day;

                }

                void setDay(int d) {

                               if(d>=1 && d<=31)

                                {   

                                    day = d;

                       

                    }

                                   

                               

                }

                int getMonth() {

                                return month;

                }

                void setMonth(int m) {

                                if(m>=1 && m<=12)

                                {   

                                    

                        month = m;

                                   

                    }

                }

                int getYear() {

                                return year;

                }

                void setYear(int y) {

                                if(y>0)

                                {   

                                   

                                    year = y;

                    }

                }

                void print1(){

                    cout<<month<<"/"<<day<<"/"<<year<<endl;

                }

                void print2(){

                    string monthName="";

                    switch(month){

                        case 1:

                                monthName="January";

                                break;

                case 2:

                                monthName="February";

                                break;

                       case 3:

                                monthName="March";

                                break;

                           case 4:

                                monthName="April";

                                break;

                           case 5:

                                monthName="May";

                                break;

                           case 6:

                                monthName="June";

                                break;

                           case 7:

                                monthName="July";

                                break;

                           case 8:

                                monthName="August";

                                break;

                           case 9:

                                monthName="September";

                                break;

                           case 10:

                                monthName="October";

                                break;

                           case 11:

                                monthName="November";

                                break;

                           case 12:

                                monthName="December";

                                break;

                           default:

                                cout<<"inalid value";

                               

                        

                    }

                    cout<<monthName<<" "<<day<<", "<<year<<endl;

                }

               

};

int main() {

   Date date=Date(12,12,2012);

   cout<<"passing arguments 12,12,2012 (month,day,year)"<<endl;

    date.print1();

    date.print2();

    cout<<"setting new value 10 to month"<<endl;

    date.setMonth(10);

    date.print1();

    date.print2();

     cout<<"setting new value 32 to day and 0 to year"<<endl;

     //this will not effect any changes , because day is not between 1 to 31 and year is not greater than 0

    date.setYear(0);

    date.setDay(32);

    date.print1();

    date.print2();

   

    Date date1=Date(32,12,2012);

   cout<<"passing arguments 32,12,2012 (month,day,year) ,it will assign default values because conditions are not satisfying.."<<endl;

    date1.print1();//it prints 1/1/2000 ,month value is not between 1 to 12

    date1.print2();

}

OutPut:

passing arguments 12,12,2012 (month,day,year)

12/12/2012

December 12, 2012

setting new value 10 to month

10/12/2012

October 12, 2012

setting new value 32 to day and 0 to year

10/12/2012

October 12, 2012

passing arguments 32,12,2012 (month,day,year) ,it will assign default values because conditions are not satisfying..

1/1/2000

January 1, 2000

Hello There, in the above code:

Date(int m,int d,int y) {

                   

                    if((d>=1 && d<=31)&& (m>=1 && m<=12) && (y>0))

                                {   

                                    day = d;

                        month = m;

                                    year = y;

                    }else{

                    day   = 1;

            month = 1;

            year = 2000;

                    }

                }

               

                this constructor will assign the value month,day and year if all conditions are satisfied i.e month between 1 to 12 ,day between 1 to 31 & year >0

                if any condition fails it will assign default values month 1 day 1 year 2000

               

                if you want, you can use the below constructor also.

                if the parameters are 32,12,2012 (month,day,year) it will assign month=1,day=12,year=2012

                Date(int m,int d,int y) {

                   

                    if(d>=1 && d<=31)

                                {   

                                    day = d;

                    }else{

                    day   = 1;

           

                    }

                                if(m>=1 && m<=12)

                                {   

                                    

                        month = m;

                                   

                    }else{

                    

            month = 1;

           

                    }

                                if(y>0)

                                {   

                                   

                                    year = y;

                    }else{

            year = 2000;

                    }

                }

                Out put if you use this constructor:

                                passing arguments 12,12,2012 (month,day,year)

                                12/12/2012

                                December 12, 2012

                                setting new value 10 to month

                                10/12/2012

                                October 12, 2012

                                setting new value 32 to day and 0 to year

                                10/12/2012

                                October 12, 2012

                                passing arguments 32,12,2012 (month,day,year) ,it will assign default values because conditions are not satisfying..

                                1/12/2012

                                January 12, 2012