Create Calendar class with day, month and year as data members. Include default and parameterized constructors to initialize a Calendar object with a valid date value. Define a function AddDays to add days to the Calendar object. Define a display function to show data in “dd/mm/yyyy” format.
CODING:
#include<iostream.h>
#include<conio.h>
class calendar
{
int date,month,year;
public:
calendar()
{
date=month=year=0;
}
calendar(int a,int b,int c)
{
date=a;
month=b;
year=c;
}
void add()
{
int a;
cout<<"Enter the date you want to add:";
cin>>a;
date=date+a;
do
{
if((month==1||month==3||month==5||month==7||month==8||month==10||month==12) && (date>31))
{
date=date-31;
month++;
}
if((month==4||month==6||month==9||month==10||month==11) && (date>30))
{
date=date-30;
month++;
}
if((month==2) && (date>29))
{
date=date-29;
month++;
}
if(month>12)
{
month=month-12;
year++;
}
}while(date>31 || month>12);
}
void display()
{
cout<<date<<'/'<<month<<'/'<<year;
}
};
void main()
{
int a,b,c;
clrscr();
cout<<"Enter DATE:";
cin>>a;
cout<<"Enter MONTH:";
cin>>b;
cout<<"Enter YEAR:";
cin>>c;
calendar obj1(a,b,c);
obj1.add();
obj1.display();
getch();
}
CODING:
#include<iostream.h>
#include<conio.h>
class calendar
{
int date,month,year;
public:
calendar()
{
date=month=year=0;
}
calendar(int a,int b,int c)
{
date=a;
month=b;
year=c;
}
void add()
{
int a;
cout<<"Enter the date you want to add:";
cin>>a;
date=date+a;
do
{
if((month==1||month==3||month==5||month==7||month==8||month==10||month==12) && (date>31))
{
date=date-31;
month++;
}
if((month==4||month==6||month==9||month==10||month==11) && (date>30))
{
date=date-30;
month++;
}
if((month==2) && (date>29))
{
date=date-29;
month++;
}
if(month>12)
{
month=month-12;
year++;
}
}while(date>31 || month>12);
}
void display()
{
cout<<date<<'/'<<month<<'/'<<year;
}
};
void main()
{
int a,b,c;
clrscr();
cout<<"Enter DATE:";
cin>>a;
cout<<"Enter MONTH:";
cin>>b;
cout<<"Enter YEAR:";
cin>>c;
calendar obj1(a,b,c);
obj1.add();
obj1.display();
getch();
}
Comments
Post a Comment