1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
| #include<iostream> using namespace std; class Time { private: int hour; int minute; int second; public: Time(int i1, int i2, int i3) :hour(i1), minute(i2), second(i3) { cout << "Time类有参构造函数" << endl; } Time() { cout << "Time类缺省构造函数" << endl; hour = minute = second = 0; } int Gethour() { return hour; } int Getminute() { return minute; } int Getsecond() { return second; } void settime(int i1, int i2, int i3) { hour = i1; minute = i2; second = i3; } void display() { cout << hour << ":" << minute << ":" << second << endl; return; } ~Time() { cout << "Time类析构函数" << endl; } }; class Date { private: int year; int month; int day; Time t; public: Date() { year = month = day = 0; cout << "Date类缺省构造函数" << endl;
} Date(int i1, int i2, int i3) :t({ 0, 0, 0 }), year(i1), month(i2), day(i3) { cout << "Date类有参构造函数" << endl; } void display(); void settime(int i1, int i2, int i3); ~Date() { cout << "Date类析构函数" << endl; } };
void Date::display() { cout << year << ":" << month << ":" << day << ":"; t.display(); return; } void Date::settime(int i1, int i2, int i3) { t.settime(i1,i2,i3); return; } int main() { Date d; d.display(); d.settime(1, 2, 3); d.display(); Date d1(4, 5, 6); d1.display(); return 0; }
|