類的成員不允許設定初始值!
private:
int m_a = 10; //錯誤
=====================================================================
class Test
{
public:
//有參構造函數
Test(int a)
{
m_a = a;
}
//無參數構造函數
Test()
{
m_a = 0;
}
//四種應用場景
//賦值構造函數 copy構造函數
Test(const Test &obj)
{
}
public:
void print()
{
cout<<"m_a"<<m_a<<endl;
}
protected:
private:
int m_a;
};
void main()
{
//括號法
Test t1(10); //1 c++編譯器自動調用這個類的有參構造函數
t1.print();
//=
Test t2 = 20; //2 c++編譯器自動調用這個類的有參構造函數
t2.print();
//
Test t3 = Test(30);//3 程序員手工的調用構造函數 進行對象初始化
t3.print();
//new
Test *p = new Test(40);//3 new 關鍵字構造函數 進行對象初始化
t4.print();
delete p;
system("pause");
}