C++复习Day_5
2022/1/29 1:05:08
本文主要是介绍C++复习Day_5,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
加号运算符重载
- 加号运算符重载
- 对于内置的数据类型,编译器知道如何进行运算
- 但是对于自定义数据类型,编译器不知道如何运算
- 利用运算符重载 可以让符号有新的含义
- 利用加号重载 实现p1 + p2 Person数据类型相加操作
- 利用成员函数 和 全局函数 都可以实现重载
- 关键字 operator +
- 成员本质 p1.operator+(p2)
- 全局本质 operator+(p1,p2)
- 简化 p1 + p2
- 运算符重载 也可以发生函数重载
左移运算符重载
-
- 不要滥用运算符重载,除非有需求
- 不能对内置数据类型进行重载
- 对于自定义数据类型,不可以直接用 cout << 输出
- 需要重载 左移运算符
- 如果利用成员 函数重载 ,无法实现让cout 在左侧,因此不用成员重载
- 利用全局函数 实现左移运算符重载
- ostream& operator<<(ostream &cout, Person & p1)
- 如果想访问类中私有内存,可以配置友元实现
前置后置递增运算符重载
-
- 前置递增
- MyInter& operator++()
- 后置递增
- MyInter operator++(int)
- 前置++ 效率高于 后置++ 效率 ,因为后置++会调用拷贝构造,创建新的数据
- 前置递增
#define _CRT_SECURE_NO_WARNINGS #include<iostream> #include<algorithm> #include<string> using namespace std; class MyInt { public: MyInt(int a) { this->num = a; } MyInt& operator++() { this->num++; return *this; } MyInt operator++(int) { MyInt temp = *this; this->num++; return temp; } int num; }; ostream& operator<<(ostream& cout, const MyInt& m) { cout << m.num << endl;; return cout; } int main() { MyInt a(10); system("pause"); return EXIT_SUCCESS; }
指针运算符重载
-
- 智能指针
- 用途: 托管new出来的对象的释放
- 设计smartPoint智能指针类,内部维护 Person * ,在析构时候释放堆区new出来的person对象
- 重载 -> * 让 sp智能指针用起来向真正的指针
#define _CRT_SECURE_NO_WARNINGS #include<iostream> #include<algorithm> using namespace std; class Person { public: Person(int age) { cout << "构造调用" << endl; this->m_age = age; } void showage() { cout << "年龄:" << this->m_age << endl; } ~Person() { cout << "析构调用" << endl; } int m_age; }; class SmartPoint { public: SmartPoint(Person* person) { this->m_person = person; } Person* operator->() { return this->m_person; } Person& operator*() { return *(this->m_person); } Person* show() { return m_person; } ~SmartPoint() { if (this->m_person) { delete this->m_person; this->m_person = nullptr; } } private: Person * m_person; }; void test01() { SmartPoint sp(new Person(18)); sp->showage(); (*sp).showage(); } int main() { test01(); system("pause"); return EXIT_SUCCESS; }
赋值运算符重载
-
- 编译器会默认个一个类添加4个函数
- 默认构造、析构 、 拷贝构造(值拷贝) 、 operator=(值拷贝)
- 如果类中有属性创建在堆区,利用编译器提供的 = 赋值运算就会出现 堆区内存重复释放的问题
- 解决方案:利用深拷贝 重载 =运算符
- Person& operator=( const Person &p)
- 编译器会默认个一个类添加4个函数
#define _CRT_SECURE_NO_WARNINGS #include<iostream> #include<algorithm> using namespace std; class Person { public: Person() = default; Person(const char *name,int age) { //cout << "构造调用" << endl; this->name = new char[strlen(name) + 1]; strcpy(this->name, name); this->m_age = age; } Person(const Person& p) { this->name = new char[strlen(p.name) + 1]; strcpy(this->name, p.name); this->m_age = p.m_age; } Person& operator=(const Person &p) { if (this->name != nullptr) { delete[]this->name; this->name = nullptr; } this->name = new char[strlen(p.name) + 1]; strcpy(this->name, p.name); this->m_age = p.m_age; return *this; } void showage() { cout << "年龄:" << this->m_age << endl; } ~Person() { //cout << "析构调用" << endl; if (this->name != nullptr) { delete[]this->name; this->name = nullptr; } } char* name; int m_age; }; ostream& operator<<(ostream& cout,const Person &p) { cout << p.name << " " << p.m_age; return cout; } class SmartPoint { public: SmartPoint(Person* person) { this->m_person = person; } Person* operator->() { return this->m_person; } Person& operator*() { return *(this->m_person); } Person* show() { return m_person; } ~SmartPoint() { if (this->m_person) { delete this->m_person; this->m_person = nullptr; } } private: Person * m_person; }; void test01() { Person p1("Tom", 18); Person p2("Jerry", 20); Person p3("", 0); p3 = p2 = p1; Person p4 = p3; cout << p1 << endl; cout << p2 << endl; cout << p3 << endl; cout << p4 << endl; //cout << p3; } int main() { test01(); system("pause"); return EXIT_SUCCESS; }
[]重载
- []运算符重载
- int& operator[](int index);
- 实现访问数组时候利用[] 访问元素
#pragma once #define _CRT_SECURE_NO_WARNINGS #include<iostream> #include<algorithm> using namespace std; class MyArray { public: MyArray(); MyArray(int capacity); MyArray(const MyArray& arr); void push_Back(int val); void set_Data(int pos, int val); int get_Data(int pos); int get_Capacity(); int get_Size(); int &operator[](int pos); ~MyArray(); private: int m_Capacity; int m_Size; int* pAddress; }; #include "标头.hpp" MyArray::MyArray() { this->m_Capacity = 100; this->m_Size = 0; this->pAddress = new int[this->m_Capacity]; } MyArray::MyArray(int capacity) { this->m_Capacity = capacity; this->m_Size = 0; this->pAddress = new int[this->m_Capacity]; } MyArray::MyArray(const MyArray& arr) { this->m_Capacity = arr.m_Capacity; this->m_Size = arr.m_Size; this->pAddress = new int[arr.m_Capacity]; for (int i = 0; i < m_Size; i++) { this->pAddress[i] = arr.pAddress[i]; } //memcpy(this->pAddress, arr.pAddress, arr.m_Size); } void MyArray::push_Back(int val) { this->pAddress[this->m_Size] = val; this->m_Size++; } void MyArray::set_Data(int pos, int val) { this->pAddress[pos] = val; } int MyArray::get_Data(int pos) { return this->pAddress[pos]; } int MyArray::get_Capacity() { return this->m_Capacity; } int MyArray::get_Size() { return this->m_Size; } int &MyArray::operator[](int pos) { return this->pAddress[pos]; } MyArray::~MyArray() { if (this->pAddress != nullptr) { delete[] this->pAddress; this->pAddress = nullptr; } }
这篇关于C++复习Day_5的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-05-15PingCAP 黄东旭参与 CCF 秀湖会议,共探开源教育未来
- 2024-05-13PingCAP 戴涛:构建面向未来的金融核心系统
- 2024-05-09flutter3.x_macos桌面os实战
- 2024-05-09Rust中的并发性:Sync 和 Send Traits
- 2024-05-08使用Ollama和OpenWebUI在CPU上玩转Meta Llama3-8B
- 2024-05-08完工标准(DoD)与验收条件(AC)究竟有什么不同?
- 2024-05-084万 star 的 NocoDB 在 sealos 上一键起,轻松把数据库编程智能表格
- 2024-05-08Mac 版Stable Diffusion WebUI的安装
- 2024-05-08解锁CodeGeeX智能问答中3项独有的隐藏技能
- 2024-05-08RAG算法优化+新增代码仓库支持,CodeGeeX的@repo功能效果提升