构造函数的分类:
无参构造函数
有参构造函数
赋值构造函数(拷贝构造函数)
#include<iostream>
using namespace std;
class test
{
public:
test()
{
m_a = 10;
}
test(int a, int b)
{
m_a = a;
m_b = b;
}
test(int a)
{
m_a = a;
m_b = 0;
}
public:
int getA()
{
return m_a;
}
int getB()
{
return m_b;
}
private:
int m_a;
int m_b;
};
void main()
{
//test t1; //调用无参构造函数
//cout <<"a="<<t1.getA() << endl; //输出为10
test t2(1,2);//1、括号法,调用有参构造函数
cout << "a1=" << t2.getA() << endl;//输出a=1,b=2
cout << "b1=" << t2.getB() << endl;
test t3 = (1, 3);//2,=号法 输出a=1,b=0
cout << "a2=" << t3.getA() << endl; //(1, 3)为逗号表达式,整个括号相当于逗号表达式最后一个数的值
cout << "b2=" << t3.getB() << endl;//所以调用只有一个参数的构造函数
test t4 = test(2, 8);//3、直接调用构造函数 手动调用构造函数
cout << "b3=" << t4.getA() << endl;//匿名对象
cout << "b3=" << t4.getB() << endl;
system("pause");
}