设置Cmytime类。
具有三个成员函数
构造函数
Set(int h,int m,int s)
Show()
输入 23 25 38
输出:
3:2:1
23:25:38
0:0:0
5:0:0
#include<iostream> using namespace std; class Cmytime { private: int hour,minute,second;//这里赋值平台是不给通过的 public: Cmytime(int h,int m,int s); Cmytime(); Cmytime(int h); int Set(int h,int m,int s); void Show(); }; Cmytime::Cmytime(int h,int m,int s)//注意,构造函数没有返回值 { hour=h; minute=m; second=s; } Cmytime::Cmytime()//最后赋值在构造函数里 { hour=0; minute=0; second=0; } Cmytime::Cmytime(int h)//最后赋值在构造函数里 { hour=h; minute=0; second=0; } int Cmytime::Set(int h,int m,int s) { hour=h; minute=m; second=s; return 1; } void Cmytime::Show() { cout << hour << ":" << minute << ":" << second; }
//StudybarCommentBegin
int main(void) {
int h,m,s;
cin>>h>>m>>s;
Cmytime t1(3,2,1),t2,t3(5);
t1.Show();
cout<<"\n";
t1.Set(h,m,s);
t1.Show();
cout<<"\n";
t2.Show();
cout<<"\n";
t3.Show();
return 0;
}
//StudybarCommentEnd
-END