233k views
1 vote
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. Output should resemble the following:

User Dividius
by
4.6k points

2 Answers

0 votes

Answer:

Check the explanation

Step-by-step explanation:

Code:

dateType.h

#ifndef dateType_H

#define dateType_H

class dateType

{

public:

void setDate(int month, int day, int year);

//Function to set the date.

//The member variables dMonth, dDay, and dYear are set

//according to the parameters.

//Postcondition: dMonth = month; dDay = day;

// dYear = year

int getDay() const;

//Function to return the day.

//Postcondition: The value of dDay is returned.

int getMonth() const;

//Function to return the month.

//Postcondition: The value of dMonth is returned.

int getYear() const;

//Function to return the year.

//Postcondition: The value of dYear is returned.

void printDate() const;

//Function to output the date in the form mm-dd-yyyy.

void isLeapYear() const;

dateType(int month = 1, int day = 1, int year = 1900);

//Constructor to set the date

//The member variables dMonth, dDay, and dYear are set

//according to the parameters.

//Postcondition: dMonth = month; dDay = day; dYear = year;

// If no values are specified, the default

// values are used to initialize the member

// variables.

private:

int dMonth; //variable to store the month

int dDay; //variable to store the day

int dYear; //variable to store the year

};

#endif

dateType.cpp

#include <iostream>

#include "dateType.h"

using namespace std;

void dateType::setDate(int month, int day, int year)

{

// Checking month is valid

while(month<1 || month>12)

{

cout << "Enterd month "<<month<< " is wrong"<<endl;

cout << "Enter correct month"<<endl;

cin>>month;

}

dMonth = month;

// Checking date is valid

while(day<1 || day>31)

{

cout << "Enterd date "<<day<<" is wrong"<<endl;

cout<<"Enter correct date"<<endl;

cin>>day;

}

dDay = day;

int count_digits = 0;

int flag=0;

int year1;

// Counting number of digits in year

while(flag==0)

{

year1=year;

count_digits=0;

while (year) {

year /= 10;

count_digits++;

}

if(count_digits != 4)

{

cout << "Enterd year "<<year1<<" is wrong"<<endl;

cout<<"Enter correct year"<<endl;

cin>>year;

flag=0;

}

else

flag=1;

}

dYear = year1;

}

int dateType::getDay() const

{

return dDay;

}

int dateType::getMonth() const

{

return dMonth;

}

int dateType::getYear() const

{

return dYear;

}

void dateType::printDate() const

{

cout << dMonth << "-" << dDay << "-" << dYear;

}

void dateType::isLeapYear() const

{

if ( dYear%400 == 0)

cout<<endl<<dYear<< " is leap year.\\";

else if ( dYear%100 == 0)

cout<<endl<<dYear<< " is leap year.\\";

else if ( dYear%4 == 0 )

cout<<endl<<dYear<< " is leap year.\\";

else

cout<<endl<<dYear<< " is not leap year.\\";

}

//Constructor with parameters

dateType::dateType(int month, int day, int year)

{

// Checking month is valid

while(month<1 || month>12)

{

cout << "Enterd month "<<month<< " is wrong"<<endl;

cout << "Enter correct month"<<endl;

cin>>month;

}

dMonth = month;

// Checking date is valid

while(day<1 || day>31)

{

cout << "Enterd date "<<day<<" is wrong"<<endl;

cout<<"Enter correct date"<<endl;

cin>>day;

}

dDay = day;

int count_digits = 0;

int flag=0;

int year1;

// Counting number of digits in year

while(flag==0)

{

year1=year;

count_digits=0;

while (year) {

year /= 10;

count_digits++;

}

if(count_digits != 4)

{

cout << "Enterd year "<<year1<<" is wrong"<<endl;

cout<<"Enter correct year"<<endl;

cin>>year;

flag=0;

}

else

flag=1;

}

dYear = year1;

}

#include<iostream>

#include "dateType.h"

using namespace std;

int main()

}

}

User CptAJ
by
5.5k points
0 votes

Answer:

see explaination

Step-by-step explanation:

dateType.h

#ifndef dateType_H

#define dateType_H

class dateType

{

public:

void setDate(int month, int day, int year);

//Function to set the date.

//The member variables dMonth, dDay, and dYear are set

//according to the parameters.

//Postcondition: dMonth = month; dDay = day;

// dYear = year

int getDay() const;

//Function to return the day.

//Postcondition: The value of dDay is returned.

int getMonth() const;

//Function to return the month.

//Postcondition: The value of dMonth is returned.

int getYear() const;

//Function to return the year.

//Postcondition: The value of dYear is returned.

void printDate() const;

//Function to output the date in the form mm-dd-yyyy.

void isLeapYear() const;

dateType(int month = 1, int day = 1, int year = 1900);

//Constructor to set the date

//The member variables dMonth, dDay, and dYear are set

//according to the parameters.

//Postcondition: dMonth = month; dDay = day; dYear = year;

// If no values are specified, the default

// values are used to initialize the member

// variables.

private:

int dMonth; //variable to store the month

int dDay; //variable to store the day

int dYear; //variable to store the year

};

#endif

dateType.cpp

#include <iostream>

#include "dateType.h"

using namespace std;

void dateType::setDate(int month, int day, int year)

{

// Checking month is valid

while(month<1 || month>12)

{

cout << "Enterd month "<<month<< " is wrong"<<endl;

cout << "Enter correct month"<<endl;

cin>>month;

}

dMonth = month;

// Checking date is valid

while(day<1 || day>31)

{

cout << "Enterd date "<<day<<" is wrong"<<endl;

cout<<"Enter correct date"<<endl;

cin>>day;

}

dDay = day;

int count_digits = 0;

int flag=0;

int year1;

// Counting number of digits in year

while(flag==0)

{

year1=year;

count_digits=0;

while (year) {

year /= 10;

count_digits++;

}

if(count_digits != 4)

{

cout << "Enterd year "<<year1<<" is wrong"<<endl;

cout<<"Enter correct year"<<endl;

cin>>year;

flag=0;

}

else

flag=1;

}

dYear = year1;

}

int dateType::getDay() const

{

return dDay;

}

int dateType::getMonth() const

{

return dMonth;

}

int dateType::getYear() const

{

return dYear;

}

void dateType::printDate() const

{

cout << dMonth << "-" << dDay << "-" << dYear;

}

void dateType::isLeapYear() const

{

if ( dYear%400 == 0)

cout<<endl<<dYear<< " is leap year.\\";

else if ( dYear%100 == 0)

cout<<endl<<dYear<< " is leap year.\\";

else if ( dYear%4 == 0 )

cout<<endl<<dYear<< " is leap year.\\";

else

cout<<endl<<dYear<< " is not leap year.\\";

}

//Constructor with parameters

dateType::dateType(int month, int day, int year)

{

// Checking month is valid

while(month<1 || month>12)

{

cout << "Enterd month "<<month<< " is wrong"<<endl;

cout << "Enter correct month"<<endl;

cin>>month;

}

dMonth = month;

// Checking date is valid

while(day<1 || day>31)

{

cout << "Enterd date "<<day<<" is wrong"<<endl;

cout<<"Enter correct date"<<endl;

cin>>day;

}

dDay = day;

int count_digits = 0;

int flag=0;

int year1;

// Counting number of digits in year

while(flag==0)

{

year1=year;

count_digits=0;

while (year) {

year /= 10;

count_digits++;

}

if(count_digits != 4)

{

cout << "Enterd year "<<year1<<" is wrong"<<endl;

cout<<"Enter correct year"<<endl;

cin>>year;

flag=0;

}

else

flag=1;

}

dYear = year1;

}

main.cpp

#include<iostream>

#include "dateType.h"

using namespace std;

int main()

{

dateType *dt1=new dateType();

cout<<"Date is "<<endl;

dt1->printDate();

cout<<endl;

dt1->isLeapYear();

cout<<endl;

dateType *dt2=new dateType(11,14,2019);

cout<<"Date is "<<endl;

dt2->printDate();

cout<<endl;

dt2->isLeapYear();

cout<<endl;

dt2->setDate(13,32,2016);

cout<<"Date is "<<endl;

dt2->printDate();

cout<<endl;

dt2->isLeapYear();

cout<<endl;

dt1->setDate(10,10,198);

cout<<"Date is "<<endl;

dt1->printDate();

cout<<endl;

dt1->isLeapYear();

cout<<endl;

system("pause");

return 0;

}