In: Computer Science
In this programming assignment, you need to create 3 files.
1. DateType.h
2. DateType.cpp
3. A test driver for testing the class defined in the other 2 files. You can name your file in the way you like. Remember it must be a .cpp file.
In DateType.h file, type these lines:
// 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; };
// Class implementation // DateType.cpp
#include "DateType.h"
void DateType::Initialize(int newMonth, int newDay, int newYear)
{
year = newYear;
month = newMonth;
day = newDay; }
int DateType::GetMonth() const
{
return month;
}
In DateType.cpp file, type these lines:
int DateType::GetDay() const
{
return day;
}
int DateType::GetYear() const
{
return year; }
In you test driver, type in these lines:
// test driver
// I give the file name: testDriver.cpp
// To compile, type c++ DataType.cpp testDriver.cpp, this will
generate an a.out executable.
// Or, type c++ Type.cpp testDriver.cpp –o testdriver, this will
generate an executable named testdriver.
#include "DateType.h" #include <iostream>
using namespace std;
int main()
{
DateType today;
DateType anotherDay; today.Initialize(9, 24, 2003); anotherDay.Initialize(9, 25, 2003);
cout << "Today is " << today.GetMonth() << "/" << today.GetDay() << "/" << today.GetYear() << endl;
cout << "Anotherday is " << anotherDay.GetMonth() << "/" << anotherDay.GetDay() << "/" << anotherDay.GetYear() << endl;
return 0; }
Once you have the 3 files ready, you can compile them. The compile command is in the comments of the test driver.
After the compilation, you run the program, and see what output
you get.
Now, modify the code to initialize 2 different dates, compile and
run it, and see what the output is.
If you have any doubts, please give me comment...
DateType.h
// 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;
};
DateType.cpp
// Class implementation // DateType.cpp
#include "DateType.h"
void DateType::Initialize(int newMonth, int newDay, int newYear)
{
year = newYear;
month = newMonth;
day = newDay;
}
int DateType::GetMonth() const
{
return month;
}
int DateType::GetDay() const
{
return day;
}
int DateType::GetYear() const
{
return year;
}
testDriver.cpp
// To compile, type g++ DateType.cpp testDriver.cpp, this will generate an a.out executable.
// Or, type g++ DateType.cpp testDriver.cpp –o testdriver, this will generate an executable named testdriver.
#include <iostream>
#include "DateType.h"
using namespace std;
int main()
{
DateType today;
DateType anotherDay;
today.Initialize(3, 20, 2003);
anotherDay.Initialize(9, 13, 2006);
cout << "Today is " << today.GetMonth() << "/" << today.GetDay() << "/" << today.GetYear() << endl;
cout << "Anotherday is " << anotherDay.GetMonth() << "/" << anotherDay.GetDay() << "/" << anotherDay.GetYear() << endl;
return 0;
}
Ouput:
After changing dates in testDriver.cpp