C++学习笔记4:运算符重载、类型转换 ⚠ ⚠

运算符重载基本概念
成员函数重载运算符

  • 单目运算符重载
  • 双目运算符重载
  • 重载++、–运算符
  • 重载赋值运算符
  • 重载下标运算符
  • 重载函数调用运算符“()”

友元函数重载运算符
成员函数重载运算符和友元函数重载运算符比较
类型转换

运算符重载基本概念

面向对象程序涉及的重载有函数重载运算符重载
函数重载是指在相同作用域内,若干个参数特征不同的函数使用相同相同的函数名
运算符重载是另一种调用函数的方法是指同样的运算符可以施加于不同类型的操作数,也就是对已有的运算符赋予多重含义,使同一个运算符作用于不同类型的数据时产生不同的行为,是一种静态联编的多态

C++中预定义的运算符只适用于基本数据类型,为了解决一些实际问题,程序员会疆场定义一些新类型,即自定义类型,
然而C++不允许生成新的运算符,因此为了实现对自定义类型的操作,就必须来编写函数说明某个运算符如何作用于这些数据类型,这样程序的可读性较差===》
针对这种情况,C++允许重载现有的大多数运算符,也就是允许给已有的运算符赋予新的含义,从而提高可扩展性,使得针对同样的操作,使用重载运算符比使用显示函数调用更能提高程序的可读性

C++语言对运算符重载的规则如下:
1)不能创造新的运算符,只能重载原先已定义的运算符
2)并不是所有的运算符否可以重载,如“.” ,“.*”, “::”,“?:”不可以进行重载
3)不能改变运算符原有的优先级和结合性
(4)不能改变运算符对预定义类型数据的操作方式,但是可以根据实际需要,对原有运算符进行适当的改造和扩充。
5)运算符重载的两种方式:重载为类的成员函数 、重载为类的友元函数

成员函数重载运算符

成员函数重载运算符的原型在类的内部

class 类名
{
   
   
	…
	返回类型 operator 运算符(形参表)
};
返回类型 类名:operator 运算符(形参表)
{
   
   
	//函数体
}

返回类型是指运算符重载函数的运算结果类型;
operator是定义运算符重载函数的关键字;
运算符是要重载的运算符名称;
形参表中给出了重载运算符所需要的参数和类型

1. 单目运算符

用成员函数重载运算符时,如果运算符是单目的,则参数表为空。因为这时操作数访问该重载运算符的对象本身的数据,该对象有this指针指向

单目运算符的调用:
1)显示调用:对象名.operator 运算符()
2)隐式调用:重载的运算符 对象名

例子👇

//取反
#include<iostream>
#include<iomanip>
using namespace std;

class Point
{
   
   
private:
	int x, y;
public:
	Point(int i= 0, int j = 0);
	Point operator -();//返回类型还是一个类对象
	void Print();
};
Point::Point(int i, int j)//这是构造函数所以没有返回类型
{
   
   
	x= i;
	y= j;
}
void Point::Print()
{
   
   
	cout<<"x = "<< x << setw(5)<<"y="<<y<<endl;
}
Point Point::operator-()//单目运算符无参数,操作数访问重载运算符的对象本身数据
{
   
   
	x = -x;
	y = -y;
	return *this;
}

int main()
{
   
   
	Point ob1(1,2);
	cout<<"ob1:"<<endl;
	ob1.Print();
	cout<<"-ob1:"<<endl;
	ob1.operator-();//显示调用
	//ob = -ob1;//隐式调用
	ob1.Print();
	return 0;
}

重载减号变成取反,单目运算符,函数不用接收参数,无参数,运算符后的操作数有this,x=-1, y=-y表示this->x = -this->x, this->y = -this->y

在这里插入图片描述

2. 双目运算符

重载双目运算符时,左边操作数是访问该重载运算符的对象本身的数据有this指针指向右边操作数通过成员运算符函数的参数指出,所以此时成员运算符函数只有一个参数。

两种调用方式:
1)显示调用:对象名.operator 运算符()
2)隐式调用:对象名 重载的运算符 参数

例子①👇 重载“+”运算符,实现两个字符串相加
(待补充

#include <iostream>
#include <string.h>
using namespace std;
const int Max = 20;
class String 
{
   
   
 
	private:
		char buffer[Max];
		int length;
	public:
		String(char *in_str);
		String();
		String operator +(char *append_str);
		void showstring();
};
 
String::String(char *in_str)
{
   
   
	strcpy(buffer, in_str);
	length = strlen(buffer);
}
 
String::String()
{
   
   
	length = 0;
}
 
String String::operator +(char *append_str)
{
   
   
	String temp;
	int templen;
	templen = strlen(buffer)+strlen(append_str)+1;
	if(templen > Max)
	{
   
   
		cout << "String is too large!" <<endl;
		strcpy(temp.buffer, buffer);
		return temp;
	}
	length = templen;
	strcpy(temp.buffer, buffer);
	strcat(temp.buffer, append_str);
	return temp;
}
 
void String::showstring()
{
   
   
	cout << buffer <<endl;
}
 
int main()
{
   
   
	String title((char*)"C/C++ ");
	title = title + (char*)"Program";             //隐式调用
	//title = title.operator+("Program");         //显式调用,不加char*会出现警告
	title.showstring();
	return 0;
}

例子②👇 用成员函数重载运算符实现复数的加、减运算

(待补充

#include <iostream>
using namespace std;
 
class Complex
{
   
   
	private:
		double real,imag;
	public:
		Complex(double r = 0.0, double i = 0.0);
		void Print();
		Complex operator+(Complex a);
		Complex operator-(Complex a);
 
};
 
Complex::Complex(double r, double i)
{
   
   
	real = r;
	imag = i;
}
 
Complex Complex::operator+(Complex a)
{
   
   
	Complex temp;
	temp.real = real + a.real;
	temp.imag = imag + a.imag;
	return temp;
}
 
Complex Complex::operator-(Complex a)
{
   
   
	Complex temp;
	temp.real = real - a.real;
	temp.imag = imag - a.imag;
	return temp;
}
 
void Complex::Print()
{
   
   
	cout <<real;
	if(imag > 0)
		cout << "+";
	if(imag != 0)
		cout << imag << "i" <<endl;
}
 
int main()
{
   
   
	Complex c1(1.1, 2.2), c2(3.3, 4.4),total;
	total = c1 + c2;
	total.Print();
	total = c1 - c2;
	total.Print();
	return 0;
}

3. 重载++、-- 运算符

C++能识别”++”、”–”运算符是前缀的还是后缀的,它们是单目运算符,分为前缀和后缀两种。

类名 operator ++() //前缀方式

类名 operator ++(int) //后缀方式

类名 operator --() //前缀方式

类名 operator --(int) //后缀方式

例子③👇 重载++、-- 运算符

(待补充

#include <iostream>
#include <iomanip>
using namespace std;
 
class Point
{
   
   
	private:
		int x,y;
	public:
		Point(int i = 0, int j = 0);
		Point operator++();
		Point operator--(); 
		Point operator++(int); 
		Point operator--
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值