In: Computer Science
This is what I have so far.
The out print I am looking for is
Date is 7/22/2020
New month: 9
New day: 5
New Year: 2020
Date is 9/5/2020
#include
class Date {
public:
Date::Date(int dateMonth, int
dateDay, int dateYear)
month{dateMonth}, day{dateDay}, int
dateYear}{}
//void displayDate();
//set month
void Date::setMonth(std::int
dateMonth){
month =
dateMonth;
}
//retrieve month
std:: string getMonth() {
return
month;
}
//set day
void setDay(std::int
dateDay){
day =
dateDay;
}
//Retrieve day
std:: string getDay() {
return
day;
}
;
//set year
void setYear(std::int
dateYear){
year =
dateYear;
}
//retrieve year
std:: int getYear() {
return
year;
}
private:
std::int day;
std::int month;
std::int year;
};
#include <iostream>
#include<string>
#include "Date.h"
using namespace std;
int main() {
Date myMonth{};
cout<<"Date is: " ;
cout<<"\nNew month: ";
string theMonth;
getline(cin, theMonth);
myMonth.setMonth(theMonth);
Date myDay;
cout<<"New day: ";
string theDay;
getline(cin, theDay);
myDay.setDay(theDay);
Date myYear;
cout<<"New year: ";
string theYear;
getline(cin, theYear);
myYear.setYear(theYear);
cout<<"Date is: "<< theMonth <<"/"
<<theDay<< "/"<< theYear;
}
Date.h
#ifndef DATE_H
#define DATE_H
#include<iostream>
class Date {
public:
Date(){day=month=year=0;};
Date(int dateMonth, int dateDay, int dateYear){
month = dateMonth;
day = dateDay;
year = dateYear;
}
//display date
void displayDate(){
std::cout<<"Date is "<<month<<"/"<<day<<"/"<<year<<std::endl;
}
//set month
void setMonth(int dateMonth){
month = dateMonth;
}
//retrieve month
int getMonth() {
return month;
}
//set day
void setDay(int dateDay){
day = dateDay;
}
//Retrieve day
int getDay() {
return day;
}
;
//set year
void setYear(int dateYear){
year = dateYear;
}
//retrieve year
int getYear() {
return year;
}
private:
int day;
int month;
int year;
};
#endif
mainDriver.cpp
#include <iostream>
#include<string>
#include "Date.h"
using namespace std;
int main() {
Date oldDate(7,22,2020);
oldDate.displayDate() ;
Date newDate;
cout<<"New month: ";
int theMonth;
cin >> theMonth;
newDate.setMonth(theMonth);
cout<<"New day: ";
int theDay;
cin >> theDay;
newDate.setDay(theDay);
cout<<"New year: ";
int theYear;
cin >> theYear;
newDate.setYear(theYear);
newDate.displayDate();
}
How to Test and Run:(Unix or Linux machine)
Put both files (Date.h and mainDriver.cpp) in the same folder or directory. Go to that directory in the terminal and run below command one by one.
g++ mainDriver.cpp
./a.out
On Window, you can compile and run the file mainDriver.cpp in any c++ compiler or IDE after putting both the files in the same directory.
Output:
Windows Linux