zoukankan      html  css  js  c++  java
  • C++ Constructor

    // File: test.cpp
    // Author: lxw
    // Date: 2015-08-11
    
    #include <iostream>
    #include <string>
    #include <vector>
    #include <algorithm>
    #include <set>
    #include <climits>
    
    using namespace std;
    
    int number(){
        cout << "hello" << endl;
        return 11;
    }
    
    class Demo{
    private:
        int a = number();   //NOTE: [calls before constructor.] NOT ALWAYS call this.
        int b = number();
    public:
        Demo(){
            cout << "Constructor Demo()" << endl;
        }
        Demo(int A):a(A){
            cout << "Constructor Demo(int A)" << endl;
        }
        int getA(){
            return this->a;
        }
        int getB(){
            return this->b;
        }
    };
    
    int main(void){
        /*
        //NOTE: NOT "hello
    Constructor Demo(int A)" but "Constructor Demo(int A)"
        Demo d(3);  //Constructor Demo(int A)
        cout << d.getA() << endl; //3    
        cout << endl;
        Demo d1;  //hello
    Constructor Demo()
        cout << d1.getA() << endl;  //11
        */
        /*
        Output:
        Constructor Demo(int A)
        3
    
        hello
        Constructor Demo()
        11
        */
    
        Demo d(3);  //hello
    Constructor Demo(int A)
        cout << d.getA() << endl; //3
        cout << d.getB() << endl; //11
        cout << endl;
        Demo d1;  //hello
    hello
    Constructor Demo()
        cout << d1.getA() << endl;  //11
        cout << d1.getB() << endl;  //11
        /*
        Output:
        hello
        Constructor Demo(int A)
        3
        11
    
        hello
        hello
        Constructor Demo()
        11
        11
        */
        return 0;
    }
  • 相关阅读:
    Linux/Unix 新手和专家教程
    恢复Ext3下被删除的文件
    如何调试bash脚本
    8个实用而有趣Bash命令提示行
    使用grep恢复被删文件内容
    一些非常有意思的杂项资源
    chmod -x chmod的N种解法
    纯文本配置还是注册表
    面向对象的Shell脚本
    你可能不知道的Shell
  • 原文地址:https://www.cnblogs.com/lxw0109/p/cpp_constructor.html
Copyright © 2011-2022 走看看