目录
1.作用
用来初始化属性
2.语法
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
class person
{
public:
int a;
int b;
int c;
person(): a(10),b(20),c(30)
{
}
};
int main(){
ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
person p;
cout << p.a << endl;
cout << p.b << endl;
cout << p.c << endl;
return 0;
}
更灵活的做法
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
class person
{
public:
int a;
int b;
int c;
person(int a,int b,int c): a(a),b(b),c(c)
{
}
};
int main(){
ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
person p(1,2,3);
cout << p.a << endl;
cout << p.b << endl;
cout << p.c << endl;
return 0;
}