C++笔记 day30 数组类封装 运算符重载

123 阅读3分钟

强化训练-数组类封装

设计类 myArray

属性

int m_Capacity数组容量

int m_Size   数组大小

int pAddress 维护真实在堆区创建的数组指针

行为

默认构造

有参构造

拷贝构造

析构

根据位置 设置数据

根据位置  获取数据

尾插

获取数组容量

获取数组大小

示例

1. 头文件

#pragma once
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;

class MyArray 
{
public:
	MyArray(); //默认构造,可以给100容量

	MyArray(int capacity); //有参构造

	MyArray(const MyArray& arr); //拷贝构造

	//尾插法
	void pushBack(int val);
	//根据位置设置数据
	void setData(int pos, int val);
	//根据位置获取数据
	int getData(int pos);
	//获取数组容量
	int getCapacity();
	//获取数组大小
	int getSize();
	//析构函数
	~MyArray();

private:

	int m_Capacity; //数组容量
	int m_Size; //数组大小
	int* pAddress; //真实在堆区开辟的数组的指针
};

2. 实现

# include "MyArray.h"

MyArray::MyArray()
{
	cout << "默认构造函数调用" << endl;
	this->m_Capacity = 100;
	this->m_Size = 0;
	this->pAddress = new int[this->m_Capacity];
}

MyArray::MyArray(int capacity)
{
	cout << "有参构造函数调用" << endl;
	this->m_Capacity = capacity;
	this->m_Size = 0;
	this->pAddress = new int[this->m_Capacity];
}

MyArray::MyArray(const MyArray& arr) //传进的参数跟自身是一样的类,可以直接访问私有内容
{
	cout << "拷贝构造函数调用" << endl;
	this->m_Capacity = arr.m_Capacity;
	this->m_Size = arr.m_Size;
	this->pAddress = new int[arr.m_Capacity];

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

//尾插法
void MyArray::pushBack(int val)
{
	this->pAddress[this->m_Size] = val;
	this->m_Size++;
}
//根据位置设置数据
void MyArray::setData(int pos, int val)
{
	this->pAddress[pos] = val;
}
//根据位置获取数据
int MyArray::getData(int pos)
{
	return this->pAddress[pos];
}
//获取数组容量
int MyArray::getCapacity()
{
	return this->m_Capacity;
}
//获取数组大小
int MyArray::getSize()
{
	return this->m_Size;
}
//析构函数
MyArray::~MyArray()
{
	cout << "析构函数调用" << endl;
	if (this->pAddress != NULL) 
	{
		delete[] this->pAddress;
		this->pAddress = NULL;
	}
}

3. 主函数

#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;
#include "MyArray.h"

void test01() 
{
	MyArray arr;
	for (int i = 0; i < 10; i++)
	{
		arr.pushBack(i);
	}
	for (int i = 0; i < arr.getSize(); i++)
	{
		cout << arr.getData(i) << endl;
	}

	MyArray arr2(arr);
	for (size_t i = 0; i < arr2.getSize(); i++)
	{
		cout << arr2.getData(i) << endl;
	}
	arr.setData(0, 1000);
	for (int i = 0; i < arr.getSize(); i++)
	{
		cout << arr.getData(i) << endl;
	}
}
int main()
{
	test01();


	system("pause");
	return EXIT_SUCCESS;
}

运算符重载

对已有的运算符进行重新定义,赋予其新的功能。

加号运算符重载

image.png

对于内置的数据类型,编译器知道如何进行运算

但是对于自定义数据类型,编译器不知道如何运算

利用运算符重载 可以让符号有新的含义

利用加号重载  实现p1 + p2 Person数据类型相加操作

利用成员函数  和  全局函数 都可以实现重载

关键字 operator +

    成员本质  p1.operator+(p2)

    全局本质  operator+(p1,p2)

    简化   p1 + p2

运算符重载 也可以发生函数重载

示例

#define _CRT_SECURE_NO_WARNINGS
#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 temp;
	//	temp.m_A = this->m_A + p.m_A;
	//	temp.m_B = this->m_B + p.m_B;
	//	return temp;
	//}

	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_B + p2.m_B;
	return temp;
}
Person operator+(Person& p1, int num)
{
	Person temp;
	temp.m_A = p1.m_A +num;
	temp.m_B = p1.m_B + num;
	return temp;
}
//全局函数重载运算符或者成员函数重载运算符两者用一个就可以,否则简化版会出现二义性
//也可以用正常版本的函数进行调用
void test01() 
{
	Person p1(10, 10);
	Person p2(20, 20);

	Person p3 = p1 + p2;

	cout << "p3.m_A = " << p3.m_A << endl;
	cout << "p3.m_B = " << p3.m_B << endl;
	//运算符重载可不可以发生函数重载?可以
	Person p4 = p1 + 100;
	cout << "p4.m_A = " << p4.m_A << endl;
	cout << "p4.m_B = " << p4.m_B << endl;
}

int main()
{

	test01();

	system("pause");
	return EXIT_SUCCESS;
}

左移运算符重载

运算符重载分为一元和二元运算符重载。

不要滥用运算符重载,除非有需求

不能对内置数据类型进行重载

对于自定义数据类型,不可以直接用 cout << 输出

需要重载 左移运算符 

    如果利用成员 函数重载 ,无法实现让cout 在左侧,因此不用成员重载

    利用全局函数 实现左移运算符重载

    *ostream*& operator<<(*ostream* &cout, Person & p1)

如果想访问类中私有内存,可以配置友元实现

示例

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

class Person 
{
	friend ostream& operator<<(ostream& cout, Person& p1);
public:
	Person(int a, int b) 
	{
		this->m_A = a;
		this->m_B = b;
	}
	//利用成员函数 做 << 重载 没法实现cout在左边
	void operator<< (Person& p) //p.operator<<(cout)  p<<cout
	{
	}
private:
	int m_A;
	int m_B;
};

//利用全局函数 实现左移运算符重载
ostream& operator<<(ostream& cout, Person& 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 EXIT_SUCCESS;
}

递增运算符重载

image.png

前置递增:返回引用,允许链式编程

MyInter& operator++()

后置递增:返回值,不允许链式编程

MyInter operator++(**int**)

前置++ 效率高于 后置++ 效率 ,因为后置++会调用拷贝构造,创建新的数据

示例

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

class MyInter 
{
	friend ostream& operator<<(ostream& cout, MyInter myInt);
public:
	MyInter() 
	{
		m_Num = 0;
	}
	//前置++重载,返回的是引用,是对本体做操作
	MyInter& operator++() 
	{
		this->m_Num++;
		return *this;
	}
	//后置++ 重载,返回是值,不是对本体做操作
	MyInter operator++(int) 
	{
		//先记录初始状态
		MyInter temp = *this;
		this->m_Num++;
		return temp;
	}
private: 
	int m_Num;
};

ostream& operator<<(ostream& cout, MyInter myInt) 
{
	cout << myInt.m_Num << endl;
	return cout;
}

void test01() 
{
	MyInter myInt;
	cout<<++(++myInt)<<endl;
	cout << myInt << endl;
}

void test02() 
{
	MyInter myInt;
	cout << myInt++ << endl;

}

int main()
{
	test01();
	test02();

	system("pause");
	return EXIT_SUCCESS;
}

指针运算符重载

智能指针

用途: 托管new出来的对象的释放

设计smartPoint智能指针类,内部维护 Person * ,在析构时候释放堆区new出来的person对象

重载  ->   * 让  sp智能指针用起来向真正的指针

示例

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

class Person 
{
public:
	Person(int age) 
	{
		cout << "Person的有参构造函数调用" << endl;
		this->m_Age = age;
	}
	void showAge() 
	{
		cout <<"年龄为"<< this->m_Age << endl;
	}
	~Person() 
	{
		cout << "Person的析构函数调用" << endl;
	}
	int m_Age;
};

class SmartPoint 
{
public:
	SmartPoint(Person* person) 
	{
		this->m_Person = person;
	}
	//重载->运算符
	Person* operator->() 
	{
		return this->m_Person;
	}
	//重载*运算符
	Person& operator*() 
	{
		return *m_Person;
	}
	~SmartPoint() 
	{
		if (this->m_Person) 
		{
			delete this->m_Person;
			this->m_Person = NULL; 
		}
	}
private:
	Person* m_Person;
};

void test01() 
{
	//Person* p = new Person(18);
	//(*p).showAge();
	//p->showAge();
	//delete p;

	//利用智能指针管理new出来的Person的释放操作
	SmartPoint sp(new Person(18));
	sp->showAge();//本质 sp->->showAge() 编译器 简化为 sp->showAge()
	(*sp).showAge();
}

int main()
{
	test01();


	system("pause");
	return EXIT_SUCCESS;
}

赋值运算符重载

编译器会默认个一个类添加4个函数

 默认构造、析构 、  拷贝构造(值拷贝)  、 operator=(值拷贝)

如果类中有属性创建在堆区,利用编译器提供的 = 赋值运算就会出现 堆区内存重复释放的问题

解决方案:利用深拷贝  重载 =运算符

Person& operator=( const Person &p)

示例:

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

//编译器 默认给1个类4个函数,默认构造、析构、拷贝构造(值拷贝) operator=(值拷贝)
class Person 
{
public:
	Person(const char* name, int age) 
	{
		this->m_Name = new char[strlen(name) + 1];
		strcpy(this->m_Name, name);
		this->m_Age = age;
	}
	//重载 =
	Person& operator=(const Person& p) 
	{
		//先判断原来的堆区是否有内容,如果有先释放
		if (this->m_Name != NULL) 
		{
			delete[] this->m_Name;
			this->m_Name = NULL;
		}
		this->m_Name = new char[strlen(p.m_Name) + 1];
		strcpy(this->m_Name, p.m_Name);
		this->m_Age = p.m_Age;
		return *this;
	}
	//拷贝构造 重载
	Person(const Person& p) 
	{
		this->m_Name = new char[strlen(p.m_Name) + 1];
		strcpy(this->m_Name, p.m_Name);
		this->m_Age = p.m_Age;
	}
	~Person() 
	{
		if (this->m_Name != NULL) 
		{
			delete this->m_Name;
			this->m_Name = NULL;
		}
	}

	char* m_Name;
	int m_Age;
};

void test01() 
{
	Person p1("Tom",10);


	Person p2("Jerry",19);

	Person p3("", 0);

	Person p4 = p3;//要不崩 需要重写拷贝构造
	p3=p2 = p1;//浅拷贝,在释放的时候会出错,重载改为深拷贝
	cout << "p1姓名 "<< p1.m_Name << " p1年龄" <<  p1.m_Age << endl;
	cout << "p2姓名 " << p2.m_Name << " p2年龄" << p2.m_Age << endl;
	cout << "p3姓名 " << p3.m_Name << " p3年龄" << p3.m_Age << endl;
}

int main()
{
	test01();
	int a = 10;
	int b = 20;
	int c;
	c = a = b;
	cout << "a = " << a << " b = " << b << " c = " << c;

	system("pause");
	return EXIT_SUCCESS;
}

[]运算符重载

int& operator[](int index);

实现访问数组时候利用[] 访问元素

想要函数引用能够作为左值存在,不能直接返回数要返回引用

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