Consider the following
Class date
{
private:
Int day;// from 1 to 31
Int month;// from 1 to 12
Int year;// from 2000 onwards
public:
Void advance() ;// move to next day
} ;
Task#1:
Implement a constructor that initialize new object of date to be set to the 1st of January 2000.
Task#2:
Implement setters for day, month and year.
Task#3:
Implement the advance method which moves to the next day, ensuring that all data members are updated appropriately.
#include <iostream>
using namespace std;
class date{
private:
int day;// from 1 to 31
int month;// from 1 to 12
int year;// from 2000 onwards
public:
//Task#1:
//Implement a constructor that initialize new object of date to be set to the 1st of January 2000.
date(){
this->day=1;
this->month=1;
this->year=2000;
}
//Task#2:
//Implement setters for day, month and year.
void setDay(int day){
this->day=day;
}
void setMonth(int month){
this->month=month;
}
void setYear(int year){
this->year=year;
}
//Task#3:
//Implement the advance method which moves to the next day, ensuring that all data members are updated appropriately.
void advance(){// move to next day
if (day > 0 && day < 28)
day += 1;
if (day == 28)
{
if (month == 2)
{
if ((year % 400 == 0) || (year % 100 != 0 || year % 4 == 0))
{
day = 29;
}
else
{
day = 1;
month = 3;
}
}
else
day += 1;
}
if (day == 29)
{
if (month == 2)
{
day = 1;
month = 3;
}
else
day += 1;
}
if (day == 30)
{
if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12)
day += 1;
else
{
day = 1;
month += 1;
}
}
if (day == 31)
{
day = 1;
if (month == 12)
{
year += 1;
month = 1;
}
else
month += 1;
}
cout << "Tomorrow's date:\n";
if (day < 10)
{
cout << "0" << day << "/";
}
else
cout <<day << "/";
if (month < 10)
{
cout << "0" << month << "/";
}
else
cout << month << "/";
cout << year;
}
};
int main (){
date date;
date.advance();
date.setDay(15);
date.setMonth(5);
date.setYear(2020);
cout<<"\n\n";
date.advance();
cout<<"\n\n";
system("pause");
return 0;
}
Comments
Leave a comment