这里我们将通过几个简单的例子来演示如何站在面向对象的角度设计程序,以及使用类的好处。
【例8.1】最简单的例子。
- #include <iostream>
- using namespace std;
- class Time //定义Time类
- {
- public : //数据成员为公用的
- int hour;
- int minute;
- int sec;
- };
- int main( )
- {
- Time t1;//定义t1为Time类对象
- cin>>t1.hour;//输入设定的时间
- cin>>t1.minute;
- cin>>t1.sec;
- //输出时间:
- cout<<t1.hour<<":"<<t1.minute<<":"<<t1.sec<<endl;
- return 0;
- }
运行情况如下:
几点注意:
1) 在引用数据成员hour,minute,sec时不要忘记在前面指定对象名。
2) 不要错写为类名,如写成
Time.hour,Time.minute,Time.sec
是不对的。因为类是一种抽象的数据类型,并不是一个实体,也不占存储空间,而对象是实际存在的实体,是占存储空间的,其数据成员是有值的,可以被引用的。
3) 如果删去主函数的3个输入语句,即不向这些数据成员赋值,则它们的值是不可预知的。
【例8.2】引用多个对象的成员。
1) 程序(a)
- #include <iostream>
- using namespace std;
- class Time
- {
- public :
- int hour;
- int minute;
- int sec;
- };
- int main( )
- {
- 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中数据成员的值
- return 0;
- }
运行情况如下:
程序是清晰易懂的,但是在主函数中对不同的对象一一写出有关操作,会使程序冗长。为了解决这个问题,可以使用函数来进行输入和输出。见程序(b)。
2) 程序(b)
- #include <iostream>
- using namespace std;
- class Time
- {
- public :
- int hour;
- int minute;
- int sec;
- };
- int main( )
- {
- void set_time(Time&);//函数声明
- void show_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对象中的数据
- return 0;
- }
- void set_time(Time& t) //定义函数set_time,形参t是引用变量
- {
- cin>>t.hour;//输入设定的时间
- cin>>t.minute;
- cin>>t.sec;
- }
- void show_time(Time& t) //定义函数show_time,形参t是引用变量
- {
- cout<<t.hour<<":"<<t.minute<<":"<<t.sec<<endl;//输出对象中的数据
- }
运行情况与程序(a)相同。
3) 程序(c)
可以对上面的程序作一些修改,数据成员的值不再由键盘输入,而在调用函数时由实参给出,并在函数中使用默认参数。将程序(b)第8行以下部分改为:
- int main( )
- {
- void set_time(Time&,int hour=0,int minute=0,int sec=0);//函数声明
- void show_time(Time&);//函数声明
- Time t1;
- set_time(t1,12,23,34);//通过实参传递时、分、秒的值
- show_time(t1);
- Time t2;
- set_time(t2);//使用默认的时、分、秒的值
- show_time(t2);
- return 0;
- }
- void set_time(Time& t,int hour,int minute,int sec)
- {
- t.hour=hour;
- t.minute=minute;
- t.sec=sec;
- }
- void show_time(Time& t)
- {
- cout<<t.hour<<":"<<t.minute<<":"<<t.sec<<endl;
- }
程序运行时的输出为:
以上两个程序中定义的类都只有数据成员,没有成员函数,这显然没有体现出使用类的优越性。在下面的例子中,类体中就包含了成员函数。
【例8.3】将例8.2的程序改用含成员函数的类来处理。
- #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
- 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的数据成员的值
- return 0;
- }
- void Time::set_time( ) //在类外定义set_time函数
- {
- cin>>hour;
- cin>>minute;
- cin>>sec;
- }
- void Time::show_time( ) //在类外定义show_time函数
- {
- cout<< hour<<":"<< minute<<":"<< sec<< endl;
- }
运行情况与例8.2中的程序(a)相同。
几点注意:
【例8.4】找出一个整型数组中的元素的最大值。这个问题可以不用类的方法来解决,现在用类来处理,读者可以比较不同方法的特点。
- #include <iostream>
- using namespace std;
- class Array_max //声明类
- {
- public : //以下3行为成员函数原型声明
- void set_value( ); //对数组元素设置值
- void max_value( ); //找出数组中的最大元素
- void show_value( ); //输出最大值
- private :
- int array[10]; //整型数组
- int max; //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 arrmax; //定义对象arrmax
- arrmax.set_value( ); //调用arrmax的set_value函数,向数组元素输入数值
- arrmax.max_value( ); //调用arrmax的max_value函数,找出数组元素中的最大值
- arrmax.show_value( ); //调用arrmax的show_value函数,输出数组元素中的最大值
- return 0;
- }
运行结果如下:
请注意成员函数定义与调用成员函数的关系,定义成员函数只是设计了一组操作代码,并未实际执行,只有在被调用时才真正地执行这一组操作。
可以看出: 主函数很简单,语句很少,只是调用有关对象的成员函数,去完成相应的操作。在大多数情况下,主函数中甚至不出现控制结构(判断结构和循环结构),而在成员函数中使用控制结构。在面向对象的程序设计中,最关键的工作是类的设计。所有的数据和对数据的操作都体现在类中。只要把类定义好,编写程序的工作就显得很简单了。