第五周 阅读程序(2)

来源:互联网 发布:北京大学大数据 招聘 编辑:程序博客网 时间:2024/05/25 08:14

问题描述:

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

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

[cpp] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. #include<iostream>  
  2. using namespace std;  
  3. class CE  
  4. {  
  5. private:  
  6.     int a,b;  
  7.     int getmin(){return (a<b? a:b);}  
  8. public:  
  9.     int c;  
  10.     void SetValue(int x1,int x2, int x3)  
  11.     {  
  12.         a=x1;  
  13.         b=x2;  
  14.         c=x3;  
  15.     }  
  16.     int GetMin();  
  17. };  
  18.    
  19. int CE::GetMin()  
  20. {  
  21.     int d=getmin();  
  22.     return (d<c? d:c);  
  23. }  
  24.    
  25. int main()  
  26. {  
  27.     int x=5,y=12,z=8;  
  28.     CE *ep;  
  29.     ep=new CE;  
  30.     ep->SetValue(x+y,y-z,10);  
  31.     cout<<ep->GetMin()<<endl;  
  32.     CE a=*ep;  
  33.     cout<<a.GetMin()*3+15<<endl;  
  34.     return 0;  
理想运行结果:4(换行)27

实际运行结果:


0 0