欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

设计Weekday类 代码参考

程序员文章站 2022-03-25 16:25:30
1 #include 2 3 using namespace std; 4 5 class Weekday 6 { 7 private: 8 int num; 9 public: 10 void SetDay(){cin>>num;return;} 11 void IncDay ......
 1 #include <iostream>
 2 
 3 using namespace std;
 4 
 5 class weekday
 6 {
 7     private:
 8         int num;
 9     public:
10         void setday(){cin>>num;return;}
11         void incday(){num+=1;if(num>6) num-=7;return;}
12         void nowday()
13         {
14             switch(num)
15             {
16                 case 0:cout<<"星期日"<<endl;break;
17                 case 1:cout<<"星期一"<<endl;break;
18                 case 2:cout<<"星期二"<<endl;break;
19                 case 3:cout<<"星期三"<<endl;break;
20                 case 4:cout<<"星期四"<<endl;break;
21                 case 5:cout<<"星期五"<<endl;break;
22                 case 6:cout<<"星期六"<<endl;break;
23                 default:break;
24             }
25         }
26 };
27 
28 int main()
29 {
30     weekday one;
31     one.setday();
32     one.nowday();
33     one.incday();
34     one.nowday();
35     one.incday();
36     one.nowday();
37     return 0;
38 }