Saturday, 11 January 2014

A program in C++ to create a Class Date and add and subtract two valid dates. Illustrate the use of copy constructor.

#include<iostream.h>
#include<conio.h>
class date
{private:int d;
             int m;
             int y;
 public:date()
            {d=0;m=0;y=0;}
            date(int a,int b,int c)
            {d=a;m=b;y=c;}
             void getdata();
             void showdata()
             {
             
cout<<"\n the date -> "<<d<<":"<<m<<":"<<y<<".";
              cout<<"\n";
             }
             void sum(date a,date b)
             {d=a.d+b.d;
              m=a.m+b.m;
              y=a.y+b.y;
             }
             void diff(date a,date b)
             {d=a.d-b.d;
              m=a.m-b.m;
              y=a.y-b.y;
             }
}d1,d2,d3;
void date :: getdata()
{int c=0,k=0,t;
 float l;
 clrscr();
 while(c==0)
 {cout<<"\n enter the parameters of date \n";
  cout<<"\n enter year: ";
  cin>>y;
  if(y>31000 || y<-31000)
  {cout<<"\n please enter the value within computational limit bet. -31000 and 31000)";
  }
  else
  {l=y%100;
   if(l==0)
   {t=y/100;
    l=t%4;
    if(l==0)
    {k=1;}
   }
   else
   {l=y%4;
    if(l==0)
    {k=1;}
   }
   c=1;
  }
 }
 c=0;
 while(c==0)
 {cout<<"\n enter month(1-12): ";
  cin>>m;
  if(m<1 || m>12)
  {cout<<"\n you have entered wrong value enter again";
  }
  else
  {c=1;}
 }
 l=0;
 while(c==1)
 {cout<<"\n enter date: ";
  cin>>d;
  if(m==1 || m==3 || m==5 || m==7 || m==8 || m==10 || m==12)
  {if(d<1 || d>31)
   {cout<<"\n sorry you have entered wrong value enter again";
   }
   else
   {c=0;}
  }
  else
  if(m==2 || m==4 || m==6 || m==9 || m==11)
  {if(d<1 || d>30)
   {cout<<"\n sorry you have entered wrong value enter again";
   }
   else
   {c=0;}
  }
  else
  {if(m==2)
   {if(k==1)
    {if(d<1 || d>29)
     {cout<<"\n sorry you have entered wrong value enter again";
     }
     else
     {c=0;}
    }
   }
   else
   if(d<1 || d>28)
   {cout<<"\n sorry you have entered wrong value enter again";
   }
   else
   {c=0;}
  }
 }
}
void main()
{int n,i;
 clrscr();
 d1.getdata();
 d1.showdata();
 d2.getdata();
 d2.showdata();
 d3.sum(d1,d2);
 d3.showdata();
 d3.diff(d1,d2);
 d3.showdata();
 getch();
}



No comments:

Post a Comment