第九周实践——阅读程序(2)

  1. /* 
  2. *Copyright (c) 2016,烟台大学计算机学院 
  3. *All rights reserved. 
  4. *文件名称 : 
  5. *作    者 : 徐聪
  6. *完成日期 : 2016年4月28号 
  7. *版 本 号 : v1.0 
  8. *问题描述 :  阅读程序,写出的程序的运行结果并理解
  9. *输入描述 :   无    
  10. *程序输出 :    
  11. */    
  12. #include <iostream>
    using namespace std;
    class MyClass
    {
    public:
        MyClass(int x=0):i(x){cout<<"C"<<i;}
        ~MyClass(){cout<<"D"<<i;}
        void SetValue(int val){i=val;}
        int GetVal(){return i;}
    private:
        int i;
    };
    int main()
    {
        MyClass *p[3];
        int i;
        for (i=0; i<3; i++)
        {
            p[i]=new MyClass(i);
            p[i]->SetValue(p[i]->GetVal()*2);
        }
        for (i=0; i<3; i++)
            delete p[i];
        cout<<endl;
        return 0;
    }
    
    运行结果:
  13. 第九周实践——阅读程序(2)_第1张图片

你可能感兴趣的:(第九周实践——阅读程序(2))