操作符重载
-
C++不允许用户自己定义新的运算符,只能对已有的 C++运算符进行重载
image.png
-
运算符重载函数可以是类的成员函数,也可以是类的友元函数,还可以是既非
类 的成员函数也不是友元函数的普通函数
操作符重载基本语法
#include <iostream>
using namespace std;
class Complex {
private:
int m_a;
int m_b;
public:
Complex(int a, int b) {
m_a = a;
m_b = b;
}
void printComplex() {
cout << "( m_a = "<< m_a << ", m_b = " << m_b << "i)" << endl;
}
Complex operator-(Complex& c1) {
Complex tmp(this->m_a - c1.m_a, this->m_b - c1.m_b);
return tmp;
}
friend Complex operator+(Complex& com1, Complex& com2);
};
//操作符重载写在全局 operator和要重载的操作符中间不能有空格
Complex operator+(Complex& com1, Complex& com2) {
Complex tmp(com1.m_a + com2.m_a, com1.m_b + com2.m_b);
return tmp;
}
int main() {
Complex c1(1,2);
Complex c2(2,3);
c1.printComplex();
c2.printComplex();
Complex c3 = c1+c2;
c3.printComplex();
Complex c4 = c3 - c2 - c1;
c4.printComplex();
return 0;
}
双目运算符重载
#include <iostream>
using namespace std;
class Complex {
private:
int m_a;
int m_b;
public:
Complex(int a, int b) {
m_a = a;
m_b = b;
}
Complex(){}
void printComplex() {
cout << "( m_a = "<< m_a << ", m_b = " << m_b << "i)" << endl;
}
Complex& operator+=(Complex& c1) {
this->m_a += c1.m_a;
this->m_b += c1.m_b;
return *this;
}
Complex& operator++() {
this->m_a++;
this->m_b++;
return *this;
}
const Complex& operator++(int) {
Complex temp(this->m_a, this->m_b);
this->m_a++;
this->m_b++;
return temp;
}
};
int main() {
Complex c1(1,2);
Complex c2(2,3);
(c1 += c2) += c2;
c1.printComplex();
Complex c3(5,6);
++++c3;
c3.printComplex();
Complex c4(3,2);
c4++;
c4.printComplex();
return 0;
}
左移右移运算符重载
#include <iostream>
using namespace std;
class Complex {
private:
int m_a;
int m_b;
public:
Complex(int a, int b) {
m_a = a;
m_b = b;
}
friend ostream& operator<<(ostream &os, Complex &c1);
// 左移操作符,只能写在全局, 不能写在成员方法在,因为调用顺序会变反 c1 << cout;
// ostream& operator<<(ostream &os){
// os <<"( m_a = "<< this->m_a << ", m_b = " << this->m_b << "i)" << endl;
// return os;
// }
friend istream& operator>>(istream &is, Complex &c1);
};
ostream& operator<<(ostream &os, Complex &c1){
os <<"( m_a = "<< c1.m_a << ", m_b = " << c1.m_b << "i)";
return os;
}
istream& operator>>(istream &is, Complex &c1) {
cout <<" m_a = ";
is >> c1.m_a;
cout <<" m_b = ";
is >> c1.m_b;
return is;
}
int main() {
Complex c1(1,2);
cout << c1 << " "<< c1 << endl;
cin >> c1;
cout << c1 << " "<< c1 << endl;
return 0;
}
=号操作符重载
#include <iostream>
#include <string.h>
using namespace std;
class Student {
private:
int id;
char * name;
public:
Student() {
this->id = 0;
this->name = NULL;
}
Student(int id, char *name) {
this->id = id;
int length = strlen(name);
this->name = new char[length +1];
strcpy(this->name, name);
}
Student& operator=(const Student &stu) {
//防止自身赋值
if (this == &stu)
{
return *this;
}
// 先将自身的额外开辟的空间回收
if (this->name != NULL)
{
delete this->name;
this->name = NULL;
this->id = 0;
}
//执行深拷贝
this->id = stu.id;
int length = strlen(stu.name);
this->name = new char[length +1];
strcpy(this->name, stu.name);
}
void print() {
cout<< "id = " << id << " name = " << name << endl;
}
~Student() {
if (this->name != NULL)
{
delete this->name;
this->name = NULL;
}
}
};
int main() {
Student stu1(22, "张三丰");
Student stu2;
stu2 = stu1;
stu2.print();
return 0;
}












网友评论