Industrial Training

Date



#include<iostream.h>
#include<conio.h>

class Date{
int d,m,y;
static Date default_date;
public:
Date(int dd); //constructor 1
Date(int dd,int mm); //constructor 2
Date(int dd=0,int mm=0,int yy=0);//constructor 3
static void set_default_date(int dd,int mm,int yy);//to set the default date
void display(void); //displays the date on the screen.
int leapyear(void); //checks if the current year is a leap year.
void addyear(int yy); //adds yy no. of years to the current date.
friend cal_age(Date d1,Date d2);//returns the difference in years between the
//date d1 and d2. If d1>d2 an error msg is printed.
};

 

 

// -------- Explicit member function definitions ---------------------------------------

Date::Date(int dd){//constructor(day)
d=dd;
m=default_date.m;
y=default_date.y;
}
Date::Date(int dd,int mm){//constructor(day,month)
d=dd;
m=mm;
y=default_date.y;
}
Date::Date(int dd,int mm,int yy){//constructor(day,month,year)
if(dd==0)
d=default_date.d;
else
d=dd;
if(mm==0)
m=default_date.m;
else
m=mm;
if(yy==0)
y=default_date.y;
else
y=yy;
return;
}
void Date::set_default_date(int dd,int mm, int yy){//sets the default date
default_date=Date(dd,mm,yy);
}
void Date::display(void){//displays the date
cout<<"Date: "<<d<<" . "<<m<<" . "<<y<<endl;
cout<<"Default date: "<<default_date.d<<" . "<<default_date.m<<" . "<<default_date.y<<"\n";
cout<<endl;
}
int Date::leapyear(void){//checks if the current year is a leap year
int i=0;
if(y%4==0)
i=1;
return i;
}
void Date::addyear(int yya){//adds yya no of years to the current date.
//If the date is 29 feb but the new yearis not aleap yr,
//it sets the Date to 1st March.
y=y+yya;
if(!(leapyear()))
if(m==2 && d==29){
m=3;//set date to 1st march
d=1;
}
return;
}

// -------------------------------------------------------------------------------------

 

Date Date::default_date;
int main (void){
Date::set_default_date(1,1,2005);
Date today(2,2,2005);
Date BDay(1,5,2004);//date & month; year of the default_date
cout<<"Today...\n";
today.display();
cout<<"BDay...\n";
BDay.display();
cout<<"Age = "<<cal_age(BDay,today)<<endl;

 

return 0;
}

 

 

int cal_age(Date Date_of_birth,Date Today){
if(Date_of_birth.y > Today.y){
cout<<"Error: Born in future.\n";
return -1;
}
return(Today.y - Date_of_birth.y);
}

Hi I am Pluto.