This must be original work and not copied from elsewhere.In this chapter, the cl
ID: 3767954 • Letter: T
Question
This must be original work and not copied from elsewhere.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 data members. 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 data members. Add a function member, isLeapYear, to check whether a year is a leap year. Moreover, write a test program to test your class.
Explanation / Answer
#include<iostream.h>
#include<conio.h>
class dataType
{
public:
int day, month,year;
dataType()
{
day=0;
month=0;
year=0;
}
void setDate(int d, int m, int y)
{
if(d>0 && d<31)
{
day=d;
}
else
{
cout<<"Day not Valid";
}
if(m>0 && m<13)
{
month=m;
}
else
{
cout<<"Month not Valid";
}
if(y>0 && y<2016)
{
year=y;
}
else
{
cout<<"Year not Valid";
}
}
void isLeapYear(int y)
{
if(y%4==0)
{
cout<<"Year is Leap";
}
else
{
cout<<"Year is not Leap";
}
}
};
void main()
{
int d,m,y;
dataType o;
cout<<"Enter day";
cin>>d;
cout<<"Enter month";
cin>>m;
cout<<"Enter year";
cin>>y;
o.setDate(d,m,y);
o.isLeapYear(y);
getch();
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.