类的构造函数类型
使用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-&gt;a = a;
    this->b = a;
    this->c = a;
    cout << a << endl;
    cout << b << endl;
    cout << c << endl;
}

int main()
{
    rlxy ss(10, 20, 30);
}

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注