Question

The class dateType is designed to implement the date in a program, but the member function...

The class dateType is 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 the 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.

*Note: I need help to write out this problem in C++ while using Visual Studio 2017

//code

#include <iomanip>
#include <iostream>

class dateType
{
public:
   //custom methods
   void printDate() const;

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

   //accessors
   int getDay() const;
   int getMonth() const;
   int getYear() const;

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

   //destructor
   ~dateType();
private:
   int dMonth;
   int dDay;
   int dYear;
};

int main()
{

}

//custom methods
void dateType::printDate() const
{
   cout << setfill('0');
   cout << setw(2) << dMonth << '-' << setw(2) << dDay << '-' << dYear;

}


dateType::dateType(int month, int day, int year)
{
   dMonth = month;
   dDay = day;
   dYear = year;
}


dateType::~dateType()
{
   dMonth = 1;
   dDay = 1;
   dYear = 1900;
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

The c++ code for your assignment is given below. All the methods have been defined to validate date and to check leap year.

*******************************date.cpp*****************************

#include <iomanip>
#include <iostream>
using namespace std;
class dateType
{
public:

   bool isLeapYear(int year);

   bool validateDate(int month,int day,int year);

//custom methods
void printDate() const;

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

//accessors
int getDay() const;
int getMonth() const;
int getYear() const;

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

//destructor
~dateType();
private:
int dMonth;
int dDay;
int dYear;
};

int main()
{
   //this main function tests all methods

   //creating object with invalid date
   dateType d(2,29,2014);
   d.printDate();

   //set new date
   d.setDate(2,29,2016);
   d.printDate();
  
d.setDate(2,29,2017);
   d.printDate();
  
d.setDate(13,32,2016);
   d.printDate();
d.setDate(12,31,2018);
   d.printDate();
  
d.setDate(11,31,2018);
   d.printDate();
}

//custom methods
void dateType::printDate() const
{
cout << setfill('0');
cout << setw(2) << dMonth << '-' << setw(2) << dDay << '-' << dYear;

}


dateType::dateType(int month, int day, int year)
{
   if(validateDate(month,day,year)){
dMonth = month;
dDay = day;
dYear = year;
}
   else{
       cout<<month<<"-"<<day<<"-"<<year<<" is invalid date. So setting default date to 1/1/1900"<<endl;
       dMonth = 1;
       dDay = 1;
       dYear = 1900;
   }
}
//set date
void dateType::setDate(int m,int d, int y){
   if(validateDate(m,d,y)){

   dMonth = m;
   dDay = d;
   dYear = y;
   cout<<"\nDate Set.."<<endl;
   }
       else{
cout<<endl;
           cout<<m<<"-"<<d<<"-"<<y<<" is invalid date. Can't set it"<<endl;
       }
}
//check if leap year
bool dateType::isLeapYear(int y){

   return ((( y% 4 == 0) && (y % 100 != 0)) ||(y % 400 == 0));
   }
//validate date
bool dateType::validateDate(int m,int d,int y){
   if(m>12 || m<1 || y<1900){
       return false;
   }
   if(m==2){
       if(this->isLeapYear(y)){
           if(d<=29)
               return true;
           else
               return false;
       }
       else{
           if(d<=28)
               return true;
           else
               return false;
       }
   }
   else{
       if(m==4||m==6||m==9||m==11){
           if(d<=30)
               return true;
           else
               return false;
       }
       else{
           if(d<=31)
               return true;
           else
               return false;
       }
   }
}

dateType::~dateType()
{
dMonth = 1;
dDay = 1;
dYear = 1900;
}

Add a comment
Know the answer?
Add Answer to:
The class dateType is designed to implement the date in a program, but the member function...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • #include "stdafx.h" #include <iostream> using namespace std; class dateType {    private:        int dmonth;...

    #include "stdafx.h" #include <iostream> using namespace std; class dateType {    private:        int dmonth;        int dday;        int dyear;       public:       void setdate (int month, int day, int year);    int getday()const;    int getmonth()const;    int getyear()const;    int printdate()const;    bool isleapyear(int year);    dateType (int month=0, int day=0, int year=0); }; void dateType::setdate(int month, int day, int year) {    int numofdays;    if (year<=2008)    {    dyear=year;...

  • Programming Exercise 11-8 PROBLEM:The class dateType defined in Programming Exercise 6 prints the date in numerical...

    Programming Exercise 11-8 PROBLEM:The class dateType defined in Programming Exercise 6 prints the date in numerical form. Some applications might require the date to be printed in another form, such as March 24, 2019. Derive the class extDateType so that the date can be printed in either form. Add a member variable to the class extDateType so that the month can also be stored in string form. Add a member function to output the month in the string format, followed...

  • Can someone please help me. i keep getting an error in my code. I need it...

    Can someone please help me. i keep getting an error in my code. I need it to be on three seperate files! attached is my assignment and my code. c++ The class date Type implements the date in a program. Write the class functions as specified in the UML class diagram. The values for the month, day, and year should be validated (in the class functions) before storing the date into the data members. Set the invalid member data to...

  • (IN JAVA ) Given the class Date that can prints the date in numerical form. Some...

    (IN JAVA ) Given the class Date that can prints the date in numerical form. Some applications might require the date to be printed in another form, such as March 24, 2005. Please do the following: Derive the class ExtDate so that the date can be printed either form. Add a data member to the class ExtDate so that the month can also be stored in string form. Add a method to output the month in the string format followed...

  • Hello, I have a bug in my code, and when I run it should ask the...

    Hello, I have a bug in my code, and when I run it should ask the user to enter the month and then the year and then print out the calendar for the chosen month and year. My problem with my code is that when I run it and I enter the month, it doesn't ask me for the year and it prints all the years of the month I chose. Please help! Code: #include "calendarType.h" #include <iostream> using namespace...

  • C++ problem 11-6 In Programming Exercise 2, the class dateType was designed and implemented to keep...

    C++ problem 11-6 In Programming Exercise 2, the class dateType was designed and implemented to keep track of a date, but it has very limited operations. Redefine the class dateType so that it can perform the following operations on a date, in addition to the operations already defined: Set the month. Set the day. Set the year. Return the month. Return the day. Return the year. Test whether the year is a leap year. Return the number of days in...

  • C++ Programming Step 1. Design a new ADT that implements a class named Date, add 3 private member variables month, day, year along with their appropriate public constructor / getter / setter member fu...

    C++ Programming Step 1. Design a new ADT that implements a class named Date, add 3 private member variables month, day, year along with their appropriate public constructor / getter / setter member functions inside Date.h and Date.cpp. Step 2. Design another ADT that implements a class named Watch, add 3 private member variables hour, minute, second, along with their appropriate public constructor / getter / setter member functions inside Watch.h and Watch.cpp. Step 3. Next, implement the additional SmartWatch...

  • 1.) Add a data member, currentTime into the DataType.h 2.) Add a member function, GetCurrentTime(). Modify...

    1.) Add a data member, currentTime into the DataType.h 2.) Add a member function, GetCurrentTime(). Modify the initialize function. ESIGN PAGE LAYOUT REFERENCES an-E4_]Ka"!Aa.le' | =.=-'E- 恒栏1순↓ | T Font Paragraph // To declare a class for the Date ADT // This is the header file DateType.h class DateType public: void Initialize (int newMonth, int newDay, int newYear) int GetYear) const; int GetMonth() const int GetDay) const private: int year; int month; int day; search

  • The following program contains the definition of a class called List, a class called Date and...

    The following program contains the definition of a class called List, a class called Date and a main program. Create a template out of the List class so that it can contain not just integers which is how it is now, but any data type, including user defined data types, such as objects of Date class. The main program is given so that you will know what to test your template with. It first creates a List of integers and...

  • This is for my c++ class and im stuck. Complete the Multiplex.cpp file with definitions for a fun...

    This is for my c++ class and im stuck. Complete the Multiplex.cpp file with definitions for a function and three overloaded operators. You don't need to change anything in the Multiplex.h file or the main.cpp, though if you want to change the names of the movies or concession stands set up in main, that's fine. Project Description: A Multiplex is a complex with multiple movie theater screens and a variety of concession stands. It includes two vectors: screenings holds pointers...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT