以下爲常用的幾種運算符重載
1、加號、減號運算符的重載
2、++、--運算符的重載
3、賦值運算符的重載
4、==與!=運算符的重載
5、[]與<<運算符的重載
注意:
[]返回值爲左值需要返回一個引用。
<<操作符重載爲鏈式重載,需要返回一個引用。
||和&&操作符一般不能重載,因爲其具有短路原則。
1、加號、減號運算符的重載
#include "iostream"
using namespace std;
class Complex
{
public:
Complex(int a, int b)
{
this->a = a;
this->b = b;
}
void Show()
{
cout << a << "+" << b << "i" << endl;
}
friend Complex operator+(Complex &a1, Complex &a2);
private:
int a;
int b;
};
Complex operator+(Complex &a1, Complex &a2)
{
Complex tmp(a1.a + a2.a,a1.b+a2.b);
return tmp;
}
int main()
{
Complex a1(1, 2), a2(3, 4);
Complex a3 = a1 + a2;
a3.Show();
system("pause");
return 0;
}
2、++、--運算符的重載
++與--運算符分爲前置++、--和後置++、--,因此要注意使用佔位符來區分。
class Complex1
{
public:
Complex1(int a = 0, int b = 0)
{
this->a = a;
this->b = b;
}
void Show()
{
cout << a << "+" << b << endl;
}
Complex1& operator++() //前置++
{
this->a++;
this->b++;
return *this;
}
Complex1 operator++(int) //後置++
{
Complex1 tmp = *this;
this->a++;
this->b++;
return tmp;
}
Complex1& operator--() //前置--
{
this->a--;
this->b--;
return *this;
}
Complex1 operator--(int) //後置--
{
Complex1 tmp = *this;
this->a--;
this->b--;
return tmp;
}
private:
int a;
int b;
};
int main()
{
Complex1 a1(1, 2);
++a1;
a1.Show();
--a1;
a1.Show();
system("pause");
return 0;
}
3、賦值運算符的重載
#define _CRT_SECURE_NO_WARNINGS
#include "iostream"
using namespace std;
class String
{
public:
String(const char *str = NULL); // 通用構造函數
String(const String &s); // 拷貝構造函數
~String(); // 析構函數
String& operator = (const String &s); // 賦值函數
void operator==(const String &s);
char& operator[](int index);
friend ostream& operator<<(ostream& out, const String &s);
void showStr()
{
cout << m_data << endl;
}
private:
char* m_data; // 用於保存字符串
int m_len;
};
//構造函數
String::String(const char *str)
{
if (str == NULL)
{
m_data = new char[m_len + 1];
strcpy(m_data, "\0");
}
else
{
m_len = strlen(str);
m_data = new char[m_len + 1];
strcpy(m_data, str);
}
//cout << "調用構造函數" << endl;
}
//拷貝構造函數
String::String(const String &s)
{
m_len = strlen(s.m_data);
this->m_data = new char[m_len + 1];
strcpy(this->m_data, s.m_data);
//cout << "調用拷貝構造函數" << endl;
}
//析構函數
String::~String()
{
if (m_data != NULL)
{
delete[] m_data;
m_data = NULL;
m_len = 0;
}
//cout << "析構函數被調用" << endl;
}
//賦值函數
String& String::operator = (const String &s)
{
if (m_data != NULL)
{
delete[] m_data;
m_data = NULL;
m_len = 0;
}
if (s.m_data == NULL)
{
m_len = 0;
this->m_data = new char[m_len + 1];
strcpy(m_data, "\0");
}
else
{
m_len = strlen(s.m_data);
this->m_data = new char[m_len + 1];
strcpy(m_data, s.m_data);
}
return *this;
}
int main()
{
String s1("hello");
s1.showStr();
String s2("world");
s1 = s2;
s1.showStr();
system("pause");
return 0;
}
4、==與!=運算符的重載
class equ
{
public:
equ(int a)
{
this->a = a;
}
void operator==(equ &a1);
void operator!=(equ &a1);
private:
int a;
};
void equ::operator==(equ &a1)
{
if (this->a == a1.a)
{
cout << "相等" << endl;
}
else
{
cout << "不相等" << endl;
}
}
void equ::operator!=(equ &a1)
{
if (this->a != a1.a)
{
cout << "不相等" << endl;
}
else
{
cout << "相等" << endl;
}
}
int main()
{
equ q1(1);
equ q2(2);
equ q3(1);
q1 == q2;
q1 != q3;
system("pause");
return 0;
}
5、[]與<<運算符的重載
class String
{
public:
String(const char *str = NULL); // 通用構造函數
String(const String &s); // 拷貝構造函數
~String(); // 析構函數
String& operator = (const String &s); // 賦值函數
char& operator[](int index);
friend ostream& operator<<(ostream& out, const String &s);
void showStr()
{
cout << m_data << endl;
}
private:
char* m_data; // 用於保存字符串
int m_len;
};
//構造函數
String::String(const char *str)
{
if (str == NULL)
{
m_data = new char[m_len + 1];
strcpy(m_data, "\0");
}
else
{
m_len = strlen(str);
m_data = new char[m_len + 1];
strcpy(m_data, str);
}
//cout << "調用構造函數" << endl;
}
//拷貝構造函數
String::String(const String &s)
{
m_len = strlen(s.m_data);
this->m_data = new char[m_len + 1];
strcpy(this->m_data, s.m_data);
//cout << "調用拷貝構造函數" << endl;
}
//析構函數
String::~String()
{
if (m_data != NULL)
{
delete[] m_data;
m_data = NULL;
m_len = 0;
}
//cout << "析構函數被調用" << endl;
}
//賦值函數
String& String::operator = (const String &s)
{
if (m_data != NULL)
{
delete[] m_data;
m_data = NULL;
m_len = 0;
}
if (s.m_data == NULL)
{
m_len = 0;
this->m_data = new char[m_len + 1];
strcpy(m_data, "\0");
}
else
{
m_len = strlen(s.m_data);
this->m_data = new char[m_len + 1];
strcpy(m_data, s.m_data);
}
return *this;
}
//[]運算符重載
char& String::operator[](int index)
{
return m_data[index];
}
//<<重載
ostream& operator<<(ostream& out, const String &s)
{
out << s.m_data;
return out;
}
int main()
{
String s1("hello");
cout << s1[1] << endl;
system("pause");
return 0;
}