ds lab programs 12 and 14
Post on 11-Dec-2015
9 Views
Preview:
DESCRIPTION
TRANSCRIPT
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 operations 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_daysis 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>
#include<process.h>
class date
{
private:
int d,m,y;
int a[20];
public:
date()
{
a[1]=31;
a[3]=31;
a[5]=31;
a[7]=31;
a[8]=31;
a[10]=31;
a[12]=31;
a[4]=30;
a[6]=30;
a[9]=30;
a[11]=30;
a[2]=28;
}
friend date operator + (date,int);
friend int operator - (date,date);
friend ostream& operator << (ostream &, date);
void read();
};
date operator + (date d1, int x)
{
int i;
for(i=1;i<=x;i++)
int operator - (date d1,date d2)
{
int count=0;
if(d1.m==d2.m && d1.y==d2.y)
{
count = d1.d-d2.d;
return count;
}
while(!((d1.d==d2.d)&&(d1.m==d2.m)&&(d1.y==d2.y)))
{
count++;
d2.d++;
if(d2.y % 4==0)d2.a[2]=29;
elsed2.a[2]=28;
if(d2.d > d2.a[d2.m]){
d2.d=1;d2.m++;
}
if(d2.m > 12){
d2.m=1;d2.y++;
}
}return count;
}ostream& operator << (ostream& os,date d1){
os<<d1.d<<"/"<<d1.m<<"/"<<d1.y<<endl;return os;
}void date :: read(){
cin>>d>>m>>y;}
void main()
{
date d1,d2;
int choice, x;
clrscr();
for(;;)
{
cout<<"1.Subtraction\n2. Addition\n";
cout<<"Enter your choice ";
cin>>choice;
switch(choice)
{
case 1: cout<<"Enter Valid date 1: ";
d1.read();
cout<<"Enter valid date 2: ";
d2.read();
x=d1-d2;
cout<<"No. of days between d1 & d2 : "<<x<<endl;
break;
case 2:cout<<"Enter valid date : ";
d1.read();
cout<<"Enter the no. of days to be added : ";
cin>>x;
d2=d1+x;
cout<<"New date : "<<d2<<endl;
break;
default: exit(0);
}
}
}
top related