Saturday 28 September 2013

classes ad object(Q3)

Q . WAP to define a class Date which contains the data member , dd,mm, and yy.
     And , functions ,
              * readDate();
              * showDate();
              * increament();
              * decreament();
              * maxDays();
#include<iostream.h>
#include<conio.h>
class Date
{
     int dd,mm,yy;
     public:
     void readDate()
              {
                   cout<<endl<<"Enter the value of dd ,mm , and yy :";
                   cin>>dd>>mm>>yy;
              }

              void showDate() 
              {
                   cout<<endl<<dd<<":"<<mm<<":"<<yy;
                  }
              void increament ();
              void decreament(); 
                int maxDays();                       };

     int Date::maxDays()
     {
         
              switch(mm)
              {
                   case 1:
                   case 3:
                   case 5:
                   case 7:
                   case 8:
                   case 10:
                   case 12: 
                             return 31;
                   case 2:
                             if((yy%4==0&&yy%100!=0)||(yy%400==0))
                                  return 29;
                             else   
                                  return 28;
                   case 4:
                   case 6:
                   case 9:
                   case 11: 
                                  return 30;
          }
}
void Date::increament()
{  
          //increament the day
                  
              dd=dd+1;

          //check for the valid day range

              int max;

              max=maxDays();
              if(dd>max)
              {
//set day to the first day of the next month
          dd=1;
              //increament the month
                        mm=mm+1;
                        if(mm>12)
                        {
                             //set month to jan
                             mm=1;
                             //increament the year

                                  yy=yy+1;        
                  
                        }
              }
}

void Date::decreament()
{
         
          //decreament the day

              dd=dd-1;

          //check for the valid
              if(dd<1)
              {
                   //decreament the month
                    mm=mm-1;
                   if(mm<0)
                   {
                        //set month to dec
                        mm=12;
          //decreament the year

                             yy=yy-1;

                   }

                   //assign the day

                   dd=maxDays();

          }  
}

void main()
{
     Data ob;

     ob.readDate();

     ob.showDate();

     ob.increament();

     ob.showDate();

     ob.decreament();

     ob.showDate();

     getch();

}

No comments:

Post a Comment