C++类中的变量和构造函数

来源:互联网 发布:怎么用网络看电视直播 编辑:程序博客网 时间:2024/06/05 06:21

今天看的一道题

#include"iostream"
using namespace std;
      class CCounter
      {
           int value;
        public:
           CCounter();
           CCounter(int val);
           ~CCounter();
      };
    class CExample
      {
                  int value;
          public:
                  CCounter car;
                  CExample();
                  CExample(int  val);
                  void  Display();
                  ~CExample();
      };
      CCounter::CCounter()
      {
          cout<<"ccounter Constructorl"<<endl;
          value=0;
      }
      CCounter::CCounter(int val)
   {
          cout<<"CCounter constructor2"<<endl;
          value=val;
      }
      CCounter::~CCounter()
      {
            cout<<"CCounter Destructor"<<endl;
      }
      CExample::CExample()
      {
          cout<<"CExample constructorl"<<endl;
            value=0;
      }
      CExample::CExample(int val)
        {
          cout<<"CExamPle Constructorl"<<endl;
           value=val;
        }
    void CExample::Display()
      {
          cout<<"value=" <<value<<endl;
      }
    CExample::~CExample()
      {
          cout<<"CExample Destructor"<<endl;
      }
      void main()
      {
          CExample  e(2);
          e.Display();
      }

输出结果是:

CCounter Constructor

CExample Constructor

Value=2

CExample Destmctor

CCounter Destructor

 

经过实验确实是这样:C++是先构造其变量,然后再进入构造函数

 

 

0 0
原创粉丝点击