//c++提供了初始化列表语法,用来初始化属性
//语法:构造函数():属性1(值1),属性2(2)...{}
普通传参赋值初始化
#include<iostream>
using namespace std;
class person{
public:
person(int a,int b,int c){
m_a=a;
m_b=b;
m_c=c;
}
int m_a;
int m_b;
int m_c;
};
int main(){
person p(10,20,30);
cout<<"m_a="<<p.m_a <<endl;
cout<<"m_b="<<p.m_b <<endl;
cout<<"m_c="<<p.m_c <<endl;
return 0;
}
初始化列表语法,初始化属性
#include<iostream>
using namespace std;
class person {
public:
person(int a,int b,int c):m_a(a),m_b(b),m_c(c){}
int m_a;
int m_b;
int m_c;
};
int main() {
person p(10, 20, 30);
cout << "m_a=" << p.m_a << endl;
cout << "m_b=" << p.m_b << endl;
cout << "m_c=" << p.m_c << endl;
return 0;
}