Day31.C++05

Day31.C++05

001.加号运算符重载

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

class Person
{
public:
	Person() :m_A(1),m_B(1){}

	Person(int a,int b):m_A(a),m_B(b){}


	//+号运算符重载 成员函数
	/*Person operator+(const 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+(const Person& p1, const 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 = Person(10, 10);
	Person p2 = Person(15, 20);
	Person p3 = p1 + p2;

	cout << "p3.m_A:" << p3.m_A << endl << "p3.m_B:" << p3.m_B << endl;
}

int main(void)
{
	test01();

	system("pause");
	return EXIT_SUCCESS;
}

002.左移运算符重载

注意事项:

- 不要随意乱用符号重载
- 内置数据类型 的运算符不可以重载
- 如果重载时候想访问p1的私有成员,那么全局函数要做Person的友元函数。

代码:

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

class Person 
{
	friend ostream& operator<<(ostream& cout, Person& p);
public:

	Person():m_A(1),m_B(1){}
	Person(int a, int b) :m_A(a), m_B(b) {}
	
	/*void operator<<()//重载左移运算符不可以写到成员函数中
	{

	}*/

private:
	int m_A;
	int m_B;

};

ostream& operator<<(ostream& cout, Person& p)
{
	cout << "m_A: " << p.m_A  << "  m_B: " << p.m_B;
	return cout;
}

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

int main(void)
{
	test01();

	system("pause");
	return EXIT_SUCCESS;
}

003.前置后置运算符的递增递减重载

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

class MyInteger
{
public:
	MyInteger()
	{
		m_Num = 0;
	}
	MyInteger(int a)
	{
		m_Num = a;
	}

	//前置++ 重载(返回值为引用)
	MyInteger& operator++()
	{
		this->m_Num++;
		return *this;
	}

	//后置++ 重载(不返回引用)
	MyInteger operator++(int)
	{
		//先保存当前的数据
		MyInteger temp(*this);
		++this->m_Num;
		return temp;
	}

	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(void)
{
	test01();

	system("pause");
	return EXIT_SUCCESS;
}

004.智能指针的实现

总结:

- Person类有showAge 成员函数
- 如果new出来的Person对象,程序员就要自觉的用delete去释放
- 有了智能指针,让智能指针去托管这个Person对象,对象的释放就可以交给智能指针去管理
- 为了让智能指针像普通的Person*指针一样使用 就要重载 -> 和 *

代码:

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

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

	void showAge()
	{
		cout << "m_Age: " << this->m_Age << endl;
	}

private:
	int m_Age;

};

class smartPointer
{
public:
	smartPointer(Person* person)
	{
		this->m_Person = person;
	}

	~smartPointer()
	{
		cout << "智能指针析构函数调用" << endl;
		if (this->m_Person != NULL)
		{
			delete m_Person;
			m_Person = NULL;
		}
	}

	//重载->   让智能指针对象像Person* p 一样去使用
	Person* operator->()
	{
		return this->m_Person;
	}

	//重载*
	Person& operator*()
	{
		return *(this->m_Person);
	}

private:
	Person* m_Person;
};

void test01()
{
	//Person* p1 = new Person(10);
	
	//delete p1;

	smartPointer sp1(new Person(10));

	sp1->showAge();//sp1->->showAge();//编译器优化了写法

	(*sp1).showAge();

}

int main(void)
{
	test01();

	system("pause");
	return EXIT_SUCCESS;
}

005.赋值运算符重载

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

//一个类默认创建 默认构造、析构、拷贝构造  operator=赋值运算符 进行简单的值传递
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 的年龄:" << p2.m_A << endl;
}

class Person2
{
public:
	Person2(const char* name)
	{
		this->m_Name = new char[strlen(name) + 1];
		strcpy(this->m_Name, name);
	}

	Person2& operator=(const Person2& 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);

		return *this;
	}

	~Person2()
	{
		if (this->m_Name != NULL)
		{
			delete[] this->m_Name;
			this->m_Name = NULL;
		}
	}

	char* m_Name;

};

void test02()
{
	Person2 p1 = Person2("狗蛋");
	Person2 p2("狗剩");
	Person2 p3(" ");
	p3 = p2 = p1;
	

	cout << "p2 的名字" << p2.m_Name << endl;
}

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

	system("pause");
	return EXIT_SUCCESS;
}

006.强化训练—数组类的封装

MyArray.h

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

class MyArray
{
public:
	MyArray();//默认100容量
	MyArray(int capacity);
	MyArray(const MyArray& array);

	~MyArray();

public:
	//尾插法
	void Push_Back(int data);

	//根据索引获取值
	int getData(int index);

	//根据索引设置值
	void setData(int index, int data);

	//获取数组大小
	int getSize();

	//获取数组容量
	int getCapacity();

	//[]中括号运算符的重载
	int& operator[](int index);
	
private:
	int* pAddress;//指向真正存储数据的指针
	int m_Size;//数组大小
	int m_Capacity;//数组容量
};

MyArray.cpp

#include"MyArray.h"

//默认构造
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& array)
{
	cout << "拷贝构造调用" << endl;
	this->m_Capacity = array.m_Capacity;
	this->m_Size = array.m_Size;
	this->pAddress = new int[this->m_Capacity];

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

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

//尾插法
void MyArray::Push_Back(int data)
{
	this->pAddress[this->m_Size] = data;
	this->m_Size++;
}

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

void MyArray::setData(int index, int data)
{
	this->pAddress[index] = data;
}

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

int MyArray::getCapacity()
{
	return this->m_Capacity;
}
//[]重载的实现
int& MyArray::operator[](int index)
{
	return this->pAddress[index];
}

强化训练_数组类的封装.cpp

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

void test01()
{
	//堆区创建数组
	MyArray* array = new MyArray(30);
	for (int i = 0; i < 10; ++i)
	{
		array->Push_Back(i);
	}
	MyArray array2(*array);

	delete array;
	for (int i = 0; i < 10; ++i)
	{
		cout << array2.getData(i) << endl;
	}

	array2.setData(0, 100);
	for (int i = 0; i < 10; ++i)
	{
		cout << array2[i] << endl;
	}
	//获取数组大小
	cout << "array2 的数组大小为" << array2.getSize() << endl;
	//获取数组容量
	cout << "array2 的数组容量为" << array2.getCapacity() << endl;

	//获取和设置数组的内容 如何用[]进行设置和访问
	array2[3] = 100;
	for (int i = 0; i < 10; ++i)
	{
		cout << array2[i] << endl;
	}
}


int main(void)
{
	test01();

	system("pause");
	return EXIT_SUCCESS;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值