This C++ program implements a class – Date. The class uses time header contains a function time() which returns current calender time as an object of type time_t. We can extract various information from this struct by using -> with the pointer to the time structure.
Here is the source code of the C++ program implements a class – Date. The C++ program is successfully compiled and run on a Linux system. The program output is also shown below.
/*
* C++ Program to implement Class Date
*/
#include <iostream>
#include <ctime>
std::string months[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
std::string days[] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri",
"Sat"};
class Date{
// Private Members
private:
std::string month;
std::string day;
int date;
int year;
// Public Members
public:
// Default Constructor
Date() {
const int BASE_YEAR = 1900;
time_t timer;
tm * time;
std::time(&timer);
time = localtime(&timer);
date = time->tm_mday;
month = months[time->tm_mon];
day = days[time->tm_wday];
year = time->tm_year + BASE_YEAR;
}
void printDate(void) {
std::cout << "Current date "
<< this->month << " " << this->day << " "
<< this->date << " " << this->year;
}
// Destructor
~Date() {}
};
int main()
{
Date d;
d.printDate();
}
$ a.out Current date Oct Mon 7 2013
Sanfoundry Global Education & Learning Series – 1000 C++ Programs.
advertisement
advertisement
If you wish to look at all C++ Programming examples, go to C++ Programs.
Next Steps:
- Get Free Certificate of Merit in C++ Programming
- Participate in C++ Programming Certification Contest
- Become a Top Ranker in C++ Programming
- Take C++ Programming Tests
- Chapterwise Practice Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
- Chapterwise Mock Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
Related Posts:
- Apply for Computer Science Internship
- Practice Programming MCQs
- Buy Computer Science Books
- Practice Computer Science MCQs
- Buy Programming Books