第五周 阅读程序(3)

来源:互联网 发布:js event target属性 编辑:程序博客网 时间:2024/05/05 15:18

问题描述:

本周再补充三个和指针有关的阅读程序,进一步掌握指针工作的原理。
友情提醒:画出内存,让程序的分析,在理性、有序中完成。如果有时间的变化,博客中加个自己画的图。
可以在上机时通过单步执行,进一步和你在人脑中运行程序的过程进行对照。

(1) 阅读程序,写出程序的运行结果并理解

[cpp] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. #include <iostream>  
  2. using namespace std;  
  3. class Time  
  4. {  
  5. public:  
  6.     Time(int,int,int);  
  7.     void output_time( );  
  8.     int hour;  
  9.     int minute;  
  10.     int sec;  
  11. };  
  12.    
  13. Time::Time(int h,int m,int s)  
  14. {  
  15.     hour=h;  
  16.     minute=m;  
  17.     sec=s;  
  18. }  
  19.    
  20. void Time::output_time( )  
  21. {  
  22.     cout<<hour<<":";  
  23.     cout<<minute<<":" <<sec<<endl;  
  24. }  
  25.    
  26. int main( )  
  27. {  
  28.     Time t1(10,13,56);  
  29.     int *p1=&t1.hour; //指向数据成员的指针  
  30.     cout<<*p1<<endl;  
  31.     t1.output_time( );  
  32.   
  33.   
  34.     Time *p2=&t1; //指向对象的指针  
  35.     p2->output_time( );  
  36.   
  37.   
  38.     void (Time::*p3)( ); //指向成员函数的指针  
  39.     p3=&Time::output_time;  
  40.     (t1.*p3)( );  
  41.     return 0;  
  42. }  
理想运行结果:

10

10:13:56

10:13:56

10:13:56

运行结果:



0 0