定义一个带重载构造函数的日期类 代码参考

 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 }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值