Create a class Date containing members as: - dd - mm - yyyy Write a C++ program for overloading operators >> and << to accept and display a Date also write a member function to validate a date.
//Note: If you are not using Borland or Turbo C then add the following line after header files :
//using namespace std;
#include<iostream.h>
#include<conio.h>
class date
{
public:
int d,m,y;
friend istream & operator >>(istream & in,date & dd); //declare friend function
friend ostream & operator <<(ostream & out,date & dd); //declare friend function
void valid()
{
switch(m)
{
case 1 : if(d>31)
cout<<"Invalid date";break;
case 2 : if(y%4==0)
{
if(d>29)
{
cout<<"Invalid date";break;
}
}
else
if(d>28)
cout<<"Invalid date";break;
case 3,6 : if(d>31)
cout<<"Invalid date";break;
case 4,7,8,10,12 : if(d>30)
cout<<"Invalid date";break;
case 5 : if(d>31)
cout<<"Invalid date";break;
}
}
};
istream & operator>>(istream & in,date & dd)
{
in>>dd.d>>dd.m>>dd.y;
return in;
}
ostream & operator <<(ostream & out,date & dd)
{
out<<dd.d<<"/"<<dd.m<<"/"<<dd.y;
return out;
}
int main()
{
date d; //creating instance of class date
cout<<"Enter date(dd mm yyyy):- ";
cin>>d;
d.valid();
cout<<"\nDate is: "<<d;
getch();
return(0);
}
0 Comments