C ++ inherits the date and time of the Application Instance

Source: Internet
Author: User
C ++ inherits the date and time of the application instance-general Linux technology-Linux programming and kernel information. The following is a detailed description. This article provides a comprehensive example of inheritance. This example is used to compile a program for date (year, month, day) and time (hour, minute, second. This program creates three classes, one of which is the Date class, the other is the Time class, and the other is the DateTime class, which is the first two classes as the base class derived class.

The following is the source code of the program:
# Include
# Include
# Include
Typedef char string80 [80];

Class Date
{
Public:
Date (){}
Date (int y, int m, int d) {SetDate (y, m, d );}
Void SetDate (int y, int m, int d)
{
Year = y;
Month = m;
Day = d;
}
Void GetStringDate (string80 & Date)
{
Sprintf (Date, "% d/% d", Year, Month, Day );
}
Protected:
Int Year, Month, Day;
};

Class Time
{
Public:
Time (){}
Time (int h, int m, int s) {SetTime (h, m, s );}
Void SetTime (int h, int m, int s)
{
Hours = h;
Minutes = m;
Seconds = s;
}
Void GetStringTime (string80 & Time)
{
Sprintf (Time, "% d: % d", Hours, Minutes, Seconds );
}
Protected:
Int Hours, Minutes, Seconds;
};

Class TimeDate: public Date, public Time
{
Public:
TimeDate (): Date (){}
TimeDate (int y, int mo, int d, int h, int mi, int s): Date (y, mo, d), Time (h, mi, s ){}
Void GetStringDT (string80 & DTstr)
{
Sprintf (DTstr, "% d/% d; % d: % d", Year, Month, Day, Hours, Minutes, Seconds );
}
};

Void main ()
{
TimeDate date1, date2 (1998, 8, 12, 12, 45, 10 );
String80 DemoStr;
Date1.SetDate (1998, 8, 7 );
Date1.SetTime (10, 30, 45 );
Date1.GetStringDT (DemoStr );
Cout <"The date1 date and time is:" <
Date1.GetStringDate (DemoStr );
Cout <"The date1 date is:" <
Date1.GetStringTime (DemoStr );
Cout <"The date1 time is:" <
Date2.GetStringDT (DemoStr );
Cout <"The date2 date and time is:" <
}

In this program, the value of the data member of the object is the string of the data member obtained through the member function, and then output using the output statement.
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.