构造函数模拟时钟的小程序,调试后无错误,但无法运行
#include<iostream>using namespace std;
class clock{
public:
clock(int newH,int newM,int newS);
void settime(int newH,int newM,int newS);
void showtime() ;
private:
int hour,minute,second;
};
clock::clock(int newH,int newM,int newS)
{
hour =newH;
minute = newM;
second = newS;
}
int main()
{
clock c(0,0,0);
c.showtime();
c.settime(8,30,30);
return 0;
} ??函数定义呢 class clock{
public:
clock(int newH,int newM,int newS);
void settime(int newH,int newM,int newS);
void showtime() ;
private:
int hour,minute,second;
};
clock::clock(int newH,int newM,int newS)
{
hour =newH;
minute = newM;
second = newS;
}qiuyouzhi 发表于 2021-3-17 18:06
??函数定义呢
这不是一个构造函数?且位于主函数之前,应该不用格外声明了吧? vaety 发表于 2021-3-19 10:08
这不是一个构造函数?且位于主函数之前,应该不用格外声明了吧?
是这些的定义
clock(int newH,int newM,int newS);
void settime(int newH,int newM,int newS);
void showtime() ; qiuyouzhi 发表于 2021-3-19 16:12
是这些的定义
找到了,谢谢。
贴一下正确代码#include<iostream>
using namespace std;
class clock{
public:
void settime(int newH=0,int newM=0,int newS=0);
void showtime() ;
private:
int hour,minute,second;
};
void clock::settime(int newH,int newM,int newS)
{
hour =newH;
minute = newM;
second = newS;
}
inline void clock::showtime()
{
cout<<hour<<":"<<minute<<":"<<second<<endl;
}
int main()
{
clock cad;
cout<<"first time set and output:"<<endl;
cad.settime();
cad.showtime();
cout<<"second time set and output:"<<endl;
cad.settime(8,30,30);
cad.showtime();
return 0;
页:
[1]