C++复习Day_5

2022/1/29 1:05:08

本文主要是介绍C++复习Day_5,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

加号运算符重载

  1. 加号运算符重载
    1. 对于内置的数据类型,编译器知道如何进行运算
    2. 但是对于自定义数据类型,编译器不知道如何运算
    3. 利用运算符重载 可以让符号有新的含义
    4. 利用加号重载  实现p1 + p2 Person数据类型相加操作
    5. 利用成员函数  和  全局函数 都可以实现重载
    6. 关键字 operator +
    7. 成员本质  p1.operator+(p2)
    8. 全局本质  operator+(p1,p2)
    9. 简化   p1 + p2
    10. 运算符重载 也可以发生函数重载

 左移运算符重载

    1. 不要滥用运算符重载,除非有需求
    2. 不能对内置数据类型进行重载
    3. 对于自定义数据类型,不可以直接用 cout << 输出
    4. 需要重载 左移运算符 
    5. 如果利用成员 函数重载 ,无法实现让cout 在左侧,因此不用成员重载
    6. 利用全局函数 实现左移运算符重载
      1. ostream& operator<<(ostream &cout, Person & p1)
    7. 如果想访问类中私有内存,可以配置友元实现

 前置后置递增运算符重载

    1. 前置递增
      1.  MyInter& operator++()
    2. 后置递增
      1. MyInter operator++(int)
    3. 前置++ 效率高于 后置++ 效率 ,因为后置++会调用拷贝构造,创建新的数据
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<algorithm>
#include<string>
using namespace std;

class MyInt
{
public:
	MyInt(int a) 
	{
		this->num = a;
	}

	MyInt& operator++()
	{
		this->num++;
		return *this;
	}

	MyInt operator++(int)
	{
		MyInt temp = *this;
		this->num++;
		return temp;

	}

	int num;
};

ostream& operator<<(ostream& cout, const MyInt& m)
{
	cout << m.num << endl;;
	return cout;
}

int main()
{
	MyInt a(10);
	

	system("pause");
	return EXIT_SUCCESS;
}

 指针运算符重载

    1. 智能指针
    2. 用途: 托管new出来的对象的释放
    3. 设计smartPoint智能指针类,内部维护 Person * ,在析构时候释放堆区new出来的person对象
    4. 重载  ->   * 让  sp智能指针用起来向真正的指针
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<algorithm>
using namespace std;

class Person
{
public:
	Person(int age)
	{
		cout << "构造调用" << endl;
		this->m_age = age;
	}

	void showage()
	{
		cout << "年龄:" << this->m_age << endl;
	}

	~Person()
	{
		cout << "析构调用" << endl;
	}

	int m_age;
};


class SmartPoint
{
public:
	SmartPoint(Person* person)
	{
		this->m_person = person;
	}

	Person* operator->()
	{
		return this->m_person;
	}

	Person& operator*()
	{
		return *(this->m_person);
	}

	Person* show()
	{
		return m_person;
	}



	~SmartPoint()
	{
		if (this->m_person)
		{
			delete this->m_person;
			this->m_person = nullptr;
		}
	}

	
private:
	Person * m_person;
};

void test01()
{
	SmartPoint sp(new Person(18));
	sp->showage();
	(*sp).showage();
	
}

int main()
{

	test01();

	system("pause");
	return EXIT_SUCCESS;
}

赋值运算符重载

    1. 编译器会默认个一个类添加4个函数
      1. 默认构造、析构 、  拷贝构造(值拷贝)  、 operator=(值拷贝)
    2. 如果类中有属性创建在堆区,利用编译器提供的 = 赋值运算就会出现 堆区内存重复释放的问题
    3. 解决方案:利用深拷贝  重载 =运算符
    4.   Person& operator=( const Person &p)
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<algorithm>
using namespace std;

class Person
{
public:
	Person() = default;

	Person(const char *name,int age)
	{
		//cout << "构造调用" << endl;
		this->name = new char[strlen(name) + 1];
		strcpy(this->name, name);
		this->m_age = age;
	}

	Person(const Person& p)
	{
		this->name = new char[strlen(p.name) + 1];
		strcpy(this->name, p.name);
		this->m_age = p.m_age;
	
	}

	Person& operator=(const Person &p)
	{
		
		if (this->name != nullptr)
		{
			delete[]this->name;
			this->name = nullptr;
		}
		this->name = new char[strlen(p.name) + 1];
		strcpy(this->name, p.name);
		this->m_age = p.m_age;
		return *this;
	}

	void showage()
	{
		cout << "年龄:" << this->m_age << endl;
	}

	~Person()
	{
		//cout << "析构调用" << endl;
		if (this->name != nullptr)
		{
			delete[]this->name;
			this->name = nullptr;
		}
	}

	char* name;
	int m_age;
};

ostream& operator<<(ostream& cout,const Person &p)
{
	cout << p.name << " " << p.m_age;
	return cout;
}

class SmartPoint
{
public:
	SmartPoint(Person* person)
	{
		this->m_person = person;
	}

	Person* operator->()
	{
		return this->m_person;
	}

	Person& operator*()
	{
		return *(this->m_person);
	}

	Person* show()
	{
		return m_person;
	}



	~SmartPoint()
	{
		if (this->m_person)
		{
			delete this->m_person;
			this->m_person = nullptr;
		}
	}

	
private:
	Person * m_person;
};

void test01()
{
	
	Person p1("Tom", 18);
	Person p2("Jerry", 20);
	Person p3("", 0);
	p3 = p2 = p1;
	Person p4 = p3;
	cout << p1 << endl;
	cout << p2 << endl;
	cout << p3 << endl;
	cout << p4 << endl;

	//cout << p3;
	
	
	
	
}

int main()
{

	test01();

	system("pause");
	return EXIT_SUCCESS;
}

[]重载

  1. []运算符重载
    1.   int& operator[](int index);
    2. 实现访问数组时候利用[] 访问元素
#pragma once
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<algorithm>
using namespace std;

class MyArray
{
public:

	MyArray();

	MyArray(int capacity);

	MyArray(const MyArray& arr);

	void push_Back(int val);

	void set_Data(int pos, int val);

	int get_Data(int pos);

	int get_Capacity();

	int get_Size();

	int &operator[](int pos);
	
	~MyArray();
	
private:

	int m_Capacity;
	int m_Size;
	int* pAddress;
};



#include "标头.hpp"

MyArray::MyArray()
{
	this->m_Capacity = 100;
	this->m_Size = 0;
	this->pAddress = new int[this->m_Capacity];
}

MyArray::MyArray(int capacity)
{
	this->m_Capacity = capacity;
	this->m_Size = 0;
	this->pAddress = new int[this->m_Capacity];
}

MyArray::MyArray(const MyArray& arr)
{
	this->m_Capacity = arr.m_Capacity;
	this->m_Size = arr.m_Size;
	this->pAddress = new int[arr.m_Capacity];

	for (int i = 0; i < m_Size; i++)
	{
		this->pAddress[i] = arr.pAddress[i];
	}

	//memcpy(this->pAddress, arr.pAddress, arr.m_Size);
}

void MyArray::push_Back(int val)
{
	this->pAddress[this->m_Size] = val;
	this->m_Size++;
}

void MyArray::set_Data(int pos, int val)
{
	this->pAddress[pos] = val;
}

int MyArray::get_Data(int pos)
{
	return this->pAddress[pos];
}

int MyArray::get_Capacity()
{
	return this->m_Capacity;
}

int MyArray::get_Size()
{
	return this->m_Size;
}

int &MyArray::operator[](int pos)
{
	return this->pAddress[pos];
}


MyArray::~MyArray()
{
	if (this->pAddress != nullptr)
	{
		delete[] this->pAddress;
		this->pAddress = nullptr;
	}
}



这篇关于C++复习Day_5的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程