定义一个带重载构造函数的日期类 代码参考
程序员文章站
2022-06-22 09:22:04
1 #include 2 3 using namespace std; 4 5 class Date 6 { 7 private: 8 int year; 9 int month; 10 int day; 11 public: 12 Date(){year=1900;month ......
1 #include <iostream> 2 3 using namespace std; 4 5 class date 6 { 7 private: 8 int year; 9 int month; 10 int day; 11 public: 12 date(){year=1900;month=1;day=1;} 13 date(int,int,int); 14 void show(); 15 void init(int,int,int); 16 }; 17 18 date::date(int a, int b, int c) 19 { 20 year=a; 21 month=b; 22 day=c; 23 } 24 25 void date::show() 26 { 27 cout<<year<<'-'<<month<<'-'<<day<<endl; 28 return; 29 } 30 31 void date::init(int a, int b, int c) 32 { 33 year=a; 34 month=b; 35 day=c; 36 return; 37 } 38 39 int main() 40 { 41 date d1,d2(2100,12,12); 42 int a,b,c; 43 cin>>a>>b>>c; 44 d1.show(); 45 d2.show(); 46 d1.init(a,b,c); 47 d1.show(); 48 return 0; 49 }