This is also the solution to this assignment you can download this file.
Here 3 files are created date.hpp, date.cpp, and main.cpp:
date.hpp
#include <string>
#include <ctime>
using namespace std;
#ifndef DATE_H // This definition protect the header against
#define DATE_H // multiple inclusions.
class Date // date class
{
private: // Data fields
int day = 0; //day
int month = 0; //month
int year = 0; // year
public:
// constructor
Date();
// constructor for seting value at declartion time in d/m/y form.
Date(int d, int m, int y);
// data seting mathod for Date to d/m/y.
void DateSet(int d, int m, int y);
int DateDay(); // accessing mathod for Day.
int DateMonth(); // accessing mathod for Month.
int DateYear(); // accessing mathod for Year.
string ToString(); // returns string form of date.
void DatePrint(); // prints Date.
};
Date DateNow(); // accessing todays date.
#endif
#ifndef DATE_H
#define DATE_H
// our code.
#endif
Using In this method we can protect the header against multiple inclusions.
date.hpp file only contain declarations.
Declarations in Date class
class Date // date class
{
private: // Data fields
int day = 0; //day
int month = 0; //month
int year = 0; // year
public:
// constructor
Date();
// constructor for seting value at declartion time in d/m/y form.
Date(int d, int m, int y);
// data seting mathod for Date to d/m/y.
void DateSet(int d, int m, int y);
int DateDay(); // accessing mathod for Day.
int DateMonth(); // accessing mathod for Month.
int DateYear(); // accessing mathod for Year.
string ToString(); // returns string form of date.
void DatePrint(); // prints Date.
};
We declare a function DateNow() out of class
Date DateNow(); // accessing todays date.
date.cpp
#include <iostream>
#include <string>
#include <ctime> // header for accessing current time.
#include "date.hpp"
using namespace std;
//All implimentation of "date.hpp" is here:
// function to convert date in to string form
string Date::ToString()
{
string s = "";
s += to_string(day);
s += '/' + to_string(month);
s += '/' + to_string(year);
return s;
}
// Default constructor.
Date::Date()
{
DateSet(0, 0, 0);
}
// constructor for setting value at declartion time.
Date::Date(int d, int m, int y)
{
DateSet(d, m, y);
}
// method for setting value of date object.
void Date::DateSet(int d, int m, int y)
{
day = d;
month = m;
year = y;
}
// mathod for access day.
int Date::DateDay()
{
return day;
}
// mathod for access month.
int Date::DateMonth()
{
return month;
}
// mathod for access year.
int Date::DateYear()
{
return year;
}
// mathod for printing date.
void Date::DatePrint()
{
cout << ToString() << endl;
}
// function for creating date object of today's date.
Date DateNow()
{
// Default value.
Date d(1, 1, 1970);
time_t t = time(0L); // Stores the current time in t.
if (t == (time_t)(-1))
{
return d;
}
tm *tp; // In C this would have been struct tm
tp = localtime(&t); // Turns t into a tm structure
// setting date.
// Perversely, months are 0-11
// The tm structure uses 2-digit years
d.DateSet(tp->tm_mday, tp->tm_mon + 1, tp->tm_year + 1900);
// return object
return d;
}
All setter and getter methods of class Date are implemented here.
You can see comments on the code for details.
More articles on cpp
main.cpp
#include <iostream>
#include <string>
#include "date.hpp"
#include "date.cpp"
#include "date.hpp"
using namespace std;
// driver code
int main()
{
// testing constructor.
Date d(20, 6, 2021);// d object is created.
// testing setter method.
Date v;
v.DateSet(19, 6, 2021);
// testing of DatePrint();
cout << "\nd:";
d.DatePrint();
cout << "v:";
v.DatePrint();
// testing getter mathod.
cout << "\nValues of date v :" << endl;
cout << "v.day :" << v.DateDay() << endl;
cout << "v.month:" << v.DateMonth() << endl;
cout << "v.year :" << v.DateYear() << endl;
// testing ToString() mathod.
cout << "\nString form of date v :" << v.ToString() << endl;
// testing DateNow() function which gives todays date.
Date today = DateNow();
cout << "\nTodays Date :" << today.ToString() << endl;
return 0;
}
Testing
of all methods of an object, Date is done here.
Testing of all methods of an object, Date is done here.
Output
data.hpp
0 Comments
If you have any doubt let me know.