C++基础 -10- 类的构造函数
类的构造函数类型一
使用this指针给类内参数赋值
class rlxy
{
public:
int a;
rlxy(int a, int b, int c)
{
this->a=a;
this->b=b;
this->c=c;
cout << "rlxy" << endl;
}
protected:
int b;
private:
int c;
};
int main()
{
rlxy ss(10, 20, 30);
}
类的构造函数类型二
使用参数列表给类内参数赋值
#include "iostream"
using namespace std;
class rlxy
{
public:
int a;
rlxy(int a, int b, int c) : a(a), b(b), c(c)
{
cout << a << endl;
cout << b << endl;
cout << c << endl;
}
protected:
int b;
private:
int c;
};
int main()
{
rlxy ss(10, 20, 30);
}
类的构造函数类型三
类外写构造函数
#include "iostream"
using namespace std;
class rlxy
{
public:
int a;
rlxy(int a, int b, int c);
protected:
int b;
private:
int c;
};
rlxy::rlxy(int a, int b, int c)
{
this->a = a;
this->b = a;
this->c = a;
cout << a << endl;
cout << b << endl;
cout << c << endl;
}
int main()
{
rlxy ss(10, 20, 30);
}