Write C++ program Date Class: Design and Implement a class called Date that has
ID: 655955 • Letter: W
Question
Write C++ program
Date Class:
Design and Implement a class called Date that has data members to store month (as a number), day, year, and name of the month. The class should have a three-parameter constructor that allows the data to be set at the time of new Data object instances are created. Default constructor that does not take any parameters should set the default values of 1 (month), 1 (day), 2001 (year). The class should have following three member functions to display date following formats
showDate1() should display the date in 1/1/2001 format
showDate2() should display the date in January 1, 2001 format
showDate3() should display the date in 1 January 2001 format
Also the class should have method to set the date (setDate()). This method should take month, day, and year as parameters and set the object instance data member values. Then your Date class should work with the Assignment5.cpp given below and produces the following output.
1/1/2001
February 12, 2010
29 August 1986
Assignment1.cpp
#include <iostream>
using namespace std;
#include "Date.h"
int main()
{
Date d1;
Date d2(2, 12, 2010);
d1.showDate1();
d2.showDate2();
d1.setDate(8, 29, 1986);
d1.showDate3();
system("pause");
return 0; }
Explanation / Answer
Date.h
class Date{
private:
int month; //variable to store month
int day; //variable to store day
int year; //variable to store year
char nameOfMonth[12][20]={"January","February","March","April","May","June","July","August","September","October","November","December"};//array to store name of months
public:
Date(){ //default constructor
month=1;
day=1;
year=2001;
}
Date(int month,int day,int year){ //parameterized constructor
this->month=month; //set the month of present object instance to value of month in argument
this->day=day; //set the day of present object instance to value of day in argument
this->year=year; //set the year of present object instance to value of year in argument
}
void setDate(int month,int day,int year){ //setter method
this->month=month; //set the month of present object instance to value of month in argument
this->day=day; //set the day of present object instance to value of day in argument
this->year=year; //set the year of present object instance to value of year in argument
}
void showDate1(){
cout<<" "<<month<<"/"<<day<<"/"<<year; //display date in 1/1/2001 format
}
void showDate2(){
cout<<" "<<nameOfMonth[month-1]<<" "<<day<<", "<<year; //display date in January 1, 2001 format
}
void showDate3(){
cout<<" "<<day<<" "<<nameOfMonth[month-1]<<" "<<year; //display date in 1 January 2001 format
}
};
-----------------------------------------------------------------------------------------------------------------------------------------------------
Assignment5.Cpp
#include <iostream>
#include<cstdlib> //for use of system() function
using namespace std;
#include "Date.h"
int main()
{
Date d1; //define a date object
Date d2(2, 12, 2010); //define a parameterized date object
d1.showDate1(); //call showDate1() function
d2.showDate2(); //call showDate2() function
d1.setDate(8, 29, 1986); //call setDate() function
d1.showDate3(); //call showDate3() function
system("pause"); //wait at the console for any key to be pressed
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.