In: Computer Science
C++ problem 11-2
In this chapter, the class dateType was designed to implement the date in a program, but the member function setDate and the constructor do not check whether the date is valid before storing the date in the member variables. Rewrite the definitions of the function setDate and the constructor so that the values for the month, day, and year are checked before storing the date into the member variables. Add a member function, isLeapYear, to check whether a year is a leap year. Moreover, write a test program to test your class.
Input should be format month day year with each separated by a space.
for dateType.h
//dateType.h
#ifndef date_H
#define date_H
class dateType
{
public:
void setDate(int month, int day, int year);
int getDay() const;
int getMonth() const;
int getYear() const;
void printDate() const;
bool isLeapYear();
dateType(int month = 1, int day = 1, int year = 1900);
private:
int dMonth;
int dDay;
int dYear;
};
#endif
---------------------------------------
For dateTypeImp.cpp
#include"dateType.h"
class dateType{
private:
int month;
int day;
int year;
public:
dateType(){
}
dateType(int month,int day,int year){
int maxNumberOfDays[]={31,28,31,30,31,30,31,31,30,31,30,31};
if(month < 1 || month > 12){
cout<<"Date :"<<month<<"-"<<day<<"-"<<year;
cout<<" Month is not valid"<<endl;
}
if(day >= 1 && day <= maxNumberOfDays[month]){
}
else if(month == 2 && day == 29){
if(isLeapYear() == 1){
}
else{
cout<<"Date :"<<month<<"-"<<day<<"-"<<year;
cout<<" Day is not valid for this month"<<endl;
}
}
else{
cout<<"Date :"<<month<<"-"<<day<<"-"<<year;
cout<<" Day is not valid for this month"<<endl;
}
if(year < 0){
cout<<"Date :"<<month<<"-"<<day<<"-"<<year;
cout<<" year is not valid"<<endl;
}
this->month = month;
this->day = day;
this->year = year;
}
int isLeapYear(){
int year = this->year;
if((year % 400) == 0){
return 1;
}
else if((year % 100) == 0){
return 0;
}
else if((year%4) == 0)
return 1;
// else
return 0;
}
void setDate(int month,int day,int year){
int maxNumberOfDays[]={31,28,31,30,31,30,31,31,30,31,30,31};
if(month < 1 || month > 12){
cout<<"Date :"<<month<<"-"<<day<<"-"<<year;
cout<<" Month is not valid"<<endl;
}
if(day >= 1 && day <= maxNumberOfDays[month]){
}
else if(month == 2 && day == 29){
if(isLeapYear() == 1){
}
else{
cout<<"Date :"<<month<<"-"<<day<<"-"<<year;
cout<<" Day is not valid for this month"<<endl;
}
}
else{
cout<<"Date :"<<month<<"-"<<day<<"-"<<year;
cout<<" Day is not valid for month="<<month<<endl;
}
if(year < 0){
cout<<"Date :"<<month<<"-"<<day<<"-"<<year;
cout<<" year is not valid"<<endl;
}
this->month = month;
this->day = day;
this->year = year;
}
string getDate(){
return (to_string(this->month)+"-"+to_string(this->day)+"-"+to_string(this->year));
}
};
--------------------------------------------------------
for main.cpp
#include"dateTypeImp.cpp"
int main(){
int month;
int day;
int year;
cin>>month>>day>>year;
dateType Date1(month,day,year);
cout<<"Date 1:"<<Date1.getDate();
int isLeap = Date1.isLeapYear();
if(isLeap == 1){
cout<<" this is a Leap Year"<<endl;
}
else{
cout<<" this is not a Leap Year"<<endl;
}
cin>>month>>day>>year;
dateType Date2;
Date2.setDate(month,day,year);
cout<<"Date 2:"<<Date2.getDate();
isLeap = Date2.isLeapYear();
if(isLeap == 1){
cout<<" this is a Leap Year"<<endl;
}
else{
cout<<" this is not a Leap Year"<<endl;
}
cin>>month>>day>>year;
dateType Date3(month,day,year);
cin>>month>>day>>year;
dateType Date4;
Date4.setDate(month,day,year);
cin>>month>>day>>year;
dateType Date5(month,day,year);
return 0;
}
`Hey,
Note: If you have any queries related the answer please do comment. I would be very happy to resolve all your queries.
#include <bits/stdc++.h>
using namespace std;
bool checkYear(int year)
{
// If a year is multiple of 400,
// then it is a leap year
if (year % 400 == 0)
return true;
// Else If a year is muliplt of 100,
// then it is not a leap year
if (year % 100 == 0)
return false;
// Else If a year is muliplt of 4,
// then it is a leap year
if (year % 4 == 0)
return true;
return false;
}
// Driver code
int main()
{
int year = 2000;
checkYear(year) ? cout << "Leap Year":
cout << "Not a Leap Year";
return 0;
}
Kindly revert for any queries
Thanks.