c++-重载等号,数组,指针,字符串类

0
11

重载

  • 重载=操作符
    • 1先释放旧对象资源
    • 2用一个对象=给另外一个对象
    • 3函数返回值当左值 返回一个引用
    • 4 数组类 Array& operator=(Array& a1);
    • 5 字符串类:MyString& operator=(const MyString& obj);
    • char& operator[](int index) const;
  • 运算符重载难点
    • 重载=操作符
      • 1先释放旧对象资源
      • 2用一个对象=给另外一个对象
      • 3函数返回值当左值 返回一个引用
      • 4 数组类 Array& operator=(Array& a1);
      • 5 字符串类:MyString& operator=(const MyString& obj);
    • char& operator[](int index) const;
    • 重载[]数组下标
      • 1 返回元素的引用,支持a[i]当左值 a[i]右值
      • 2 数组类 int& operator[](int i);
      • 3 字符串类 char& operator[](int index) const;
    • && 和 || 不能被重载
    • ()重载
  • 案例:数组类 运算符重载优化
  • 案例:字符串类 运算符重载

等号运算符重载

  • 重载=操作符
    • 1 先释放旧对象资源
    • 2 用一个对象=给另外一个对象
    • 3 函数返回值当左值 返回一个引用
    • 4 数组类 Array& operator=(Array& a1);
    • 5 字符串类:MyString& operator=(const MyString& obj);
    • char& operator[](int index) const;
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <string.h>


using namespace std;

class Student
{
public:
	Student()
	{
		this->id = 0;
		this->name = NULL;
	}

	Student(int id, char *name)
	{
		this->id = id;

		//this->name = name;
		int len = strlen(name);
		this->name = new char[len + 1];
		strcpy(this->name, name);
	}

	Student(const Student &another)
	{
		this->id = another.id;

		//深拷贝
		int len = strlen(another.name);
		this->name = new char[len + 1];
		strcpy(this->name, another.name);
	}

	Student & operator=(const Student &another)
	{
		//1 防止自身赋值
		if (this == &another) {
			return *this;
		}

		//2 先将自身的额外开辟的空间回收掉
		if (this->name != NULL) {
			delete[] this->name;
			this->name = NULL;
			this->id = 0;
		}

		//3 执行深拷贝
		this->id = another.id;

		int len = strlen(another.name);
		this->name = new char[len + 1];
		strcpy(this->name, another.name);


		//4 返回本身
		return *this;
	}

	void printS()
	{
		cout << name << endl;
	}

	~Student() {
		if (this->name != NULL) {
			delete[] this->name;
			this->name = NULL;
			this->id = 0;
		}
	}
private:
	int id;
	char *name;
};

int main(void)
{

	Student s1(1, "zhang3");
	Student s2(s1);

	s2 = s1;

	Student s3(2, "li4");


	//s2 = s3 = s1;//s2 = 赋值操作符



	s1.printS();
	s2.printS();
	s3.printS();


	return 0;
}

自定义数组类运用重载

main.cpp

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include "MyArray.h"

using namespace std;



int main(void)
{

 MyArray array1(10);//开辟10元素的数组

	//赋值操作
	for (int i = 0; i < 10; i++) {
		//array1.setData(i, i + 10);
		array1[i] = i + 10;//space[1] = 1+10
	}

	cout << "--------" << endl;

	cout << "array1:" << endl;
	for (int i = 0; i < 10; i++) {
		cout << array1[i] << " ";
	}
	cout << endl;

	MyArray array2 = array1;
	cout << "array2:" << endl;
	for (int i = 0; i < array2.getLen(); i++) {
		cout << array2[i] << " ";
	}
	cout << endl;
	

	cout << " ------------" << endl;
	MyArray array3(5);

	cin >> array3;


	cout << "array3:" << endl;
	cout << array3 << endl;
	cout << endl;

	
	if (array3 != array1)  {
		cout << "不相等" << endl;
	}
	else {
		cout << "相等 " << endl;
	}

	return 0;
}

Array.cpp

#include "MyArray.h"


MyArray::MyArray()
{
	cout << "MyArray()..." << endl;
	this->len = 0;
	this->space = NULL;
}

MyArray::MyArray(int len)
{
	if (len <= 0) {
		this->len = 0;
		return;
	}
	else {
		this->len = len;

		//给space开辟空间
		this->space = new int[this->len];
		cout << "MyArray::MyArray(int len) ..." << endl;
	}
}
MyArray::MyArray(const MyArray &another)
{
	if (another.len >= 0) {
		this->len = another.len;

		//深拷贝
		this->space = new int[this->len];
		for (int i = 0; i < this->len; i++) {
			this->space[i] = another.space[i];
		}
		cout << "MyArray::MyArray(const MyArray &another) ..." << endl;

	}
}
MyArray::~MyArray()
{
	if (this->space != NULL) {
		delete[]this->space;
		this->space = NULL;
		len = 0;
		cout << "MyArray::~MyArray() ..." << endl;
	}
}

void MyArray::setData(int index, int data)
{
	if (this->space != NULL) {
		this->space[index] = data;
	}
}
int MyArray::getData(int index)
{
	return this->space[index];
}
int MyArray::getLen() const
{
	return this->len;
}

MyArray&  MyArray::operator=(const MyArray& another)
{
	if (this == &another) {
		return *this;
	}

	if (this->space != NULL) {
		delete[]this->space;
		this->space = NULL; 
		this->len = 0;
	}

	if (another.len >= 0) {
		this->len = another.len;

		//深拷贝
		this->space = new int[this->len];
		for (int i = 0; i < this->len; i++) {
			this->space[i] = another.space[i];
		}
		cout << "MyArray::operator=(const MyArray& another) ..." << endl;

	}

	return *this;
}

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


ostream &operator<<(ostream &os,const MyArray &array)
{
	os << "遍历整个数组 " << endl;
	//array.getLen(); //getLen(&array);
	for (int i = 0; i < array.getLen(); i++) {
		os << array[i] <<" ";//array.operator[]( i)
	}

	os << "调用的<<操作符重载" << endl;

	return os;
}

istream &operator>>(istream &is, MyArray &array)
{
	cout << "请输入" << array.getLen() << "个数" << endl;
	for (int i = 0; i < array.getLen(); i++) {
		cin >> array[i];
	}
	return is;
}


bool operator==(MyArray &array1, MyArray &array2)
{
	if (array1.len != array2.len) {
		return false;
	}

	for (int i = 0; i < array1.len; i++) {
		if (array1.space[i] != array2.space[i]) {
			return false;
		}
	}

	return true;
}
bool MyArray::operator!=(MyArray &another)
{
	return !(*this == another);
}

Array.h

#pragma once
#include <iostream>

using namespace std;

class MyArray
{
public:
	MyArray();
	MyArray(int len);
	MyArray(const MyArray &another);
	~MyArray();

	void setData(int index, int data);
	int getData(int index);
	int getLen() const ;

	MyArray& operator=(const MyArray& another);

	int & operator[](int index) const;

	friend ostream &operator<<(ostream &os,const MyArray &array);
	friend istream &operator>>(istream &is, MyArray &array);

	friend bool operator==(MyArray &array1, MyArray &array2);
	bool operator!=(MyArray &another);
private:
	int len;
	int *space;
};

重载小括号

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>


using namespace std;

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

	int operator()(int value)
	{
		return value * value;
	}

	int operator()(int value1, int value2)
	{
		return value1 * value2;
	}


private:
	int a;
};

void func(int a)
{

}

int main(void)
{
	Sqr s(10);

	int value = s(2);
	//s.operator()(2);
	
	//将一个对象 当成一个普通函数来调用。
					//称这种对象是 仿函数,伪函数, 函数对象
	 

	cout << value << endl;

	value = s(10, 20);
	
	cout << value << endl;


	return 0;
}

重载new和delete运算符

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <iostream>
#include <fstream>


using namespace std;

class A
{
public:
	A()
	{
		cout << "A()..." << endl;
	}
	A(int a) {
		cout << "A(int)..." << endl;
		this->a = a;
	}

	//重载的new操作符 依然会触发对象的构造函数
	void * operator new(size_t size)
	{
		cout << "重载了new操作符" << endl;
		return malloc(size);
	}

	void *operator new[](size_t size)
	{
		cout << "重载了new[]操作符" << endl;
		return malloc(size);
	}
	void operator delete(void * p)
	{
		cout << "重载了delete操作符" << endl;
		if (p != NULL) {
			free(p);
			p = NULL;
		}
	}

	void operator delete[](void *p)
	{
		cout << "重载了delete[]操作符" << endl;
		if (p != NULL) {
			free(p);
			p = NULL;
		}
	}

	~A() {
		cout << "~A().... " << endl;
	}

private:
	int a;
};

int main(void)
{
	//char *array = malloc(sizeof(char)* 80);

	//int *value_p = new int;

	A *array_p = new A[10];


	//array_p->operator new[](sizeof(A[10]));
	delete[] array_p;

	A *ap = new A(10);

	//ap->operator new(sizeof(A));

	delete ap;
	
	return 0;
}

重载&&和||(不建议重载)

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>


using namespace std;


class Test
{
public:
	Test(int value) {
		this->value = value;
	}

	Test operator+(Test &another)
	{
		cout << "执行了+操作符重载" << endl;
		Test temp(this->value + another.value);
		return temp;
	}

	bool operator&&(Test &another)
	{
		cout << "执行了&&操作符重载" << endl;
		if (this->value && another.value) {
			return true;
		}
		else {
			return false;
		}

	}

	bool operator||(Test &another)
	{
		cout << "重载了||操作符" << endl;
		if (this->value || another.value) {
			return true;
		}
		else {
			return false;
		}
	}

	~Test(){
		cout << "~Test()..." << endl;
	}
private:
	int value;
};

int main(void)
{
	int a = 1;
	int b = 20;


	Test t1(0);
	Test t2(20);

	//重载&&操作符,并不会发生短路现象。

	if (t1 && (t1+t2) ) {	//t1.operator&&(  t1.operator+(t2)  )
		cout << "为真" << endl;
	}
	else {
		cout << "为假" << endl;
	}

	cout << "------" << endl;

	if (t1 || (t1 + t2)) {//t1.operator||(  t1.operator+(t2) )
		cout << "为真" << endl;
	}
	else {
		cout << "为假" << endl;
	}


	return 0;
}

自定义智能指针

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <memory>


using namespace std;

class A
{
public:
	A(int a)
	{
		cout << "A()..." << endl;
		this->a = a;
	}

	void func() {
		cout << "a = " << this->a << endl;
	}

	~A() {
		cout << "~A()..." << endl;
	}
private:
	int a;
};


class MyAutoPtr
{
public:
	MyAutoPtr(A * ptr)
	{
		this->ptr = ptr;//ptr = new A(10)
	}

	~MyAutoPtr() {
		if (this->ptr != NULL) {
			cout << "delte ptr" << endl;
			delete ptr;
			this->ptr = NULL;
		}
	}

	A* operator->()
	{
		return this->ptr;
	}


	A& operator*()
	{
		return *ptr;
	}

private:
	A *ptr;
};

void test1()
{
#if 0
	A* ap = new A(10);

	ap->func();
	(*ap).func();

	delete ap;
	auto_ptr<int> ptr(new int);
#endif
	auto_ptr<A> ptr(new A(10));

	ptr->func();
	(*ptr).func();
}


void test2()
{
	MyAutoPtr my_p(new A(10));

	my_p->func(); //my_p.ptr -> func()
	(*my_p).func(); //  *ptr.func()
}
int main(void)
{
	
	//test1();
	test2();

	return 0;
}

自定义字符串类

main.cpp

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <string>
#include "MyString.h"

using namespace std;

int main(void)
{
	string s1;
	MyString s1("abc");
	MyString s2("123");

	//cout << s1 + s2 << endl;

	cout << s1 << endl;
	cout << s2 << endl;


#if 0
	MyString s1("abc");
	MyString s2(s1);
	MyString s3 = "123";


	cout << s1 << endl;
	cout << s2 << endl;

	s1[1] = 'x';

	cout << s1 << endl;

	s1 = s3;

	cout << s1 << endl;

#endif
	return 0;
}

String.cpp

#include "MyString.h"


MyString::MyString()
{
	this->len = 0;
	this->str =NULL;
}

MyString::MyString(const char *str)
{
	if (str == NULL) {
		this->len = 0;
		this->str = new char[0 + 1];
		strcpy(this->str, "");
	}
	else {
		int len = strlen(str);
		this->len = len;

		this->str = new char[len + 1];
		strcpy(this->str, str);
	}
}

//初始化时候被调用的
MyString::MyString(const MyString &another)
{
	this->len = another.len;
	this->str = new char[this->len + 1];
	strcpy(this->str, another.str);
}



MyString::~MyString()
{
	if (this->str != NULL) {
		cout << this->str << "执行了析构函数" << endl;
		delete this->str;
		this->str = NULL; 
		this->len = 0;
	}
}

char & MyString::operator[](int index)
{
	return this->str[index];
}

MyString &  MyString::operator=(const MyString &another)
{
	if (this == &another) {
		return *this;
	}

	if (this->str != NULL) {
		delete[] this->str;
		this->str = NULL;
		this->len = 0;
	}

	this->len = another.len;
	this->str = new char[this->len + 1];
	strcpy(this->str, another.str);

	return *this;
}

ostream & operator<<(ostream &os, MyString&s)
{
	os << s.str;
	return os;
}

istream & operator>>(istream &is, MyString &s)
{
	//1 将s之前的字符串释放掉
	if (s.str != NULL) {
		delete[] s.str;
		s.str = NULL;
		s.len = 0;
	}

	//2 通过cin添加新的字符串
	char temp_str[4096] = { 0 };
	cin >> temp_str;

	int len = strlen(temp_str);
	s.str = new char[len + 1];
	strcpy(s.str, temp_str);
	s.len = len;

	return is;
}

MyString MyString::operator+(MyString &another)
{
	MyString temp;

	int len = this->len + another.len;

	temp.len = len;

	temp.str = new char[len + 1];
	memset(temp.str, 0, len + 1);
	strcat(temp.str, this->str);
	strcat(temp.str, another.str);

	return temp;
}


String.h

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

class MyString
{
public:
	MyString();
	//MyString(int len); //创建一个长度是len的string对象
	MyString(const char *str);
	MyString(const MyString &another);
	~MyString();

	//重载操作符[]
	char &operator[](int index);



	//重载操作符>>
	friend istream & operator>>(istream &is, MyString &s);

	//重载=操作符
	MyString & operator=(const MyString &another);

	//重载==操作符

	//重载!=操作符


	//重载+操作符
	MyString operator+(MyString &another);


	//重载操作符<<
	friend ostream & operator<<(ostream &os, MyString&s);

private:
	int len;
	char *str;
};


<

发布回复

请输入评论!
请输入你的名字