Write a C++ program that has a class called Date with the following declaration:
ID: 656702 • Letter: W
Question
Write a C++ program that has a class called Date with the following declaration:
class Date
{
private:
int month;
int day;
int year;
public:
Date();
int getMonth();
int getDay();
int getYear();
void setMonth(int);
void setDay(int);
void setYear(int);
};
Write definition of constructor with no argument and set month and day to 1 and year to 2001. Remember that the constructor is the same as the class name.
So, it will be something like this:
Date::Date()
{
month = 1;
day = 1;
year = 2001;
}
Explanation / Answer
Answer
#include <iostream>
#include <cstdlib>
#include "Date.h"
using std::cout;
using std::endl;
using std::cin;
int main()
{
Date mdyDate( );
//display initial date
mdyDate.displayDate();
return 0;
}
----------------------------------------------------------------------------------------------------------------------------------
// Date.h file
#include <cstdio>
#include <cstdlib>
#include <iostream>
class Date
{
private:
int day;
int month;
int year;
public:
Date();
int getDay();
void setDay(int);
int getMonth();
void setMonth(int);
int getYear();
void setYear(int);
void displayDate(); // display function
};
-------------------------------------------------------------------------------------------------------------------------------
class definition
// Date.cpp file
#include <iostream>
using std::cout;
using std::endl;
#include "Date.h"
//initialization with constructor
Date::Date()
{
}
// set day function
void Date::setDay(int d)
{
day = d;
}
// set month function
void Date::setMonth(int m)
{
month = m;
}
// set year function
void Date::setYear(int y)
{
year = y;
}
// get day function
int Date::getDay()
{
return day;
}
// get month function
int Date::getMonth()
{
return month;
}
// get year function
int Date::getYear()
{
return year;
}
// display date function
void Date::displayDate()
{
cout << getMonth() << "/" << getDay() << "/" << getYear() << endl;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.