C++ 知识点12 构造函数

C++ 构造函数


一、构造函数是什么

核心特点(必背)

  1. 函数名 和类名完全相同

  2. 没有返回值 (连 void 都不用写)

  3. 创建对象时 自动调用

  4. 作用:初始化成员变量

最简基础代码

cpp 复制代码
#include <iostream>
using namespace std;
​
class Person
{
public:
    // 构造函数:和类名同名,无返回值
    Person()
    {
        cout << "构造函数自动调用" << endl;
    }
};
​
int main()
{
    // 创建对象 → 自动执行构造函数
    Person p;
    return 0;
}

输出:

cpp 复制代码
构造函数自动调用

二、无参构造函数

规则

  • 没有参数

  • 不写构造时,编译器自动生成一个默认无参构造(空函数体)

cpp 复制代码
#include <iostream>
using namespace std;
​
class Student
{
public:
    // 无参构造
    Student()
    {
        cout << "无参构造执行" << endl;
        // 初始化默认值
        id = 0;
    }
    int id;
};
​
int main()
{
    Student s; // 自动调用无参构造
    cout << "id = " << s.id << endl;
    return 0;
}

⚠️ 易错点:

cpp 复制代码
Student s();   // 不是创建对象!是函数声明,千万别这么写

无参实例化直接写:Student s;


三、有参构造函数

用来创建对象时直接给成员赋初值

cpp 复制代码
#include <iostream>
#include <string>
using namespace std;
​
class Person
{
public:
    string name;
    int age;
​
    // 有参构造
    Person(string n, int a)
    {
        name = n;
        age = a;
        cout << "有参构造执行" << endl;
    }
​
    void show()
    {
        cout << "姓名:" << name << " 年龄:" << age << endl;
    }
};
​
int main()
{
    // 调用有参构造
    Person p("张三", 18);
    p.show();
​
    // C++11 列表初始化写法
    Person p2{"李四", 20};
    p2.show();
​
    return 0;
}

输出:

cpp 复制代码
有参构造执行
姓名:张三 年龄:18
有参构造执行
姓名:李四 年龄:20

四、构造函数重载

一个类可以有多个构造函数,参数个数 / 类型不同,构成重载。

cpp 复制代码
#include <iostream>
#include <string>
using namespace std;
​
class Person
{
public:
    string name;
​
    // 无参构造
    Person()
    {
        name = "未知";
        cout << "无参构造" << endl;
    }
​
    // 有参构造
    Person(string n)
    {
        name = n;
        cout << "有参构造" << endl;
    }
};
​
int main()
{
    Person p1;         // 匹配无参
    Person p2("王五"); // 匹配有参
    return 0;
}

五、初始化列表初始化(重点、考试必考)

作用

  1. 构造函数执行前先初始化成员

  2. 初始化 const 成员、引用成员、成员对象 必须用初始化列表

语法

cpp 复制代码
类名(参数) : 成员1(值1), 成员2(值2)
{
    函数体
}

代码示例:

cpp 复制代码
#include <iostream>
#include <string>
using namespace std;

class Person
{
private:
    string name;
    int age;
public:
    // 初始化列表写法
    Person(string n, int a) : name(n), age(a)
    {
        cout << "构造函数体执行" << endl;
    }

    void show()
    {
        cout << name << " " << age << endl;
    }
};

int main()
{
    Person p("赵六", 22);
    p.show();
    return 0;
}

关键规则

  • 初始化顺序 按类中定义顺序,不是列表书写顺序

  • const 成员、引用成员 只能用初始化列表初始化

cpp 复制代码
class Test
{
private:
    const int x;
public:
    // 必须在列表初始化const成员
    Test() : x(100) {}
};

六、拷贝构造函数

作用

一个已有对象 去初始化新对象

格式

cpp 复制代码
类名(const 类名& 旧对象)

代码演示:

cpp 复制代码
#include <iostream>
#include <string>
using namespace std;

class Person
{
private:
    string name;
    int age;
public:
    // 普通有参构造
    Person(string n, int a) : name(n), age(a) {}

    // 拷贝构造函数
    Person(const Person& p)
    {
        name = p.name;
        age = p.age;
        cout << "拷贝构造执行" << endl;
    }

    void show()
    {
        cout << name << " " << age << endl;
    }
};

int main()
{
    Person p1("小明", 19);
    
    // 调用拷贝构造
    Person p2(p1);  
    p2.show();

    // 等价简写
    Person p3 = p1; 

    return 0;
}

什么时候会自动调用拷贝构造

  1. 用旧对象初始化新对象

  2. 对象作为函数值传递参数

  3. 函数返回对象


七、默认构造函数规则

  1. 自己不写任何构造 → 编译器自动生成默认无参构造

  2. 只要写了一个有参构造 ,编译器 不再生成默认无参构造

代码演示坑:

cpp 复制代码
class Student
{
public:
    // 写了有参构造
    Student(int a)
    {
        cout << a << endl;
    }
};

int main()
{
    // Student s;  // 报错!没有无参构造了
    Student s(10); // 只能调用有参
    return 0;
}

八、构造函数核心总结(背诵版)

  1. 构造函数 名同类名、无返回值、创建对象自动调用

  2. 分:无参、有参、重载、拷贝构造

  3. 初始化列表:优先初始化,必须初始化 const / 引用 / 成员对象;

  4. 写了有参构造,编译器不再提供默认无参构造

  5. 拷贝构造格式:类名(const 类名&),用于对象复制初始化。

相关推荐
Jack2015 小时前
HarmonyOS开发中错误处理策略:网络异常统一处理
算法
小小杨树17 小时前
读懂色彩:拍照调色不再难
算法·计算机视觉·配色
JieE2121 天前
LeetCode 226. 翻转二叉树|JS 递归超详细拆解,二叉树入门经典题
javascript·算法
JieE2121 天前
LeetCode 104. 二叉树的最大深度|递归思路超详细拆解
javascript·算法
vivo互联网技术2 天前
CVPR 2026 | 全新强化学习框架 BeautyGRPO:重塑真实人像
算法·大模型·cvpr·影像
Darling噜啦啦2 天前
列表转树算法深度解析:从 Map 到 Reduce 的两种实现,面试高频考点
数据结构·算法·面试
clint4562 天前
C++进阶(1)——前景提要
c++
用户497863050732 天前
(一)小红的数组操作
算法·编程语言
夜悊2 天前
C++代码示例:进制数简单生成工具
c++