【C++】类和对象(中)——默认成员函数详解(万字)
文章目录
- 上文链接
- 类的默认成员函数
- 1. 构造函数
- (1) 什么是构造函数
- (2) 构造函数的使用
- 2. 析构函数
- (1) 什么是析构函数
- (2) 析构函数的使用
- (3) 小练习
- 3. 拷贝构造函数
- (1) 什么是拷贝构造函数
- (2) 拷贝构造函数的使用
- 4. 赋值运算符重载
- (1) 运算符重载
- (2) 运算符重载的简单应用
- (3) 赋值运算赋重载函数
- (4) 赋值运算符重载函数的使用
- (5) 综合运用(日期类)
- 5. 取地址运算符重载
- (1) const 成员函数
- (2) 取地址运算符重载
- 下文链接
上文链接
类和对象(上)
类的默认成员函数
默认成员函数就是用户没有显式实现,但是编译器会自动生成的成员函数称为默认成员函数。
默认成员函数一共有以下 6 个,其中我们需要重点掌握前 4 个,后面两个了解即可。
在 C++11 以后还会增加两个默认成员函数,移动构造和移动赋值。
默认成员函数很重要,也比较复杂,我们要从两个方面去学习:
- 第一:我们不写时,编译器默认生成的函数行为是什么,是否满足我们的要求。
- 第二:编译器默认生成的函数不满足我们的需求,我们需要自己实现,那么如何自己实现?
1. 构造函数
(1) 什么是构造函数
构造函数是一种特殊的成员函数。它的功能类似于我们在模拟实现 Stack 类时的 Init() 函数,是用来在实例化对象时初始化对象。虽然它名字里有“构造”二字,但并非是去构造一个对象,我们常使用的局部对象是栈帧创建时,空间就已经开好了的。
(2) 构造函数的使用
上面说构造函数是一种特殊的成员函数,它到底特殊在哪里?
- 构造函数的函数名与类名相同。
- 构造函数无返回值。(没有返回值,也不需要写 void,C++ 就是这么规定的)
- *对象实例化时系统会自动调用对应的构造函数
- 构造函数可以重载。
(还有很多特点,别急!我们先来看看前 4 个特点)
为了方便理解,这里我们简单实现一个 Date 类
#include using namespace std; class Date { public: // 构造函数 Date() // 没有void, 并且函数名与该类的名称相同 { _year = 2025; // 初始化的数据 _month = 1; _day = 1; } void print() { cout Date d1; d1.print(); return 0; } public: // 构造函数 1 Date() { _year = 2025; _month = 1; _day = 1; } // 构造函数 2 Date(int year, int month, int day) { _year = year; _month = month; _day = day; } void print() { cout Date d1; // 调用的是构造函数 1 d1.print(); Date d2(2025, 4, 18); // 调用的是构造函数 2 d2.print(); return 0; } // Date d1(); } public: void print() { cout Date d1; d1.print(); return 0; } public: // 全缺省 -- 是一个默认构造函数 Stack(int n = 4) { _a = (int*)malloc(sizeof(int) * n); if (_a == nullptr) { perror("malloc申请空间失败"); return; } _capacity = n; _top = 0; } // ... private: int* _a; int _capacity; int _top; }; class MyQueue { Stack _pushst; Stack _popst; }; int main() { MyQueue q; return 0; } public: Stack(int n) // 改成需要传参的一个构造函数 { _a = (int*)malloc(sizeof(int) * n); if (_a == nullptr) { perror("malloc申请空间失败"); return; } _capacity = n; _top = 0; } // ... private: int* _a; int _capacity; int _top; }; public: Stack(int n = 4) { _a = (int*)malloc(sizeof(int) * n); if (_a == nullptr) { perror("malloc申请空间失败"); return; } _capacity = n; _top = 0; } ~Stack() // 析构函数 { free(_a); _a = nullptr; _top = _capacity = 0; } private: int* _a; int _capacity; int _top; }; int main() { Stack st1; Stack st2; return 0; } public: Stack(int n = 4) { _a = (int*)malloc(sizeof(int) * n); if (_a == nullptr) { perror("malloc申请空间失败"); return; } _capacity = n; _top = 0; } ~Stack() { cout Stack _pushst; Stack _popst; }; int main() { MyQueue q; return 0; } public: ~MyQueue() { free(_ptr); } private: Stack _pushst; Stack _popst; int* _ptr; }; int main() { MyQueue q; return 0; } A a; B b; static D d; return 0; } public: Date(int year = 1, int month = 1, int day = 1) { _year = year; _month = month; _day = day; } Date(const Date& d) // 拷贝构造函数 { _year = d._year; _month = d._month; _day = d._day; } void print() { cout Date d1(2025, 4, 19); d1.print(); Date d2(d1); // 用 d1 来初始化 d2, 相当于拷贝一份 d2.print(); return 0; } _year = d._year; _month = d._month; _day = d._day; } public: // ... Date(const Date& d) // 拷贝构造函数 { _year = d._year; _month = d._month; _day = d._day; } // ... }; void Func(Date d) { } int main() { Date d1(2025, 4, 19); d1.print(); Func(d1); return 0; } _year = d._year; _month = d._month; _day = d._day; } public: Date(int year, int month, int day) { _year = year; _month = month; _day = day; } void print() { cout Date d1(2025, 4, 19); d1.print(); Date d2(d1); d2.print(); return 0; } public: Stack(int n = 4) { _a = (int*)malloc(sizeof(int) * n); if (_a == nullptr) { perror("malloc申请空间失败"); return; } _capacity = n; _top = 0; } ~Stack() { free(_a); _a = nullptr; _top = _capacity = 0; } private: int* _a; int _capacity; int _top; }; int main() { Stack st1; Stack st2(st1); return 0; } public: // ... Stack(const Stack& st) { // 拷贝构造函数(深拷贝) // 需要对 _a 指向的资源创建同样大小的资源再拷贝值 _a = (int*)malloc(sizeof(int) * st._capacity); if (_a == nullptr) { perror("malloc申请空间失败!"); return; } memcpy(_a, st._a, sizeof(int) * st._top); _top = st._top; _capacity = st._capacity; } // ... }; int i = 0; ++i; bool ret = i
免责声明:我们致力于保护作者版权,注重分享,被刊用文章因无法核实真实出处,未能及时与作者取得联系,或有版权异议的,请联系管理员,我们会立即处理! 部分文章是来自自研大数据AI进行生成,内容摘自(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供学习参考,不准确地方联系删除处理! 图片声明:本站部分配图来自人工智能系统AI生成,觅知网授权图片,PxHere摄影无版权图库和百度,360,搜狗等多加搜索引擎自动关键词搜索配图,如有侵权的图片,请第一时间联系我们。