Question

Design and implement a C++ class called Date that has the following private member variables month (int) day (nt) . year (int Add the following public member functions to the class. Default Constructor with all default parameters: The constructors should use the values of the month, day, and year arguments passed by the client program to set the month, day, and year member variables. The constructor should check if the values of the parameters are valid (that is day is between 1-31, month is between 1-12 and year is >0). If not, the constructor should use default values 1, 1, 2000 for month, day, and year respectively. . Accessors (getters): write one for each member variable month, day, and year. Mutators (setters): write one for each member variable month, day, and year. The mutators should check if the values passed are valid (that is day is between 1-31, month is between 1-12 and year is >0) and if not, do not update the member variable with an invalid value. . print1: this function should print the date in month/day/year format to the screen, where each month, day and year are ints. For example, 1/2/2012 print2: this function should print the date in month day, year format to the screen where day and year are ints and month is a string. For example, January 2, 2012 b) to create at least two different Date objects and call both print1 and print2 functions, etc. Demonstrate the use of Date class by writing code in your main function. Make sure
0 0
Add a comment Improve this question Transcribed image text
Answer #1


// C++ code
#include <iostream>
#include <stdlib.h>
using namespace std;


string months[] = {"January","February ","March","April","May","June","July","August","September","October","November","December"};
int days_in_months[] = {31,28,31,30,31,30,31,31,30,31,30,31};
class Date
{
private:
int month; //Data member
int day; // Data member
int year;
public:
Date(int m, int d, int y)
{
if(m<1 || m >12)
month = 1;
else
month = m;
if(d<1 || d >days_in_months[month-1])
day = 1;
else
day = d;
if(y<2001)
year = 2001;
else
year = y;
}

int getmonth()
{
return month;
}

int getyear()
{
return year;
}

int getday()
{
return day;
}

void setday(int d)
{
if(day > 0 && day <= 31)
day = d;
}

void setmonth(int m)
{
if(month > 0 && month <= 12)
month = m;
}

void setyear(int y)
{
if(year > 0)
year = y;
}

~Date(){}

void printDate1()
{
cout << this->month << "/" << this->day << "/" << this->year << "\n";
}
void printDate2()
{
cout << months[month-1] << " " << day << ", " << year << endl;
}
void printDate3()
{
cout <<day <<" " <<months[month-1] << " "<< year << endl;
}
};
int main(void)
{
int tempMonth = 0 ;
int tempDay = 0;
int tempYear = 0;
while(tempMonth > 12 || tempMonth < 1)
{
cout << "Input a month 1 through 12 and press enter: \n";
cin >> tempMonth;
}
while(tempDay > 31 || tempDay < 1)
{
cout << "Input a Day 1 through 31 and press enter: \n";
cin >> tempDay;
}
while(tempYear < 1)
{
cout << "Input a Year (assuming we do not count leap year): \n";
cin >> tempYear;
}
Date *d = new Date(tempMonth,tempDay,tempYear);

cout << endl;

d->printDate1();
cout << endl;
d->printDate2();
cout << endl;
d->printDate3();
cout << endl;

return 0;
}

Add a comment
Know the answer?
Add Answer to:
Design and implement a C++ class called Date that has the following private member variables month...
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
  • 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...

  • Please write the program in C++ Problem Description: Design a class called Date. The class should...

    Please write the program in C++ Problem Description: Design a class called Date. The class should store a date in three integers: month, day, and year.     Create the necessary set and get methods.       Design member functions to return the date as a string in the following formats:                                     12/25/2012                                     December 25, 2012                                     25 December 2012 Input Validation: Do not accept values for day greater than 30 or less than 1. Do not accept values for month...

  • Given the following date class interface: class date {private: int month;//1 - 12 int day;//1 -...

    Given the following date class interface: class date {private: int month;//1 - 12 int day;//1 - 28. 29. 30. 31 depending on month & year int year;//4-digit, e.g.. 2017 public: date();//Default constructor (investigate; find what it is used for)//Postcondition: the newly declared date object is initialized to 01/01/2000 date(int mm, int dd, int yyyy);//Second constructor//Postcondition: the newly declared data object is initialized to mm/dd/yyyy void setDate(int mm. int dd. int yyyy);//Postcondition: set the contents of the calling date object to...

  • Write a class called Book that has two private member variables called page (integer) and topic...

    Write a class called Book that has two private member variables called page (integer) and topic (string). It also has a static private member variable called count (integer). This class has only one constructor with default argument for page and topic. Write this constructor. Also overload the addition operator for this class such that it will add an integer value to the page variable of the book. For example when in main we say: book2 = book1 + 4; then...

  • //Please help me out with this problem.. This problem has to be done in C++ and...

    //Please help me out with this problem.. This problem has to be done in C++ and has to strictly follow the instructions... I have given the expected output of the problem below.   1) Design a class Date: Provide 3 instance variables (int) to store the month, the day number, and the year Provide the following functions Default constructor -sets date to, 1500 Constructor with parameters - if parameters are not valid, set like default constructor setDate if parameters are not...

  • C++ PROGRAMMING: Define a class called Date that has 3 private members that represent the month,...

    C++ PROGRAMMING: Define a class called Date that has 3 private members that represent the month, day, and year respectively. They will be represented as integers. Include a constructor and a public void function called print that prints out the date. (For example, if the month is 2, the day is 25, and the year is 1946, print provides as output 2/25/1946.) Include the class in a program that prints out the date 2/25/1946. Also, create another function that will...

  • Design a class named Month. The class should have the following private members:

    Design a class named Month. The class should have the following private members:   • name - A string object that holds the name of a month, such as "January", "February", etc.   • monthNumber - An integer variable that holds the number of the month. For example, January would be 1, February would be 2, etc. Valid values for this variable are 1 through 12.  In addition, provide the following member functions:• A default constructor that sets monthNumber to 1 and name...

  • You are to create a class Appointment.java that will have the following: 5 Instance variables: private...

    You are to create a class Appointment.java that will have the following: 5 Instance variables: private String month private int day private int year private int hour private int minute 1 default constructor public Appointment() 1 non-default constructor that accepts arguments for all instance variables, your constructor must call the setter methods below to set the values of the instance variables public Appointment(String monthPassed, int dayPassed, int yearPassed, int hourPassed, int minutePassed) 5 setter methods (one for each instance variable)...

  • Create a class named Date in C++ Class has 3 private data items (name the data...

    Create a class named Date in C++ Class has 3 private data items (name the data items month, day, year) int month int day int year Member functions ‘getters’   a getter for each data item Use ‘get’ and the data items name The data item name must be capitalized Return the data item Example: int getMonth()        {return month;} ‘setters’   a setter for each data item Use ‘set’ and the data items name The data item name must be capitalized Change...

  • Create a new project called Date In This new project you are going to design a...

    Create a new project called Date In This new project you are going to design a class of Date. This mean that you need to create functions within the class to validate the Date for example: is it a leap year? How many days a months has? How many months are? Create a simple main menu capable of entering a date and validating a date. Instructions • Create a class of Date with attribues and member functions (remember the use...

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
ADVERTISEMENT