C++基础学习(五)

1 加号运算符重载

(1)自定义加法数据类型,需要重载+运算符
(2)在成员函数或者全局函数中重写一个+运算符的函数
(3)函数名 operator+() {}

#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;
 }

 void test()
 {
     Person p1(1,1),p2(2,2);
     Person p3;
     p3 = p1 + p2;
     cout << p3.m_A << " " << p3.m_B << endl;
 }

 int main()
 {
     test();

     return 0;
 }

输出:

3 3

2 左移运算符重载

(1)不要随意乱用符号重载
(2)内置数据类型 的运算符不可以重载
(3)cout << 直接对Person自定义数据类型 进行输出
(4)写到全局函数中ostream& operator << (ostream &cout,Person & p1) {}
(5)如果重载时候想访问p1的私有成员,那么全局函数要做Person的友元函数

#include<iostream>
using namespace std;

 class Person
 {
 public:
    Person() {}
    Person(int a, int b)
    {
        this->m_A = a;
        this->m_B = b;
    }
    friend ostream &operator<<(ostream&cout, Person &p1);
    // 重载左移运算符不可以写到成员函数中
private:
    int m_A;
    int m_B;
};

ostream &operator<<(ostream&cout, Person &p1)
{
    cout << "p1.m_A=" << p1.m_A << " " << "p1.m_B=" << p1.m_B << endl;
    return cout;
}

 void test()
 {
    Person p1(10,10);
    cout << p1 << endl;
 }

 int main()
 {
     test();

     return 0;
 }

输出:

p1.m_A=10 p1.m_B=10

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

(1)myInt++后置 ++ myInt前置
(2)重载++运算符 operator++()前置 operator++(int) 后置
(3)前置理念 先++ 后返回自身 后置理念 先保存住原有值 内部++ 换回临时数据

#include<iostream>
using namespace std;

class MyInteger
{
public:
	MyInteger()
	{
		m_Num = 0;
	}
	// 前置++重载
	MyInteger& operator++()
	{
		this->m_Num++;
		return *this;
	}
	// 后置++重载
	MyInteger operator++(int)
	{
		MyInteger tmp = *this;
		this->m_Num++;
		return tmp;
	}
	friend ostream& operator<< (ostream& out, MyInteger& myInt);
	int m_Num;
	~MyInteger()
	{
		cout << "析构函数" << endl;
	}
};

ostream& operator<< (ostream& out, MyInteger& myInt)
{
	cout << myInt.m_Num;
	return out;
}

void test()
{
	MyInteger myInt;
	cout << ++myInt << endl;
	MyInteger p1 = myInt++;
	cout << p1 << endl;
	// 如果在vs2017开发环境下,下面会报错,但是如果在vs2012开发环境下,则不会。
	//cout << myInt++ << endl;
	cout << myInt << endl;
}

int main()
{
	test();

	return 0;
}

输出:

1
析构函数
1
2
析构函数
析构函数

4 智能指针实现

(1)Person类有showAge成员函数
(2)如果new出来的Person对象,需要手动去释放 delete
(3)有了智能指针,让智能指针托管这个Person对象,这样就不用操心了,让智能指针管理
(4)为了让智能指针向普通的Person指针一样使用 就要重载->和

#include <iostream>
using namespace std;

class Person
{
public:
	Person(int age)
	{
		this->m_Age = age;
	}
	void showAge()
	{
		cout << "年龄为:" << this->m_Age << endl;
	}
	~Person()
	{
		cout << "析构" << endl;
	}
	int m_Age;
};
// 智能指针
// 用来托管自定义类型的对象,让对象进行自动的释放
class smartPointer
{
public:
	smartPointer(Person *person)
	{
		this->person = person;
	}
	// 重载->让智能指针对象 向Person *p一样去使用
	Person* operator->()
	{
		return this->person;
	}
	Person operator*()
	{
		return *this->person;
	}
	~smartPointer()
	{
		cout << "智能指针析构" << endl;
		if (this->person != NULL)
		{
			delete this->person;
			this->person = NULL;
		}
	}
private:
	Person *person;
};

void test()
{
	// Person p1(10); //开辟在栈上,自动释放
	// Person *p1 = new Person(10);// 开辟在堆上,手动释放
	// delete p1;
	smartPointer sp(new Person(10)); // 开辟到栈上,自动释放
	sp->showAge(); // sp->返回的是个指针,应该写成sp->->showAge();单可以这样写,因为编译器给优化了
	(*sp).showAge();
}
int main()
{
	test();

	return EXIT_SUCCESS;
}

输出:

年龄为:10
年龄为:10
析构
智能指针析构
析构

5 赋值运算符重载

(1)系统默认给类提供 赋值运算符写法 是简单值拷贝
(2)导致如果类中有指向堆区的指针,就可能出现深浅拷贝的问题
(3)所以要重载 = 运算符
(4)如果想链式编程 return *this

#include <iostream>
using namespace std;
// 一个类默认创建 默认构造 析构 拷贝构造 opeartor=赋值运算符进行简单的值传递
class Person
{
public:
	Person(int a)
	{
		this->m_A = a;
	}
	Person(const Person &p)
	{
		cout << "拷贝" << endl;
		this->m_A = p.m_A;
	}
	int m_A;
};

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 test()
{
	Person p1(10);

	Person p2(0);
	p2 = p1;
	cout << "p2的m_A" << p2.m_A << endl;
}
void test02()
{
	
	
	Person2 p1("赵信");
	Person2 p2("无极");
	Person2 p3(" ");
	p3 = p2 = p1;
	cout << p2.pName << endl;
	cout << p3.pName << endl;
}

int main()
{
	//test();
	test02();

	return EXIT_SUCCESS;
}

输出:

赵信
赵信

6 关系运算符重载

自定义数据类型 不会内部做比较 == !=,所以要重载

#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 true;
		return false;
	}
	string m_Name;
	int m_Age;
};

void test()
{
	Person p1("小明",10);
	Person p2("小强", 9);
	Person p3("小强", 9);

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

int main()
{
	test();
	return 0;
}

输出:

p1、p2不相等
p2和p3相等
p1、p2不相等

7 函数调用运算符()重载

  • ()仿函数 对象() 看似像函数调用-
  • MyAdd() 匿名对象
#include<iostream>
#include<string>
using namespace std;

class MyPrint
{
public:
	void operator()(string text)
	{
		cout << text << endl;
	}
};

class MyAdd
{
public:
	int operator()(int v1, int v2)
	{
		return v1 + v2;
	}
};

void test01()
{
	MyPrint myPrint;
	myPrint("hello world!"); // 仿函数
}

void test02()
{
	MyAdd myAdd;
	cout << myAdd(1, 1) << endl;
}

int main()
{
	test01();
	test02();
	return EXIT_SUCCESS;
}

输出:

hello world!
2

8 符号重载总结

  • =、[]、()、->操作符只能通过成员函数进行重载
  • <<和>>只能通过全局函数和友元函数进行重载
  • 不要重载&&和||操作符,因为无法实现短路规则

猜你喜欢

转载自blog.csdn.net/qq_42711815/article/details/88865634