logo
  • Home
  • About
  • Training
  • Programming
  • CS
  • IT
  • IS
  • ECE
  • EEE
  • EE
  • Civil
  • Mechanical
  • Chemical
  • Metallurgy
  • Instrumentation
  • Aeronautical
  • Aerospace
  • Biotechnology
  • Agriculture
  • MCA
  • BCA
  • Internship
  • Contact

Questions & Answers

C Interview Questions
C++ Questions
Linux MCQs
C# Quiz
Java MCQs
JavaScript MCQs
SAN Questions
PHP Questions
Python Quiz

Computer Science Questions

Operating System Quiz
Computer Architecture MCQs
Software Architecture MCQs
Software Engineering MCQs
Artificial Intelligence MCQs
LISP Programming MCQs
Database Management MCQs
Computer Network MCQs
Microprocessor MCQs

C Programming Examples

Simple C Programs
C - Arrays
C - Matrix
C - Strings
C - Bitwise Operations
C - Linked Lists
C - Stacks & Queues
C - Searching & Sorting
C - Trees
C - Strings
C - File Handling
C - Mathematical Functions
C - Puzzles & Games
C Programs - Recursion
C Programs - No Recursion

Java Algorithms

Java - Numerical Problems
Java - Combinatorial Problems
Java - Graph Problems
Java - Hard Graph Problems
Java - Computation Geometry
Java - Sets & Strings
Java - Data-Structures
Java - Collection API Problems

C++ Algorithms

C++ - Numerical Problems
C++ - Combinatorial Problems
C++ - Graph Problems
C++ - Hard Graph Problems
C++ - Computation Geometry
C++ - Sets & Strings
C++ - Data-Structures
C++ - STL Library

C Algorithms

C - Numerical Problems
C - Combinatorial Problems
C - Graph Problems
C - Hard Graph Problems
C - Computation Geometry
C - Sets & Strings
C - Data-Structures

« Prev Page
Next Page »

C++ Program to Implement a Class Date

Posted on October 8, 2013 by Manish
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.

  1. /*
  2.  * C++ Program to implement Class Date
  3.  */
  4. #include <iostream>
  5. #include <ctime>
  6.  
  7. std::string months[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
  8.                         "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
  9. std::string days[] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri",
  10.                       "Sat"};
  11.  
  12. class Date{
  13.     // Private Members
  14.     private:
  15.         std::string month;
  16.         std::string day;
  17.         int date;
  18.         int year;
  19.     // Public Members
  20.     public:
  21.         // Default Constructor
  22.         Date() { 
  23.                 const int BASE_YEAR = 1900;
  24.                 time_t timer;
  25.                 tm * time;
  26.                 std::time(&timer);
  27.                 time = localtime(&timer);
  28.                 date = time->tm_mday;
  29.                 month = months[time->tm_mon];
  30.                 day = days[time->tm_wday];
  31.                 year = time->tm_year + BASE_YEAR;
  32.         }
  33.         void printDate(void) { 
  34.             std::cout << "Current date " 
  35.                       << this->month << " " << this->day << " "
  36.                       << this->date  << " " << this->year;
  37.         }
  38.         // Destructor
  39.         ~Date() {}
  40. };
  41.  
  42. int main()
  43. {
  44.     Date d;
  45.  
  46.     d.printDate();
  47. }

$ a.out
Current date Oct Mon 7 2013

Sanfoundry Global Education & Learning Series – 1000 C++ Programs.

If you wish to look at all C++ Programming examples, go to C++ Programs.
« Prev Page - C++ Program to Demonstrate do-while Loop
» Next Page - C++ Program to Multiply Numbers Without * Operator
« C# Program to Implement Namespaces
C++ Program to Multiply Numbers Without * Operator »

Deep Dive @ Sanfoundry:

  1. Java Programming Examples on Collection API
  2. C# Programming Examples on Interfaces
  3. Python Programming Examples
  4. Internships – Engineering, Technology and Core Science Internships
  5. C# Programming Examples on Files
  6. Course Registration – 2
  7. Simple C Programs
  8. C# Programming Examples on Strings
  9. Recruitment Support Service – Employers
  10. C# Basic Programming Examples
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer and SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage & Cluster Administration, Advanced C Programming, SAN Storage Technologies, SCSI Internals and Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him below:
LinkedIn | Facebook | Twitter | Google+

Best Careers

Developer Tracks
SAN Developer
Linux Kernel Developer
Linux Driver Developer
Linux Network Developer

Live Training Photos
Mentoring
Software Productivity
GDB Assignment
Sanfoundry is No. 1 choice for Deep Hands-ON Trainings in SAN, Linux & C, Kernel Programming. Our Founder has trained employees of almost all Top Companies in India such as VMware, Citrix, Oracle, Motorola, Ericsson, Aricent, HP, Intuit, Microsoft, Cisco, SAP Labs, Siemens, Symantec, Redhat, Chelsio, Cavium, ST-Micro, Samsung, LG-Soft, Wipro, TCS, HCL, IBM, Accenture, HSBC, Mphasis, Tata-Elxsi, Tata VSNL, Mindtree, Cognizant and Startups.

Best Trainings

SAN I - Technology
SAN II - Admin
Linux Fundamentals
Advanced C Training
Linux-C Debugging
System Programming
Network Programming
Linux Threads
Kernel Programming
Kernel Debugging
Linux Device Drivers

Best Reference Books

Computer Science Books
Algorithm & Programming Books
Electronics Engineering Books
Electrical Engineering Books
Chemical Engineering Books
Civil Engineering Books
Mechanical Engineering Books
Industrial Engineering Books
Instrumentation Engg Books
Metallurgical Engineering Books
All Stream Best Books

Questions and Answers

1000 C Questions & Answers
1000 C++ Questions & Answers
1000 C# Questions & Answers
1000 Java Questions & Answers
1000 Linux Questions & Answers
1000 Python Questions
1000 PHP Questions & Answers
1000 Hadoop Questions
Cloud Computing Questions
Computer Science Questions
All Stream Questions & Answers

India Internships

Computer Science Internships
Instrumentation Internships
Electronics Internships
Electrical Internships
Mechanical Internships
Industrial Internships
Systems Internships
Chemical Internships
Civil Internships
IT Internships
All Stream Internships

About Sanfoundry

About Us
Copyright
TOS & Privacy
Jobs
Bangalore Training
Online Training
SAN Training
Developers Track
Mentoring Sessions
Contact Us
Sitemap
© 2011 Sanfoundry