您的位置:首页 > 其它

菜鸟学习历程【29】运算符重载

2018-01-12 20:12 183 查看
我们知道,函数重载就是对一个已有的函数赋予新的含义,使之实现新功能。

那么对于运算符而言,也可以重载实现不同的功能。

不可重载的运算符有(用括号括起来): (.),(::),(.*),(?:),(sizeof)

不可使用友元函数重载的有: = 、 () 、 【】、 ->

对于无法修改左操作数的时候,只能使用全局函数实现。

双目运算符

以“+”运算符为例

//对于两个整型变量a, b
//定义 int c = a + b;编译器很容易去理解,但对于两个复数类的对象,编译器并不知道,该如何对这种情况进行相加,因此,我们需要重载“+”运算符。
#include <iostream>

using namespace std;

class Complex
{
public:
Complex(int a, int b)
{
m_a = a;
m_b = b;
}
Complex operator+(const Complex &obj) const
{
Complex tmp(m_a + obj.m_a, m_b + obj.m_b);

return tmp;
}
void print()
{
printf("%d + %di\n", m_a, m_b);
}
private:
int m_a; //实部
int m_b; //虚部
};

int main()
{
Complex c1(1, 2);
c1.print(); // 1 + 2i

Complex c2(2, 3);
c2.print(); // 2 + 3i

Complex c3;
//运算符重载的步骤
//1.写出函数名: operator + 要重载的运算符;例如: operator+
//2.根据运算符需要的操作数个数写出参数列表,例如:operator+(c1, c2)
//3.根据需要,写出函数返回值:Complex operator+(const Complex &c1, const Complex &c2)
//写成内部:Complex operator+(const Complex &obj) const
//写成类的成员函数与写成全局函数,只能有一个存在
c3 = c1 + c2;
return 0;
}


单目运算符

以前置 ++ 和后置 ++ 为例

我们想通过对类的成员进行 ++ 操作时,对实部与虚部都进行 ++ 操作,因此需要重载这种运算符。

#include <iostream>

using namespace std;

class Complex
{
//外部实现时,需声明为友元函数才行
friend Complex& operator++(Complex &obj);
public:
Complex(int a, int b)
{
m_a = a;
m_b = b;
}
//前置++,内部实现比外部实现少一个参数,因为内部非静态函数内有this指针,指向当前对象
Complex& operator++()
{
++m_a;
++m_b;
return *this;
}
//后置++,为了与前置++进行区分,会使用一个占位参数
Complex operator++(int)
{
Complex tmp(m_a, m_b);
m_a++;
m_b++;
return tmp;
}
void print()
{
printf("%d + %di\n", m_a, m_b);
}
private:
int m_a; //实部
int m_b; //虚部
};

//前置++
//外部实现与内部实现,只允许一个存在
//Complex& operator++(Complex &obj)
//{
//  ++obj.m_a;
//  ++obj.m_b;
//  return obj;
//}

int main()
{
Complex c1(1, 2);
++c1;
c1.print();  // 2 + 3i
Complex c2 = c1++;
c2.print();  // 2 + 3i
c1.print();  // 3 + 4i
return 0;
}


函数运算符()

#include <iostream>

using namespace std;

class Test
{
public:
Test(int a)
{
this->a = a;
}

void operator()(int b1, int b2)
{
printf ("a = %d\n", a);
}
private:
int a;
};

int main7_1()
{
// 定义对象
Test a(10);

// ()是函数调用运算符
// operator()(Test &a)  ===>   operator()()
a(1, 2);  // a 是一个对象, 不是一个函数, 但是它的行为和函数很像, 这样的使用  叫仿函数

return 0;
}


左移与右移运算符

输入输出运算符,只能对一些基本数据类型进行直接的操作,从键盘直接输入一个整型数、输入一个字符、字符串。。。。

如果让它输入输出一个自定义类型的数据,编译无法通过,所以需要重载来完成。

例如,我们通过左移运算符直接输出一个复数类的对象。

#include <iostream>

using namespace std;

class Complex
{
friend ostream& operator<<(ostream &out,const Complex &obj);
public:
Complex(int a, int b)
{
m_a = a;
m_b = b;
}
void print()
{
printf("%d + %di\n", m_a, m_b);
}
private:
int m_a; //实部
int m_b; //虚部
};

ostream& operator<<(ostream &cout,const Complex &obj)
{
out << obj.m_a << " + " << obj.m_b << "i";
return out;
}

int main()
{
Complex c1(1, 2);
//左移运算符的左操作数是cout,它属于ostream,我们无法修改,所以只能通过友元函数进行实现
// operator<<(ostream &cout,const Complex &obj);
cout<< c1 << endl;

return 0;
}


赋值运算符

#include <iostream>

using namespace std;
class Teacher
{
friend ostream &operator<<(ostream &out, const Teacher &s);
public:
Teacher()
{
age  = 0;
name = NULL;
}
Teacher (int age, char *name)
{
this->age = age;
this->name = new char[20];

strcpy (this->name, name);
}

Teacher (const Teacher &obj)
{
this->age = obj.age;
this->name = new char[20];

strcpy (this->name, obj.name);
}
~Teacher()
{
if (name != NULL)
delete [] name;
name = NULL;
age  = 0;
}

Teacher &operator=(const Teacher &obj)
{
// 函数入口参数检查  如果是自己直接返回
if (&obj == this)
return *this;

// 1、释放原有的空间
if (name != NULL)
delete [] name;

// 2、开辟新空间
this->name = new char[20];

// 复制
this->age = obj.age;
strcpy (this->name, obj.name);

return *this;
}

private:
int age;
char *name;
};

ostream &operator<<(ostream &out, const Teacher &s)
{
printf ("age = %d, name = %s", s.age, s.name);
return out;
}

int main()
{
Teacher t1(20, "wang");

Teacher t2 = t1;
cout << t2 << endl;

Teacher t3(25, "zhang");

// 默认的赋值运算是  一个浅拷贝的过程, 不会复制堆上的空间
t3 = t2;
cout << t3 << endl;

Teacher t4;

t3 = t3;

return 0;
}


数组下标运算符

#include <iostream>

using namespace std;

class Array
{
friend ostream &operator<<(ostream &out, const Array& obj);
public:
Array(int len = 0)
{
m_len = len;
if (len <= 0)
m_array = NULL;

m_array = new int[len];
}
~Array()
{
if (m_array != NULL)
delete[]m_array;
}
Array(const Array &obj)
{
m_len = obj.m_len;
m_array = new int[m_len];

for (int i = 0;i < m_len; i++)
{
m_array[i] = obj.m_array[i];
}
}
int & operator[](int i)
{
return m_array[i];
}
Array & operator=(const Array &obj)
{
for (int i = 0; i < obj.m_len; i++)
{
m_array[i] = obj.m_array[i];
}
}

private:
int m_len;
int *m_array;
};

ostream &operator<<(ostream &out, const Array& obj)
{
for (int i = 0; i < obj.m_len; i++)
{
out << obj.m_array[i] << "  " ;
}

return out;
}

int main()
{
Array a(10);
for (int i = 0; i < 10; i++)
{
a[i] = i;
}

Array a1 = a;

cout <<"a = "<< a << endl;
cout <<"a1 = "<<a1 << endl;

a1[3] = 200;

cout << "a1 = " << a1 << endl;

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  运算符重载