c++学习

您所在的位置:网站首页 c加加类与对象 c++学习

c++学习

2023-06-06 15:24| 来源: 网络整理| 查看: 265

运算符重载 **运算符重载的概念****加号运算符重载****减号运算符重载****左移运算符重载****右移运算符重载****赋值运算符重载****关系运算符重载****前置加加和后置加加****数组下标重载**

运算符重载的概念

1.运算符重载,就是对已有的运算符重新进行定义,赋予其另一种功能,以适应不同的数据类型。 2.运算符重载的目的是让语法更加简洁 3.运算符重载不能改变本来寓意,不能改变基础类型寓意 4.运算符重载的本质是另一种函数调用(是编译器去调用) 5.这个函数同一的名字叫operator 6.重载函数可以写成全局或成员函数 7.重载函数如果写成全局的,那么双目运算符左边的是第一个参数,右边是第二个参数 8.重载函数如果写成成员函数,那么双目运算符的左边是this,右边是第一个参数 9.不能改变运算符优先级,不能改变运算符的参数个数

在这里插入图片描述 短路规则是:与运算前面假就假后面无需判断,或者是或运算前面真就真后面无需判断 什么叫短路规则

加号运算符重载 #define _CRT_SECURE_NO_WARNINGS #include using namespace std; class Maker { public: Maker(int id, int age) { this->id = id; this->age = age; } //写成成员函数,那么只需要一个参数,这个参数是加号的右边 Maker operator+(Maker &m2) { Maker temp(this->id + m2.id, this->age + m2.age); return temp; } public: int id; int age; }; //全局方式 //2、编译器调用这个函数 //Maker operator+(Maker &p1,Maker &p2)//3、编译器检查参数是否对应 //{ // Maker temp(p1.id + p2.id, p1.age + p2.age); // return temp; //} void test01() { Maker m1(1, 20); Maker m2(2, 22); Maker m3 = m1 + m2;//1、编译器看到两个对象相加,那么编译器会去找有没有叫operator+的函数 cout mid = id; } public: int mid; }; //返回值是任意一个类都行 Maker operator+(Maker &m,Student &s) { Maker tmp(m.id + s.mid, 20);//20随便传的一个参数 return tmp; } Maker operator+(Student &s,Maker &m) { Maker tmp(s.mid + m.id,20);//20随便传的一个参数 return tmp; } void test() { Maker m1(1, 18); Student s1(2); Maker m2 = m1 + s1; Maker m3 = s1 + m1; } int main() { test01(); system("pause"); return EXIT_SUCCESS; }

在这里插入图片描述

减号运算符重载 #define _CRT_SECURE_NO_WARNINGS #include using namespace std; class Maker { public: Maker(int id) { this->id = id; } public: int id; //写成成员函数,那么只需要一个参数,这个参数是减号的右边 Maker operator-(Maker &m2) { Maker tmp(this->id - m2.id); return tmp; } }; int operator-(Maker &m, int b) { return m.id - b; } void test() { Maker m1(10); Maker m2(5); Maker m3 = m1 - m2; cout friend ostream& operator cout test01(); system("pause"); return EXIT_SUCCESS; } 右移运算符重载 #define _CRT_SECURE_NO_WARNINGS #include using namespace std; #include void test() { int a; cin >> a; cout this->name = name; this->age = age; } int getAge() { return age; } private: string name; int age; }; istream &operator>>(istream &in, Maker &m) { in >> m.age; in >> m.name; return in; } void test02() { Maker m("悟空", 15); Maker m2("悟空", 15); cin >> m >> m2; cout public: Maker() { id = 0; age = 0; } Maker(int id, int age) { this->id = id; this->age = age; } public: int id; int age; }; void test() { Maker m1(10, 20); Maker m2; m2 = m1;//赋值操作 //默认的赋值运算符重载函数进行了简单的赋值操作 cout pName = new char[strlen(name) + 1]; strcpy(pName, name); } //防止浅拷贝 Student(const Student &stu) { pName = new char[strlen(stu.pName) + 1]; strcpy(pName, stu.pName); } //重写赋值运算符重载函数 Student &operator=(const Student &stu) //如果不返回&的话相当于是Student operator=s1 相当于调用了一次拷贝构造函数 { //1.不能确定this->pName指向的空间是否能装下stu中的数据,所以先释放thist指向的空间 if (this->pName != NULL) { delete[] this->pName; this->pName = NULL; } //2、申请堆区空间,大小由stu决定 this->pName = new char[strlen(stu.pName) + 1]; //3、拷贝数据 strcpy(this->pName, stu.pName); //4、返回对象本身 return *this;//*this返回的是对象的本身 s1 s2 s3 } ~Student() { if (pName!=NULL) { delete[] pName; pName = NULL; } } void printStudent() { cout Student s1("a"); Student s2("b"); Student s3("c"); s1 = s2 = s3;//赋值操作 cout public: Maker() { id = 0; age = 0; } Maker(int id, int age) { this->id = id; this->age = age; } bool operator==(Maker &m) { if (this->id == m.id && this->age == m.age) { return true; } return false; } bool operator != (Maker &m) { if (this->id != m.id && this->age != m.age) { return true; } return false; } public: int id; int age; }; void test01() { Maker p1(1, 20); Maker p2; if (p1 == p2) { cout cout test01(); system("pause"); return EXIT_SUCCESS; }

在这里插入图片描述

前置加加和后置加加

/你的第二个operator++返回了一个临时变量,这个在C++里面是一个右值(简单来说就是只能放在 = 号右边) 而你重载的 friend ostream &operator ++this->a; return *this; } //重载后置++ //这里不用引用 是因为 //如果返回引用这里是返回局部的引用 在这个函数执行完时,被释放掉了 Maker operator++(int)//区分后置++ //占位参数,必须是int { //后置++,先返回,后++ Maker tmp(*this); //1、*this里面的值a是等于2的 ++this->a; //这个对象的a是等于3的 return tmp;//返回的是右值常量 零时变量 调用一次构造函数 /*你的第二个operator++返回了一个临时变量,这个在C++里面是一个右值(简单来说就是只能放在 = 号右边) 而你重载的 Maker m1(1); //cout public: MyArray(); //拷贝构造 MyArray(const MyArray &arr); MyArray(int capacity, int val = 0); //重写赋值运算符重载函数 MyArray &operator=(const MyArray &m); //要能当左右值 int &operator[](int index); ~MyArray(); //头插 void PushFront(int val); //尾插 void PushBack(int val); //头删 void PopFront(); //尾删 void PopBack(); //获取数组元素个数 int Size(); //获取数组容量 int Capacity(); //指定位置插入元素 void Insert(int pos, int val); //获取指定位置的值 int &Get(int pos);//返回引用可以当左值 又可以当右值 //在指定位置修改值 void Set(int pos, int val); private: int *pArray;//指向堆区空间,存储数据 int mSize;//元素个数 int mCapacity;//容量 };

MyArray.cpp

#include "MyArray.h" MyArray::MyArray() { this->mCapacity = 20; this->mSize = 0; this->pArray = new int[this->mCapacity]; //初始化 for (int i = 0; i mCapacity; i++) { this->pArray[i] = 0; } } //深拷贝 MyArray::MyArray(const MyArray &arr) { this->mCapacity = arr.mCapacity; this->mSize = arr.mSize; //申请空间 this->pArray = new int[arr.mCapacity]; //拷贝数据 for (int i = 0; i mSize; i++) { this->pArray[i] = arr.pArray[i]; } } //MyArray::MyArray(int capacity, int val = 0)//err //声明和实现只能有一个默认参数 MyArray::MyArray(int capacity, int val) { this->mCapacity = capacity; this->mSize = capacity; this->pArray = new int[capacity]; for (int i = 0; i mSize; i++) { this->pArray[i] = val; } } MyArray::~MyArray() { if (this->pArray != NULL) { delete[] this->pArray; this->pArray = NULL; } } //头插 void MyArray::PushFront(int val) { //判断容量是否满 if (this->mSize == this->mCapacity) { return; } for (int i = this->mSize - 1; i >= 0; i--) { this->pArray[i + 1] = this->pArray[i]; } //空出了0的位置 this->pArray[0] = val; //维护元素个数 this->mSize++; } //尾插 void MyArray::PushBack(int val) { //判断容量是否满 if (this->mSize == this->mCapacity) { return; } this->pArray[this->mSize] = val; this->mSize++; } //头删 后面的数往前移动来覆盖第一个元素 void MyArray::PopFront() { if (this->mSize == 0) { return; } for (int i = 0; i mSize - 1; i++) { this->pArray[i] = this->pArray[i + 1]; } this->mSize--; } //尾删 void MyArray::PopBack() { if (this->mSize == 0) { return; } this->mSize--; } //获取数组元素个数 int MyArray::Size() { return this->mSize; } //获取数组容量 int MyArray::Capacity() { return this->mCapacity; } //指定位置插入元素 void MyArray::Insert(int pos, int val) { //判断容量是否满 if (this->mSize == this->mCapacity) { return; } //如果位置不合法,就插入到尾部 if (posthis->mSize - 1) { pos = this->mSize; } for (int i = this->mSize - 1; i >= pos; i--) { this->pArray[i + 1] = this->pArray[i]; } //pos的位置空出 this->pArray[pos] = val; this->mSize++; } //获取指定位置的值 //返回引用可以当左值 又可以当右值 int &MyArray::Get(int pos) { return this->pArray[pos]; } //在指定位置修改值 void MyArray::Set(int pos, int val) { if (pos this->mCapacity - 1) { return; } this->pArray[pos] = val; } //重写赋值运算符重载函数 MyArray &MyArray::operator=(const MyArray &m) { cout this->pArray[i] = m.pArray[i]; } //4、返回对象本身 return *this; } //要能当左右值 int &MyArray::operator[](int index) { if (this->mSize MyArray arr; for (int i = 0; i cout arr[i] = i + 10; } for (int i = 0; i cout


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3