静态成员和函数要怎么改呀
#include<iostream>using namespace std;
class Time{
int hour;
int minute;
int second;
public:
Time(int h=0,int m=0,int s=0):hour(h),minute(m),second(s){cout<<"构造函数!"<<endl;}
Time& add();
void show(){cout<<hour<<":"<<minute<<":"<<second<<endl;}
~Time(){cout<<"析构函数!"<<endl;}
};
Time& Time::add(){
second++;
if(second>=60){
second-=60;
minute++;
if(minute>=60){
minute-=60;
hour=(hour+1)%24;
}
}
return (*this);
}
int main(){
Time time(11,59,58);
for(int i=0;i<4;i++)
{
time.add();
time.show();
}
return 0;
}
如何在类中增加静态数据成员和成员函数????求求大佬快速解决 万分感谢!!!! class Time{
int hour;
int minute;
int second;
//定义
static int _s;
public:
Time(int h=0,int m=0,int s=0):hour(h),minute(m),second(s){cout<<"构造函数!"<<endl;}
Time& add();
void show(){cout<<hour<<":"<<minute<<":"<<second<<endl;}
~Time(){cout<<"析构函数!"<<endl;}
static void _fun()
{
_s++;
cout << _s <<endl;
}
};
//声明
int Time::_s = 0;
Time& Time::add(){
second++;
if(second>=60){
second-=60;
minute++;
if(minute>=60){
minute-=60;
hour=(hour+1)%24;
}
}
return (*this);
}
int main(){
Time time(11,59,58);
for(int i=0;i<4;i++)
{
time.add();
time.show();
}
//使用
Time::_fun();
return 0;
}
xieglt 发表于 2020-12-9 16:54
考试的时候我怎么没想到这么搞。。。
页:
[1]