文章目录
- 一、普通函数显示时间
- 二、使用类的成员函数显示时间
- 三、找出元素的最大值(使用类的成员函数)
一、普通函数显示时间
#include <iostream>
using namespace std;
class Time
{
public:
int hour;
int minute;
int sec;
};
void set_time(Time&);
void show_time(Time&);
int main()
{
Time t1;
set_time(t1);
show_time(t1);
Time t2;
set_time(t2);
show_time(t2);
return 0;
}
void set_time(Time &t)
{
cin >> t.hour;
cin >> t.minute;
cin >> t.sec;
}
void show_time(Time &t)
{
cout << t.hour << ":" << t.minute << ":" << t.sec << endl;
}
二、使用类的成员函数显示时间
#include <iostream>
using namespace std;
class Time
{
public:
void set_time();
void show_time();
private:
int hour;
int minute;
int sec;
};
int main()
{
Time t1;
t1.set_time();
t1.show_time();
Time t2;
t2.set_time();
t2.show_time();
return 0;
}
void Time::set_time()
{
cin >> hour;
cin >> minute;
cin >> sec;
}
void Time::show_time()
{
cout << hour << ":" << minute << ":" << sec << endl;
}
三、找出元素的最大值(使用类的成员函数)
#include <iostream>
using namespace std;
class Array_max
{
public:
void set_value();
void max_value();
void show_value();
private:
int array[10];
int max;
};
void Array_max::set_value()
{
int i;
for(i=0; i<10; i++)
cin >> array[i];
}
void Array_max::max_value()
{
int i;
max= array[0];
for(i=1; i<10; i++)
if(array[i]>max)
max= array[i];
}
void Array_max::show_value()
{
cout << "max=" << max;
}
int main()
{
Array_max arr;
arr.set_value();
arr.max_value();
arr.show_value();
return 0;
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)