/*
C++提供了初始化列表语法,用来初始化属性
语法:构造函数():属性1(初值1),属性2(初值2),…{}
*/
#include "pch.h"
#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;
};
void test01() {
person p1(10, 20, 30);
cout << "m_A:" << p1.m_A << endl;
cout << "m_B:" << p1.m_B << endl;
cout << "m_C:" << p1.m_C << endl;
}
//初始化列表初始化属性
class person {
public:
//初始化列表 赋初值
/*person(): m_A(10), m_B (20), m_C (30)
{
}*/
//和上面那种相比,这种更灵活
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;
};
void test02() {
person p1(10,20,30);
cout << "m_A:" << p1.m_A << endl;
cout << "m_B:" << p1.m_B << endl;
cout << "m_C:" << p1.m_C << endl;
}
int main()
{
//test01();
test02();
}