- #include <iostream>
- using namespace std;
-
- class Test
- {
- public:
- Test()
- {
- m_a = 0;
- m_b = 0;
- cout<<"無參構(gòu)造函數(shù)"<<endl;
- }
- Test(int a)//3種方法
- {
- m_a = a;
- m_b = 0;
- cout<<"1個參數(shù)有參構(gòu)造函數(shù)"<<endl;
- }
- Test(int a, int b )//3種方法
- {
- m_a = a;
- m_b = b;
- cout<<"有參構(gòu)造函數(shù)"<<endl;
- }
- Test(const Test&obj)
- {
- cout<<"賦值構(gòu)造函數(shù)"<<endl;
- }
- void print()
- {
- cout<<m_a<<endl;
- cout<<"普通函數(shù)"<<endl;
- }
- protected:
- private:
- int m_a ;
- int m_b;
- };
- void display()
- {
- //1.調(diào)用有參構(gòu)造函數(shù)方法1
- //Test t(1,2);
- //t.print();
-
- //1.調(diào)用有參構(gòu)造函數(shù)方法2
- //Test t = (1,2,3);//逗號表達式 取最后一個值
- //t.print();
-
- //1.調(diào)用有參構(gòu)造函數(shù)方法3
- Test t = Test(1,2);//編譯器會產(chǎn)生一個匿名對象
- //t.print();
- }
- int main()
- {
- display();
- system("pause");
- return 0;
- }
|