Blogger news

Followers

Monday, July 21, 2014
Design,develop and execute a program in c++ to create a class called DATE with methods to accept two valid dates in the form dd/mm/yy and to implement the following operation by overloading the operators + and – After every operation the results are to be displayed by overloading the operator<<
i.no_of_days=d1-d2;where d1 and d2 are DATE objects,d1>=d2 and no_of_days is an integer.
ii.d2=d1+no_of_days;where d1 is a DATE object and no_of_days is an integer.
#include<iostream.h>
#include<conio.h>

class date
{
  int dd;
  int mm;
  int yy;
  int a[13];
  public:
     date()
     {
       a[1]=31;a[2]=28;a[3]=31;a[4]=30;a[5]=31;a[6]=30;a[7]=31;a[8]=31;a[9]=30;
       a[10]=31;a[11]=30;a[12]=31;dd=0;mm=0;yy=0;
     }
     date operator+(int n);
     int operator-(date d2);
     friend ostream& operator<<(ostream &print,date d1);
     void read();
     int isleap(date d1);
  };


     date date::operator+(int n)
     {
       int i;
       for(i=1;i<=n;i++)
       { dd++;
                 if(isleap(*this))
                 {
                  a[2]=29;
                 }
                 if(dd>a[mm])
                 {
                  dd=1;
                  ++mm;
                 }
                 if(mm>12)
                 {
                  mm=1;
                  ++yy;
                 }

       }
       return (*this);
     }

     int date::operator-(date d2)
     { int count=0;
       while(!((mm==d2.mm)&&(dd==d2.dd)&&(yy==d2.yy)))
       { count++;
                 d2.dd++;
                 if(isleap(d2))
                 {
                  d2.a[2]=29;
                 }
                 if(d2.dd>d2.a[d2.mm])
                 {
                   d2.dd=1;
                   d2.mm++;
                 }
                 if(d2.mm>12)
                 {
                  d2.mm=1;
                  d2.yy++;
                 }
       }
       return count;
     }

     void date::read()
     {
      cout<<"enter the date"<<endl;
      cout<<"dd="<<endl;
      cin>>dd;
      cout<<"mm="<<endl;
      cin>>mm;
      cout<<"yy="<<endl;
      cin>>yy;
     }

     int date::isleap(date d1)
     {
      if((d1.yy%4==0)&&(d1.yy%100!=0))
      {
                return 1;
      }
      else
       return 0;
     }

     ostream& operator<<(ostream &print,date d1)
     {
       print<<d1.dd<<"/"<<d1.mm<<"/"<<d1.yy<<endl;
       return print;
      }

      void main()
      {
       int no_of_days=0;
       date d1,d2,d3;
       clrscr();
       d1.read();
      
       cout<<"date1="<<d1;
      
       cout<<"temp="<<endl;
       cin>>no_of_days;
       d3=d1+no_of_days;
       cout<<"date3="<<d3;
       d2.read();
       cout<<"date2="<<d2;
       no_of_days=d1-d2;
       cout<<"no_of_days="<<no_of_days<<endl;
       getch();
      }
//program end


0 comments: