用带参数的构造函数给类中的对象赋初值
#include <iostream>using namespace std;
class Time
{
public:
Time(int, int, int);
void set_time();
void show_time();
public:
int houre;
int minute;
int sec;
};
Time::Time(int x,int y,int z)
{
houre = x;
minute = y;
sec = z;
}
void Time::set_time()
{
cin >> houre;
cin >> minute;
cin >> sec;
}
void Time::show_time()
{
cout << houre << ":" << minute << ":" << sec << endl;
};
int main()
{
Time t1(120,120,20);
t1.set_time();
t1.show_time();
Time t2(10,10,10);
t2.show_time();
return 0;
}
为什么t1.set_time();输入的数据和t1.show_time();输出的数据不一样 本帖最后由 IChoose 于 2019-5-25 18:58 编辑
一致的呀
你是想输出120,120,20吗,
页:
[1]