c++中运算符重载(加号运算,左移运算,前置后置++运算符,赋值运算,关系运算,函数运算)

运算符重载注意

  1. 重载的运算符要易读
  2. 内置的数据类型的表达式的运算符是不可以改变的
  3. 不要重载&&| | 运算符
  4. =[]->运算符只能通过成员函数进行重载
  5. <<>>只能通过全局函数配合友元函数进行重载

加号运算符重载

  1. 如果想让自定义数据类型 进行**+**运算,那么就需要重载 **+**运算符

  2. 在成员函数 或者 全局函数里 重写 一个**+**运算符的函数

  3. 函数名operate+(){}

  4. 运算符重载可以不停重载,接着重载

     #include<iostream>
     
     using namespace std;
     
     
     class Person
     {
     
     public:
     	Person(){}
     
     	Person(int a, int b) :m_A(a), m_B(b){}
     
     	//+号运算符重载 成员函数
     	/*Person operator+(Person & p)
     	{
     		Person tmp;
     		tmp.m_A= this->m_A + p.m_A;
     		tmp.m_B = this->m_B + p.m_B;
     		return tmp;
     	}*/
     
     	int m_A;
     	int m_B;
     };
     
     //利用全局函数,进行+号运算符的重载
     Person operator+(Person &p1, Person &p2) //二元
     {
     	Person tmp;
     	tmp.m_A = p1.m_A + p2.m_A;
     	tmp.m_B = p1.m_B + p2.m_B;
     	return tmp;
     }
     
     //函数重载 附带运算符再次重载
     Person operator+(Person &p1, int a) //二元
     {
     	Person tmp;
     	tmp.m_A = p1.m_A + a;
     	tmp.m_B = p1.m_B + a;
     	return tmp;
     }
     void test01()
     {
     	Person p1(10, 10);
     	Person p2(10, 10);
     
     	Person p3 = p1 + p2; //p1+p2 从什么表达式转变的? p1.operator(p2) operator(p1,p2)
     
     	Person p4 = p1 + 10;//重载的版本
     	cout << "p3的m_A" << p3.m_A << endl << "p3的m_B" << p3.m_B << endl;
     
     }
     
     int main()
     {
     	test01();
     	system("pause");
     	return 0;
     }
    

左移运算符重载

  1. 不要随意乱用符号重载

  2. 内置数据类型的运算符不可以重载

  3. cout<< 直接对Person自定义数据类型 进行输出

  4. 写到全局函数中 ostream& operator<<(ostream & cout,Person & p1){}

  5. 如果重载时想访问p1的私有成员,那么全局函数要做Person的友元函数

     #include<iostream>
     
     using namespace std;
     
     class Person
     {
     	friend ostream & operator <<(ostream &cout, Person&p1);
     public:
     	Person(){}
     
     	Person(int a, int b)
     	{
     		this->m_A = a;
     		this->m_B = b;
     	}
     
     	/*void operator<<() 重载左移运算符不可以写到成员函数中
     	{
     
     	}*/
     
     private:
     	int m_A;
     	int m_B;
     
     };
     //cout是属于ostream类 
     //如果向访问私有成员,就要用友元函数
     ostream & operator <<(ostream &cout, Person&p1) //第一个参数cout 第二个参数p1
     {
     	cout << "m_A=" << p1.m_A << "m_B=" << p1.m_B;
     	return cout;
     }
     
     void test01()
     {
     	Person p1(10, 10);
     	cout << p1<<endl;
     }
     
     int main()
     {
     	test01();
     	system("pause");
     	return 0;
     }
    

前置,后置 ++运算符重载

  1. 自己实现int类型MyIntege

  2. 内部维护int 数据

  3. MyInteger myint

  4. myint ++ 后置 ++myint前置

  5. 重载++运算符 operator++()前置 operator++(int )后置

  6. 前置理念 先++ 后返回自身 后置理念 先保存原有值 内部++ 返回临时数据

     #include<iostream>
     
     using namespace std;
     
     class MyInteger
     {
     	friend ostream & operator<<(ostream &cout, MyInteger& myInt);
     	
     public:
     	MyInteger()
     	{
     		m_Num = 0;
     	}
     	//前置++重载
     	//返回引用
     	MyInteger & operator++()
     	{
     		this->m_Num++;
     		return *this;
     	}
     	//用占位参数区分前置和后置
     	//后置++重载
     	//返回值,
     	MyInteger operator++(int)
     	{
     		//先保存目前的数据
     		MyInteger tmp = *this;
     		//实际数据++
     		m_Num++;
     		//返回++前的数值
     		return tmp;
     	}
     	//所以前置++好,因为返回引用,少一份开销
     
     	int m_Num;
     };
     
     ostream & operator<<(ostream &cout, MyInteger& myInt)
     {
     	cout << myInt.m_Num;
     	return cout;
     }
     
     void test01()
     {
     	MyInteger myInt;
     
     	//前置++值
     	cout << ++myInt << endl;
     	//后置++值
     	cout << myInt++ << endl;
     	//本身的值
     	cout << myInt << endl;
     }
     
     int main()
     {
     
     	test01();
     	system("pause");
     	return 0;
     }
    

赋值运算符重载

  1. 系统默认给类提供 赋值运算符写法 是简单值拷贝

  2. 导致如果类中有指向堆区的指针,就可能出现深浅拷贝的问题

  3. 所以要重载=运算符

  4. 链式编程return *this

     #define _CRT_SECURE_NO_WARNINGS
     #include<iostream>
     
     using namespace std;
     
     class Person
     {
     public:
     	Person(int a)
     	{
     		this->m_A = a;
     	}
     
     	
     	int m_A;
     };
     
     void test01()
     {
     	Person p1(10);
     	Person p2(0);
     
     	p2 = p1;
     
     	cout << "p2的m_A" << p2.m_A << endl;
     
     }
     
     
     class Person2
     {
     public:
     	Person2(char * name)
     	{
     		this->pName = new char[strlen(name) + 1];
     		strcpy(this->pName, name);
     	}
     	//类中重载 =赋值运算符
     	Person2 & operator=(const Person2 &p)
     	{
     		//判断如果原来已经堆区有内容,先释放
     		if (this->pName != NULL)
     		{
     			delete[]this->pName;
     			this->pName = NULL;
     		}
     		this->pName = new char[strlen(p.pName) + 1];
     		strcpy(this->pName, p.pName);
     
     		return *this;
     	}
     
     
     	~Person2()
     	{
     		if (this->pName != NULL)
     		{
     			delete[]this->pName;
     			this->pName = NULL;
     		}
     	}
     	char * pName;
     };
     
     void test02()
     {
     	Person2 p1("狗蛋");
     
     	Person2 p2("狗剩");
     
     
     	Person2 p3("");
     	
     	p3=p2 = p1;
     	cout << p2.pName << endl;
     	cout << p3.pName << endl;
     
     
     	cout << p2.pName << endl;
     
     	int a = 10;
     	int b = 20;
     	int c;
     	c = a = b; //都是20
     	cout << a << " " << b << " " << c << endl;
     }
     
     int main()
     {
     
     	test02();
     
     	system("pause");
     	return 0;
    
     }
    

关系运算符重载

    #include<iostream>
    #include<string>
    using namespace std;
    
    class Person
    {
    public:
    	Person(string name, int age)
    	{
    		this->m_Name = name;
    		this->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 false;
    		}
    		return true;
    	}
    
    
    	string m_Name;
    	int m_Age;
    };
    
    void test01()
    {
    	Person p1("小明", 10);
    	Person p2("小强", 15);
    
    	Person p3("小强", 15);
    	/*int a = 10;
    	int b = 10;
    	if (a == b)
    	{
    		cout << "a,b相等" << endl;
    	}*/
    
    	if (p1 == p2)
    	{
    		cout << "p1和p2相等" << endl;
    
    	}
    	else
    	{
    		cout << "不相等" << endl;
    	}
    
    	if (p3 == p2)
    	{
    		cout << "p3和p2相等" << endl;
    
    	}
    	else
    	{
    		cout << "不相等" << endl;
    	}
    
    	if (p1 != p2)
    	{
    		cout << "p1和p2不相等" << endl;
    	}
    	else
    	{
    		cout << "相等" << endl;
    	}
    
    }
    
    
    int main()
    {
    
    	test01();
    	system("pause");
    	return 0;
    }

函数调用运算符重载

    #include<iostream>
    #include<string>
    using namespace std;
    
    //     ()重载
    class  MyPrint
    {
    public:
    	void operator()(string text)
    	{
    		cout << text << endl;
    	}
    };
    
    
    void test01()
    {
    	MyPrint myPrint;
    	myPrint("hello world");  //仿函数
    }
    
    class MyAdd
    {
    public:
    	int operator()(int v1,int v2)
    	{
    		return v1 + v2;
    	}
    
    };
    
    void test02()
    {
    	/*MyAdd myAdd;
    	cout << myAdd(1, 1) << endl;
    */
    	cout << MyAdd()(1, 1) << endl; //匿名对象
    }
    
    
    
    
    
    int main()
    {
    	//test01();
    	test02();
    		system("pause");
    		return 0;
    }
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值