当前位置: 首页 > article >正文

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);
}


http://www.kler.cn/news/149303.html

相关文章:

  • Docker—共享应用程序
  • 20世纪30年代的大危机
  • c++环形队列
  • Linux grep命令
  • JavaWeb后端数据库MySQL的使用
  • 平凯星辰携手教育部教育管理信息中心,助力普惠教育数字化
  • Python与设计模式--桥梁模式
  • 互联网程序设计HTML+CSS+JS
  • Debian10安装VMware Tools
  • torch::和at:: factory function的差別
  • 【问题系列】消费者与MQ连接断开问题解决方案(一)
  • Go使用logrus框架
  • Unity 轨道展示系统(DollyMotion)
  • go标准库
  • 基于协同过滤算法的音乐推荐系统的研究与实现
  • 激光雷达毫米波雷达
  • PyTorch:模型加载方法详解
  • Vue2 若依框架头像上传 全部代码
  • 建筑工程模板包工包料价格
  • Kubernetes基础(九)-标签管理
  • 【Web】攻防世界 难度3 刷题记录(1)
  • Linux 调试工具:gdb
  • 使用shell快速查看电脑曾经连接过的WiFi密码
  • 记一次简单的PHP反序列化字符串溢出
  • 交流负载的功能实现原理
  • 各种排序算法
  • sed应用
  • 视觉CV-AIGC一周最新技术精选(2023-11)
  • 【面经八股】搜广推方向:面试记录(四)
  • git commit 撤销的三种方法