【函数重载和运算符重载:加号、递增、赋值、关系和函数调用运算符重载】

函数重载的知识点

函数重载概述:

作用:函数名可以相同,提高复用性
函数重载满足条件:

  • 同一个作用域下
  • 函数名称相同
  • 函数参数类型不同,或者个数不同或者顺序不同

注意:函数的返回值不可以作为函数重载的条件

#include <iostream>
using namespace std;
void fun()
{
	cout << "this is a func" << endl;
}
void fun(int a)
{
	cout << "this is a func(int a)" << endl;
}
int main()
{
	fun(10);
	fun();
	system("pause");
	return 0;
}

//注意事项:
//函数的返回值不可以作为函数重载的条件,这样会有二义性

1.函数重载的注意事项

  • 引用作为重载的条件
#include <iostream>
using namespace std;
void fun(int& a)
{
	cout << "this is a func(int& a)" << endl;
}
void fun(const int& a)
{
	cout << "this is a func(const int& a)" << endl;
}
int main()
{
	fun(10);
	int a = 10;
	fun(a);
	system("pause");
	return 0;
}


  • 函数重载遇到默认参数

因为存在默认参数,在函数调用的时候,不写有的参数也可以调用,就会导致两个函数具有二义性,尽量避免

#include <iostream>
using namespace std;
void fun(int a)
{
	cout << "this is a func(int a)" << endl;
}
void fun(int a, int b = 10)
{
	cout << "this is a func(int a,int b)" << endl;
}
int main()
{
	fun(10, 20);
	//fun(10);两个函数都可以调用,产学生二义性
	system("pause");
	return 0;
}

运算符重载

运算符重载概念:对已有的运算符重新进行定义,赋予其另一种功能,以适应不同的数据类型

加号运算符重载

作用:实现两个自定义数据类型相加的运算

利用成员函数进行加号运算符的重载

#include <iostream>
using namespace std;
class Person {
public:
	int m_A;
	int m_B;
	Person operator+(Person& p) {
		Person temp;
		temp.m_A = this->m_A + p.m_A;
		temp.m_B = this->m_B + p.m_B;
		return temp;
	}

};

void text01() {
	Person p1;
	p1.m_A = 10;
	p1.m_B = 10;
	Person p2;
	p2.m_A = 10;
	p2.m_B = 10;
	Person p3 = p1 + p2;
	cout << p3.m_A << endl;
	cout << p3.m_B << endl;
}
int main()
{
	text01();
	system("pause");
	return 0;
}


利用全局函数进行加号运算符的重载

#include <iostream>
using namespace std;
class Person {
public:
	int m_A;
	int m_B;

};

Person operator+(Person& p1, Person& p2)
{
	Person temp;
	temp.m_A = p1.m_A + p2.m_A;
	temp.m_B = p1.m_A + p2.m_B;
	return temp;
}

void text01() {
	Person p1;
	p1.m_A = 10;
	p1.m_B = 10;
	Person p2;
	p2.m_A = 10;
	p2.m_B = 10;
	Person p3 = p1 + p2;
	cout << p3.m_A << endl;
	cout << p3.m_B << endl;
}
int main()
{
	text01();
	system("pause");
	return 0;
}
2.左移运算符重载
#include <iostream>
using namespace std;

class Person {
public:
	int m_A;
	int m_B;
};


//ostream这个对象只能由一个,只可以用全局函数
ostream& operator<<(ostream& cout, Person& p) {
	cout << "m_A=" << p.m_A << "  m_B= " << p.m_B;
	return cout;
}

void text02()
{
	Person p;
	p.m_A = 10;
	p.m_B = 10;

	cout << p << endl;
}
int main()
{
	text02();
	system("pause");
	return 0;
}

总结:重载左移运算符配合友元可以实现输出自定义的数据类型

递增运算符重载

#define _CRT_SECURE_NO_WARNINGS 1
#include <iostream>
using namespace std;

class Integer
{
	//friend ostream& operator<< (ostream& cout, const Integer& temp);
public:
	Integer() {}

	//前置递增运算符重载:	
	Integer& operator++()
	{
		this->integer++;
		return *this;
	}

	//后置递增运算符重载:
	//注意返回值,非引用
	Integer operator++(int)
	{
		Integer temp = *this;
		this->integer++;
		return temp;
	}

	//不是常函数,是因为传入的是const 修饰的对象,所以形参也要是const修饰的
	//而第一个形参是编译器默认的this指针,不可以显式写出来,所以就在括号后面加一个const表示修饰的是this指针
	void display() const
	{
		cout << this->integer;
	}

protected:
private:
	int integer{};
};

ostream& operator<< (ostream& cout, const Integer& temp)
{
	//cout << "integer = " << temp.integer << endl;
	temp.display();
	return cout;
}

//输出的对象是一个类,需要重载运算符<<
//ostream& operator<< (ostream& cout, const Integer& temp)
//{
//	cout << "integer = " << temp.integer << endl;
//}

void test01()
{
	Integer pinteger01;
	++pinteger01;
	cout << (++pinteger01) << endl;
	cout << pinteger01 << endl;
}

void test02()
{
	Integer pinteger02;
	pinteger02++;
	cout << pinteger02++ << endl;
	cout << pinteger02 << endl;
}
int main()
{
	test01();
	test02();
	system("pause");
	return 0;
}

//第一次调用:
//调用拷贝函数->原来的类的整数值+1->返回一个被拷贝后的类的对象
//第二次调用:
//调用拷贝函数(被拷贝的类的整数值已经在原来的基础上自增了)->原来的类的整数值+1->返回一个被拷贝后的类的对象

赋值运算符重载

#include <iostream>
#include<string>
using namespace std;

class People
{
public:
    People(string name = "", int* ptr = NULL);  // 普通构造函数,
    People(const People& peo);   //显示声明拷贝构造函数
    ~People();
    void Display();
    void SetAge(int age);
private:
    string m_name;
    int* mp_age;

};


People::People(string name, int* ptr)
{
    m_name = name;
    mp_age = ptr;
}

People::People(const People& peo)
{
    this->m_name = peo.m_name;
    this->mp_age = new int(*peo.mp_age);  //重新申请一块内存来存放 age,避免两个对象使用同一块内存
}

People::~People()
{
    delete mp_age;  // 不重载赋值运算符时多次释放内存会导致崩溃。
    mp_age = NULL;
}


void People::Display()
{
    cout << m_name << " is age " << *mp_age << endl;
}

void People::SetAge(int age)
{
    *mp_age = age;
}

int main()
{
    int* ptr = new int(10);
    string name = "Xiao Ming";
    People people1 = People(name, ptr);
    People people2;
    people2 = people1;  //不重载赋值运算符

    people1.Display();
    people2.Display();

    people1.SetAge(15);  // 修改 people1 age

    people1.Display();
    people2.Display();

    system("pause");

    return 0;
}


关系运算符重载

#include<iostream>
using namespace std;
#include<string>

//重载关系运算符

class Person
{
public:
	Person(string name, int age)
	{
		m_name = name;
		m_age = age;
	}

	//重载==号
	bool operator==(Person& p)
	{
		if (this->m_name == p.m_name && this->m_age == p.m_age)
			return true;
		return false;
	}

	bool operator!=(Person& p)
	{
		if (this->m_name != p.m_name || this->m_age != p.m_age)
			return true;
		return false;
	}
	string m_name;
	int m_age;
};


void test1()
{
	Person p1("Tom", 18);
	Person p2("Tom", 18);

	if (p1 == p2)
	{
		cout << "p1==p2" << endl;
	}
	else
	{
		cout << "p1和p2是不相等的!" << endl;
	}
	if (p1 != p2)
	{
		cout << "p1!=p2" << endl;
	}
	else
	{
		cout << "p1和p2是相等的!" << endl;
	}
}


int main()
{
	test1();

	system("pause");
	return 0;
}

函数调用运算符重载

#include <iostream>
#include <string>
using namespace std;
//函数调用运算符重载

//打印输出类
class MyPrint
{
public:
	//重载函数调用运算符
	void operator()(string test)
	{
		cout << test << endl;
	}
};
void MyPrint02(string test)
{
	cout << test << endl;
}
void test01()
{
	MyPrint myPrint;
	myPrint("hello world");//由于使用起来非常类似于函数调用,因此称为仿函数
	MyPrint02("hello world");
}
//仿函数非常灵活,没有固定的写法
//加法类
class MyAdd
{
public:
	int operator()(int num1, int num2)
	{
		return num1 + num2;
	}

};
void test02()
{
	MyAdd myadd;
	int ret = myadd(100, 100);
	cout << "ret=" << ret << endl;

	//匿名的函数对象
	cout << MyAdd()(100, 100) << endl;
}
int main()
{
	test01();
	test02();
	system("pause");
	return 0;
}


运算符重载的实验结果

函数的返回值不可以作为函数重载的条件

在这里插入图片描述

引用作为函数重载的条件

在这里插入图片描述

函数重载遇到默认参数

在这里插入图片描述

加法运算符——利用成员函数进行加法运算符的重载

在这里插入图片描述

加法运算符——利用全局函数进行加法运算符的重载

在这里插入图片描述

左移运算符的重载

在这里插入图片描述

递增运算符的重载

在这里插入图片描述

赋值运算符的重载

在这里插入图片描述

关系运算符的重载

在这里插入图片描述

函数调用运算符的重载

在这里插入图片描述

总结:通过这次实验,我基本掌握了通过运算符重载实现多态性的方法,学会了运算符重载的成员函数法和友元函数法,基本能够区分单目运算符的前置与后置。运算符重载是对已有的运算符赋予多重含义,使一个运算符作用域不同类型的数据导致不同行为的发生,C++为运算符重载提供一种方法,即运算符重载函数,其函数的名字规定为operator后紧跟重载运算符。重载运算符函数可以对运算符做出新的解释,即可以定义用户所需要的各种操作,但是运算符重载后,原有的基本语义不变,不改变运算符的优先级,不改变运算符的结结合性,不改变运算符所需要的操作数,不能创建新的运算符,优先级和结合性主要体现在重载运算符的使用上,而操作数的个数不但体现在重载运算符的使用上,更关系到函数定义时的参数设定。运算符的功能不能直接对他们进行操作,所以就需要我们对运算符进行重载,实际意思就是赋予运算符新的功能,让他们能够对我们自己定义的类和结构体进行操作。通过运算符的重载就能够看到C++的优越性,但是因为自己的基础不够好,所以应该多多锻炼,才能更加熟悉C++。

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值