在c++对象的学习中,我们会使用到运算符重载,接下来大家一起学习一下吧!
运算符重载:
operator overloading运算符重载是一种形式的C++多态
即对已有运算符进行重新定义,赋予新的功能,使其适配不同的类型。
形式:operator op(对象参数)
💖总结(建议仔细阅读!!!):
1、一元运算符:++ – a-- ++a-- 二元运算符:a+b
2、成员运算符函数: 一元是没有参数
二元只有一个参数:operator+(Time& t)
3、全局运算符函数: 一元有一个参数
二元有两个参数:operator+(Time& t1,Time& t2)
4、运算符重载的符号必须符合C++要求,不能自己虚构一个运算符,比如@
5、不能违反原来的运算规则
6、不能重载的运算符:(1)sizeof
(2). 成员运算符 p.age
(3)* 成员指针运算符
(4):: 作用域运算符
(5)?: 条件运算符7、只能在成员函数内重载的运算符
(1)= () [] -> 四个
(2)<< >> 只能通过全局函数和友元配合使用
(3)不要重载&&和||两个,因为无法实现短路的规则, flag&&(flag1||flag2)
接下里代码演示一下如何去实现各个运算符的重载:
#include
using namespace std;class Time {
public:int m_hour;int m_min;Time() {m_hour = 0;m_min = 0;}Time(int hour, int min) {m_hour = hour;m_min = min;}void addtime(int hour, int min) {m_hour += hour+(min+m_min)/ 60;m_min = (min+m_min)% 60;}//调用成员方法Time count2(Time& t) {Time time;time.m_hour = this->m_hour + t.m_hour + (this->m_min + t.m_min) / 60;time.m_min= (this->m_min + t.m_min) % 60;return time;}//运算符重载/*Time operator+(Time& t) {Time time;time.m_hour = this->m_hour + t.m_hour + (this->m_min + t.m_min) / 60;time.m_min = (this->m_min + t.m_min) % 60;return time;}*/void showtime() {cout << "小时:" << m_hour << "分钟:" << m_min << endl;}};
//全局函数运算符重载,只能与成员方法的运算符存在一个
Time operator+(Time& t1,Time& t2){Time time;time.m_hour = t1.m_hour + t2.m_hour + (t1.m_min + t2.m_min) / 60;time.m_min = (t1.m_min + t2.m_min) % 60;return time;
}int main() {/*Time t1(1, 20);t1.showtime();t1.addtime(2, 50);t1.showtime();*/Time t1(1, 40);Time t2(2, 30);//调用成员方法相加Time t3 =t1.count2(t2);t3.showtime();//加号运算符重载Time t4 = t1 + t2;t4.showtime();return 0;
}
#include
using namespace std;class Person {friend ostream& operator<<(ostream& out, Person& p);
private:int age;int height;
public:Person(int age, int height) {this->age = age;this->height = height;}void show() {cout << "年龄:" << age << " 身高:" << height << endl;}};
//左移运算符重载:
//void operator<<(ostream& out, Person& p) {
// cout << "年龄:" <cout << "年龄:" << p.age << " 身高:" << p.height << endl;return out;
}int main() {Person p1(21, 183);p1.show();cout << p1;cout << p1 << endl;//operator(cout,p1) ..operator(void,cout),因此修改返回值即可return 0;
}
#include
using namespace std;class Num {
// friend ostream& operator<<(ostream& out, Num& n);friend ostream& operator<<(ostream& out, Num n);
private:int num;
public:Num() {num = 0;}Num(int num) {this->num = num;}//前置++Num& operator++() {num++;return *this;}//后指++Num operator++(int) {//占位参数Num temp = *this;num++;return temp;}};//ostream& operator<<(ostream& out, Num& n) {
// out << "数字num为:" << n.num ;
// return out;
//}ostream& operator<<(ostream& out, Num n) {out << "数字num为:" << n.num;return out;
}int main() {Num n1(1);cout << n1 << endl;cout << ++n1 << endl;cout << n1++ << endl;cout << n1 << endl;return 0;
}
#include
using namespace std;class Person {
private:int age;
public:Person(int age) {this->age = age;}void show() {cout << "年龄为:" << this->age << endl;}~Person() {cout << "Person的析构函数。。。" << endl;}
};//智能指针类
class smartpointer {
public:Person* operator->() {//指针重载return this->person;}Person& operator*() {//指针重载return *person;}smartpointer(Person *p) {person = p;}Person* person;~smartpointer() {cout << "smartpointer的析构函数。。。" << endl;if (person != nullptr) {delete person;person = nullptr;}}};int main() {Person* p1 = new Person(21);p1->show();(*p1).show();//delete p1;smartpointer s1(p1);s1->show();(*s1).show();return 0;
}
#include
#pragma warning(disable:4996)
using namespace std;class Person {
public:char* name;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);return *this;}Person() {cout << "默认构造函数" << endl;}Person(const char* n) {name = new char[strlen(n) + 1];strcpy(name, n);}Person(Person& p) {name = new char[strlen(p.name) + 1];strcpy(name, p.name);}~Person() {cout << "Person的析构函数" << endl;if (name != nullptr) {delete[]name;name = nullptr;}}};int main() {Person p1("Tom");Person p2;p2 = p1;cout << "p1的名字:" << p1.name << endl;cout << "p2的名字:" << p2.name << endl;Person p3(p1);cout << "P3的名字:" << p3.name << endl;return 0;
}
#include
using namespace std;class Array {
public:int m_num;int* arr;Array(int size) {m_num = 10;arr = new int[size];for (int i = 0; i < size; i++) {arr[i] = i;}}int getnum(int poisition) {return arr[poisition];}int& operator[](int position) {//中括号运算符重载return arr[position];}};int main() {Array arr1(5);for (int i = 0; i < 5; i++) {cout << arr1.arr[i] << endl;}cout << "==============" << endl;cout << arr1.getnum(1) << endl;cout << "==============" << endl;cout << arr1[2] << endl;return 0;
}
#include
#includeusing namespace std;class Person {
public:string m_name;int m_age;Person(string name, int age) {this->m_age = age;this->m_name = name;}bool operator==(const Person& p) {//==关系运算符重载return (this->m_name == p.m_name && this->m_age == p.m_age);}bool operator!=(const Person& p) {//!=关系运算符重载return !(this->m_name == p.m_name && this->m_age == p.m_age);}};int main() {Person p1("Tom", 18);Person p2("Lilua", 20);cout << p1.m_name << endl;cout << p2.m_name << endl;if (p1 == p2) {cout << "p1==p2" << endl;}else {cout << "p1!=p2" << endl;}if (p1 != p2) {cout << "p1!=p2" << endl;}else {cout << "p1==p2" << endl;}return 0;
}
#include
#include
using namespace std;class Print {
public:void operator()(string str) {//函数调用运算符重载cout << str << endl;}int operator()(int a, int b) {//函数调用运算符重载return a + b;}
};int main() {Print p;p("nihao,my world");int sum = p(1, 2);//仿函数cout << sum << endl;return 0;
}