c++运算符重载(基于黑马程序员)

1.加号运算符重载
运算符重载的俩种方式:全局函数重载和成员函数重载

#include <iostream>
#include <stdio.h>
using namespace std;
//运算符重载,也可以发生函数重载
class Person {
public://1.成员函数重载+//Person operator+ (Person &p)
//{
//	Person temp;
//	temp.m_a = m_a + p.m_a;
//	temp.m_b = m_b + p.m_b;
//	return temp;
//}
	int m_a, m_b;

};
//2.全局函数重载+号
Person operator+ (Person& p1, int num) {
	Person temp;
	temp.m_a = p1.m_a + num;
	temp.m_b = p1.m_b + num;
	return temp;
}
Person operator+(Person& p1, Person& p2) {
	Person temp;
		temp.m_a = p1.m_a + p2.m_a;
		temp.m_b = p1.m_b + p2.m_b;
		return temp;
}
void test01() {
	Person p1;
	p1.m_a = 10, p1.m_b = 10;
	Person  p2;
	p2.m_a = 10, p2.m_b = 10;
	//本质 Person p3 = operator+(p1,p2);
	Person p3 = p1 + p2;
	cout << p3.m_a<<endl;
	//重载的
	Person p4 = operator+(p1, 100);
	cout << p4.m_a;
}
int main() {
	test01();
	return  0;
}

2.重载左移运算符<<
实现:自定义的数据类型输出
只能用全局函数重载,运用链式思想加友元去实现
cout 属于ostream类

#include <iostream>
#include <stdio.h>
using namespace std;
//左移运算符重载 
class Person {
    利用成员函数重载左移运算符
    // 不好利用成员函数去重载<<,因为无法实现cout在左侧
    //void operator<<(cout)
    //{

    //}
    friend ostream& operator<<(ostream& cout, Person& p);
    int m_a=10, m_b=10;
};
//只能利用全局函数重载<<,cout传入引用**
//链式的编程思想返回cout能够继续使用<<
ostream & operator<<(ostream &cout, Person &p)//本质operator<<(cout,p)简化cout <<p;
{
    cout << "m_a= " << p.m_a << " ,m_b =" << p.m_b;
    return cout;
}
void test01() {
    Person p;
    //p.m_a = 10;
    //p.m_b = 10;
    cout << p << endl;
}
int main() {
    test01();
    return 0;
}

3。递增运算符重载
占位重载的问题

#include <iostream>
#include <stdio.h>
using namespace std;
//递增运算符重载
 class MyInteger{
 	friend	ostream &operator<<(ostream& cout , MyInteger myint);
 	public:
 		MyInteger()
 		{
 			m_Num=0;
		 }
  //重载前置++,返回引用是为了一直对一个对象操作 
  MyInteger& operator++(){
  	//先++ 
	  m_Num++;
	  //返回引用本体,链式编程 
  	return *this;
  } 
  //重载后置++运算符,暂时不会链式编程,, 
  // operator++(int)   int 表示占位,区分前置和后置 递增 
  MyInteger operator++(int)
  {
  	//先记录
	  MyInteger temp=*this;
	  //然后++ 
	  m_Num++;
	  //返回记录的值 
	  return temp; 
  }
	private:
	     int m_Num;
 };
 ostream &operator<<(ostream& cout ,MyInteger myint)
 {
 	cout <<myint.m_Num;
 }
 
 void test01()
 {
 	MyInteger myint;
 	cout <<++myint;
 	cout <<myint++;
 }
 int main(){
	test01();
	return 0;
}

4.赋值运算符重载
创建堆区出现深浅拷贝的问题,避免出现重复删除堆区的数据,被忘了析构也要重新写
在这里插入图片描述

#include <iostream>
#include <stdio.h>
using namespace std;
//深浅拷贝是动态分配的问题,需要自己重新写拷贝构造函数
class Person {
public:
	Person(int age)
	{
		m_Age = new int(age);
	}
	~Person() {
		if (m_Age != NULL)
			delete m_Age;
		m_Age = NULL;
	}
	//链式编程的思想返回引用
	Person& operator=(Person& p)
	{//编译器是提供浅拷贝
		//m_Age = p.m_Age;
		// 进行赋值的时候可能之前已经有值所以
		//应该先判断是否有属性在堆区,如果有应该先释放干净,然后再深拷贝
		if (m_Age != NULL)
		{
			delete m_Age;
			m_Age = NULL;
		}
		//再进行深拷贝
		m_Age = new int (*p.m_Age);
		return *this;
	}
	int* m_Age;
};
void test01()
{
	Person p1(18);
	Person p2(20);
	Person p3(30);
	p3 = p2= p1;//浅拷贝的问题,如果没有自己些拷贝构造就会重复释放堆区的内存 
	cout << "p1的年龄为:" << *p1.m_Age << endl;
	cout << "p2的年龄为:" << *p2.m_Age << endl;
	cout << "p3的年龄为:" << *p3.m_Age << endl;
}
int main() {
	test01();
	
	return 0;
}

4.关系运算符重载
学了前面这就是一个练习(不等于自己写qaq)

#include<iostream>
#include <stdio.h>
#include <string>
using namespace std;
//重载关系运算符 
class Person {
	public:
		Person (string name,int age)
		{
			m_Name= name;
			m_Age = age;
		}
		bool operator==(Person &p)
		{
			if(m_Name==p.m_Name && m_Age ==p.m_Age)
			return true;
			else return false; 
		}
		string m_Name;
		int m_Age;
		
};
void test01(){
	Person p1("tom",18);
	Person p2("tom",19);
	if(p1==p2)  cout<<"相等";
	else cout <<"不相等"; 
}
int main(){
	test01();
	return 0;
}

5.函数调用运算符重载
重载(),即为仿函数,学了一点点基础的

#include <iostream>
#include <stdio.h>
using namespace std;
class MyPrint
{
	public:
		void operator()(string test)
		{
			cout <<test <<endl;
		}
};
void test01(){
	MyPrint myprint ;
	myprint("hello world");//由于使用起来非常想函数,因此叫仿函数 
}
class MyAdd 
{
	public:
		int operator()(int num1 ,int num2)
		{
			return num1+num2;
		}
};
void test02()
{
	MyAdd myadd;
cout <<	myadd(100,100)<<endl;
//MyAdd()匿名对象 
cout <<MyAdd()(100,100);
}
int main(){
	test01();
	test02();
	return 0;
}
  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值