//构造函数和类同名,无返回值,参数可有可无。
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
using namespace std;class test
{
public:test(){m_a = 0; m_b = 0;cout << "无参构造函数" << endl;}test(int a,int b){m_a = a;m_b = b;cout << "有参构造函数" << endl;}test(int a){m_a = a;m_b = 0;cout << "有1个参数的构造函数" << endl;}//赋值构造函数(copy构造函数)test(const test& obj){cout << "赋值构造函数" << endl;}~test(){}
private:int m_a;int m_b;
};
int main()
{test t1;//调用无参构造函数//test t1();//err 不能这么写//调用有参构造函数//1)括号法test t2(1,2);//2)等号法 //c++对=功能增强test t3 = (3,4);//逗号表达式:最后一个值是该表达式的值test t4 = 5;//3)直接调用构造函数test t5 = test(5,6);//将产生一个匿名对象 去和留return 0;
}