c++構造函數的調用方法


#include<iostream>
using namespace std;

class Base
{
public:

Base(){
cout<<"hello"<<endl;
}

Base (int _a ):base(_a){ // 將a賦值給base
base++;
cout << base << endl;
}

Base (int _a ,float _b):base(_a),th(_b){
cout << base + th << endl;
}

Base (int _a ,float _b ,int _c):base(_a),th(_b),xh(_c){
cout << base + th + xh<< endl;
}

void fun0(){cout << base << endl;}
int base;
float th;
int xh;
};

int main(){
Base b; // 調用默認構造函數
Base t(10); // 調用帶參的構造函數的簡寫形式
t.fun0();
t.base=100;
t.fun0();
Base t1 =Base(100,88.12); // 調用帶參的構造函數
Base t2 =(10,100.12,20); //
}

 注意:Base t2 =(10,100.12,20); 在烏班圖下輸出的結果為錯誤的。 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM