c++关于运算符重载的整理

一、运算符重载

1、运算符重载是为了让一个运算符具有多种操作和功能,和函数重载的含义类似,同一个运算符可以具有多种功能。同一运算符计算多种变量。

2、几乎所有的运算符都可以被重载,除了以下几个运算符:. :: ?: sizeof这些是不能做重载的运算符。

3、运算符重载基本上出现再类中和结构中。

4、运算符重载要满足运算符原来的运算符所具有的含义。

5、运算符重载可以理解为定义了一个函数,该函数可以让运算符具有新的功能。

运算符重载函数的一般形式:

返回值类型 operator运算符(形参)
{
  函数内容;
}
 

二、基础运算符重载

在这里,先把最熟悉的加减乘除这类基础运算符重载。

1、类内定义运算符重载函数

#include<iostream>
using namespace std;
//基础运算符重载
class Math
{
private:
 int m_data;
public:
 Math(int a);
 void Getshow();
 int operator+(const Math & math);
};
Math::Math(int a):m_data(a){}
int Math::operator+(const Math& math)
{
 return this->m_data + math.m_data;
}

int main()
{
 Math m1(1), m2(2);
 int c = m1 + m2;
 cout << c << endl;
 
 system("pause");
 return 0;
}

结果输出为3.

因为在类内定义了运算符重载函数,所以this指针可作为一个形参,因此定义成了

 int operator+(const Math & math);

只含一个参数的函数形式。
同时应该注意返回值与返回值类型匹配,可根据要返回的值来确定返回值类型,在此处也可以定义返回值类型为类类型的返回值,在下面的例子将会用到。

2、在全局范围内定义运算符重载函数

#include<iostream>
using namespace std;

class Math
{
private:
 int m_data;
public:
 Math();
 Math(int a);
 void Getshow()const;
 friend Math operator+(const Math& math1, const Math& math2);
};
Math:: Math():m_data(){}
Math::Math(int a):m_data(a){}
Math operator+(const Math& math1 ,const Math& math2)
{
 Math math3;
 math3.m_data=math1.m_data + math2.m_data;
 return math3;
}
void Math::Getshow()const
{
 cout << m_data << endl;
}
int main()
{
 Math m1(1), m2(2);
 Math m3;
 m3 = m1 + m2;
 m3.Getshow();
 system("pause");
 return 0;
}

输出结果为3.
在定义全局变量时,要在类中声明为友元函数,否则无法调用类内的私有变量。

对其他基本运算符的操作也基本如此,只需要改变operator后面的运算符就可以啦,需要注意的是返回值的类型。

三、前++和后++的运算符重载

前++和后++有些许形式上的不同,在使用的时候应当注意。

1、前++的运算符重载

#include<iostream>
using namespace std;
class Math
{
private:
 int m_data;
public:
 Math();
 Math(int a);
 void Getshow()const;
 Math& operator++();
};
Math:: Math():m_data(){}
Math::Math(int a):m_data(a){}
Math& Math::operator++()//前++
{
 ++this->m_data;
 return *this;
}
void Math::Getshow()const
{
 cout << m_data << endl;
}
int main()
{
 Math m1(1);
  ++m1;
 
 m1.Getshow();
 system("pause");
 return 0;
}

前++运算符重载的函数形式为:

 Math& operator++();

2、后++的运算符重载

#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;
class Math
{
private:
 int m_data;
public:
 Math();
 Math(int a);
 void Getshow()const;
 Math operator++(int);
};
Math:: Math():m_data(){}
Math::Math(int a):m_data(a){}
Math Math::operator++(int)//后++
{
 Math m1=*this;
 this->m_data++;
 return m1;
}
void Math::Getshow()const
{
 cout << m_data << endl;
}
int main()
{
 Math m1(1);
 Math m2=m1++;
 m1.Getshow();
 m2.Getshow();
 system("pause");
 return 0;
}

后++运算符重载的形式为:

Math operator++(int);

这里在()内加上int是后++的固定形式

四、<<和>>的运算符重载

标准库本身已经对左移运算符<<和右移运算符>>分别进行了重载,使其能够用于不同数据的输入输出,但是输入输出的对象只能是 C++ 内置的数据类型(例如 bool、int、double 等)和标准库所包含的类类型(例如 string、complex、ofstream、ifstream 等)。

如果我们自己定义了一种新的数据类型,需要用输入输出运算符去处理,那么就必须对它们进行重载。通过重载,让其输入输出运算符可以输或者输出类或者更多。
输入输出运算符重载一般需要在类外进行声明,定义为全局函数,同时需要在类中声明为友元函数。

#include<iostream>
using namespace std;
class Math
{
private:
 int m_data;
public:
 friend istream& operator>>(istream& cin, Math& math);
 friend ostream& operator<<(ostream& cout, Math& math);
};
istream& operator>>(istream& cin,Math& math)
{
 cin >> math.m_data;
 return cin;
}
ostream& operator<<(ostream& cout, Math& math)
{
 cout << math.m_data;
 return cout;
}
int main()
{
 Math m1;
 cin >> m1;
 cout << m1;
 system("pause");
 return 0;
}

五、=运算符重载

等号运算符相当于拷贝函数的作用,对类中开辟内存的变量,进行重新开辟内存和赋值。

#include<iostream>
using namespace std;
class Student
{
private:
 char* m_name;
public:
 Student(const char*name);
 Student& operator=(const Student& stu);
 void Getshow();
};
Student::Student(const char*name){
 this->m_name = new char[strlen(name) + 1];
 strcpy(this->m_name, name);
}
Student& Student::operator=(const Student& stu)
{
 if (this->m_name != NULL)
 {
  delete[]this->m_name;
  this->m_name = NULL;
 }
 this->m_name = new char[strlen(stu.m_name) + 1];
 strcpy(this->m_name, stu.m_name);
 return *this;
}
void Student::Getshow()
{
 cout << m_name << endl;
}
int main()
{
 Student stu1("xiaowang");
 Student stu2("xiaokong");
 stu1 = stu2;
 stu1.Getshow();
 system("pause");
 return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值