例8.1 最简单的例子。
#include <iostream>
usingnamespacestd;
classTime //定义Time类
{public: //数据成员为公用的
inthour;
intminute;
intsec;
};
intmain( )
{ Time t1; //定义t1为Time类对象
cin>>t1.hour; //输入设定的时间
cin>>t1.minute;
cin>>t1.sec;
cout<<t1.hour<<":"<<t1.minute<<":"<<t1.sec<<endl; //输出时间
return0;
}
例8.2 引用多个对象的成员。
(1)程序(a)
#include <iostream>
usingnamespacestd;
classTime
{public:
inthour;
intminute;
intsec;
};
intmain( )
{Time t1; //定义对象t1
cin>>t1.hour; //向t1的数据成员输入数据
cin>>t1.minute;
cin>>t1.sec;
cout<<t1.hour<<":"<<t1.minute<<":"<<t1.sec<<endl; //输出t1中数据成员的值
Time t2; //定义对象t2
cin>>t2.hour; //向t2的数据成员输入数据
cin>>t2.minute;
cin>>t2.sec;
cout<<t2.hour<<":"<<t2.minute<<":"<<t2.sec<<endl; //输出t2中数据成员的值
return0;
}
(2)程序(b)
#include <iostream>
usingnamespacestd;
classTime
{public:
inthour;
intminute;
intsec;
};
intmain( )
{
voidset_time(Time&); //函数声明
voidshow_time(Time&); //函数声明
Time t1; //定义t1为Time类对象
set_time(t1); //调用set_time函数,向t1对象中的数据成员输入数据
show_time(t1); //调用show_time函数,输出t1对象中的数据
Time t2; //定义t2为Time类对象
set_time(t2); //调用set_time函数,向t2对象中的数据成员输入数据
show_time(t2); //调用show_time函数,输出t2对象中的数据
return0;
}
voidset_time(Time& t) //定义函数set_time,形参t是引用变量
{
cin>>t.hour; //输入设定的时间
cin>>t.minute;
cin>>t.sec;
}
voidshow_time(Time& t) //定义函数show_time,形参t是引用变量
{
cout<<t.hour<<":"<<t.minute<<":"<<t.sec<<endl; //输出对象中的数据
}
(3)程序(c)
可以对上面的程序作一些修改,数据成员的值不再由键盘输入,而在调用函数时由实参给出,并在函数中使用默认参数。将程序(b)第8行以下的部分修改为
intmain( )
{
voidset_time(Time&,inthour=0,intminute=0,intsec=0); //函数声明
voidshow_time(Time&); //函数声明
Time t1;
set_time(t1,12,23,34); //通过实参传递时、分、秒的值
show_time(t1);
Time t2;
set_time(t2); //使用默认的时、分、秒的值
show_time(t2);
return0;
}
voidset_time(Time& t,inthour,intminute,intsec)
{
t.hour=hour;
t.minute=minute;
t.sec=sec;
}
voidshow_time(Time& t)
{
cout<<t.hour<<":"<<t.minute<<":"<<t.sec<<endl;
}
例8.3 将例8.2的程序改用含成员函数的类来处理。
#include <iostream>
usingnamespacestd;
classTime
{public:
voidset_time( ); //公用成员函数
voidshow_time( ); //公用成员函数
private: //数据成员为私有
inthour;
intminute;
intsec;
};
intmain( )
{
Time t1; //定义对象t1
t1.set_time( ); //调用对象t1的成员函数set_time,向t1的数据成员输入数据
t1.show_time( ); //调用对象t1的成员函数show_time,输出t1的数据成员的值
Time t2; //定义对象t2
t2.set_time( ); //调用对象t2的成员函数set_time,向t2的数据成员输入数据
t2.show_time( ); //调用对象t2的成员函数show_time,输出t2的数据成员的值
return0;
}
voidTime∷set_time( ) //在类外定义set_time函数
{
cin>>hour;
cin>>minute;
cin>>sec;
}
voidTime∷show_time( ) //在类外定义show_time函数
{
cout<<hour<<":"<<minute<<":"<<sec<<endl;
}
例8.4 找出一个整型数组中的元素的最大值。
这个问题可以不用类的方法来解决,现在用类来处理,读者可以比较不同方法的特点。
#include <iostream>
usingnamespacestd;
classArray_max //声明类
{public: //以下3行为成员函数原型声明
voidset_value( ); //对数组元素设置值
voidmax_value( ); //找出数组中的最大元素
voidshow_value( ); //输出最大值
private:
intarray[10]; //整型数组
intmax; //max用来存放最大值
};
voidArray_max∷set_value( ) //成员函数定义,向数组元素输入数值
{ inti;
for(i=0;i<10;i++)
cin>>array[i];
}
voidArray_max∷max_value( ) //成员函数定义,找数组元素中的最大值
{inti;
max=array[0];
for(i=1;i<10;i++)
if(array[i]>max) max=array[i];
}
voidArray_max∷show_value( ) //成员函数定义,输出最大值
{cout<<"max="<<max;}
intmain( )
{Array_max arrmax; //定义对象arrmax
arrmax.set_value( ); //调用arrmax的set_value函数,向数组元素输入数值
arrmax.max_value( ); //调用arrmax的max_value函数,找出数组元素中的最大值
arrmax.show_value( ); //调用arrmax的show_value函数,输出数组元素中的最大值
return0;
}