"Jai Saraswati Maa"
Hi ... Dear All ..Today I am uploading a superb example of operator overloading to check next date.
In this example we use Member function to overload ++ operator.
Special Thanks to Kanika Bhargava (Student)
Please check.
A. Code is **************
#include<conio.h>
#include<iostream.h>
class date
{
int d,m,y;
public:
void read()
{
cout<<"enter date:"<<endl; cin>>d;
cout<<"enter month:"<<endl; cin>>m;
cout<<"enter year:"<<endl; cin>>y;
}
void show()
{
cout<<"next date is:"<<d<<"/"<<m<<"/"<<y<<endl;
}
int operator++();
};
int date::operator++()
{
if(m>12)
{ cout<<"INVALID DATE!!"<<endl<<"ENTER DATE AGAIN"<<endl;
read();
}
else if(d==31&&m==12) {d=1;m=1;y++;}
else if(m==2)
{
if((y%4==0&&d==29)||(y%4!=0&&d==28))
{ d=1;m=3; }
else if((y%4==0)&&(d>29))
{cout<<"INVALID DATE!!"<<endl<<"ENTER DATE AGAIN"<<endl;
read();}
else if((y%4!=0)&&(d>28))
{cout<<"INVALID DATE!!"<<endl<<"ENTER DATE AGAIN"<<endl;
read(); }
else ++d;
}
else if(m==1||m==3||m==5||m==7||m==8||m==10||m==12)
{
if(d==31){++m; d=1;}
else if(d>31)
{ cout<<"INVALID DATE!!"<<endl<<"ENTER DATE AGAIN"<<endl;
read();
}
else ++d;
}
else if(m==4||m==6||m==9||m==11)
{
if(d==30){++m; d=1;}
else if(d>30)
{cout<<"INVALID DATE!!"<<endl<<"ENTER DATE AGAIN"<<endl;
read();
}
else ++d;
}
}
int main()
{
clrscr();
date d1;
d1.read();
++d1;
d1.show();
getch();
}
No comments:
Post a Comment