c++操作符重载与友元函数 单目 双目 左移右移 ==号操作符重载 自定义数组

#define  _CRT_SECURE_NO_WARNINGS 
#include <iostream>

using namespace std;


class Complex
{
public:
	Complex(int a, int b) {
		this->a = a; 
		this->b = b;
	}

	void printC()
	{
		cout << "(" << this->a << "+" << this->b << "i" << ")"<<endl;//(1+2i)
	}



	Complex AddComplex(Complex &another)
	{
		Complex temp(this->a + another.a, this->b + another.b);

		return temp;
	}

	Complex operator+(Complex &another)
	{
		Complex temp(this->a + another.a, this->b + another.b);

		return temp;
	}

	friend Complex AddComplex(Complex&c1, Complex &c2);
	//friend Complex operator+(Complex &c1, Complex &c2);

private:
	int a;//实数
	int b;//虚数
};

//全局

Complex AddComplex(Complex&c1, Complex &c2)
{
	Complex temp(c1.a + c2.a, c1.b + c2.b);

	return temp;
}

//operator 操作符重载的关键字
#if 0
Complex operator+(Complex &c1, Complex &c2)
{
	Complex temp(c1.a + c2.a, c1.b + c2.b);

	return temp;
}
#endif




int main(void)
{
	Complex c1(1, 2);
	Complex c2(2, 3);




	//Complex c3 = AddComplex(c1, c2);
	//Complex c3 = c1.AddComplex(c2);
	//Complex c3 = operator+(c1, c2); 调用全局的方式
	//Complex c3 = c1.operator+(c2); 调用成员的方式
	Complex c3 = c1 + c2;

	c3.printC();


	return 0;
}



双目运算符


#define  _CRT_SECURE_NO_WARNINGS 
#include <iostream>

using namespace std;

class Complex
{
public:
	Complex(int a, int b) {
		this->a = a;
		this->b = b;
	}

	void printC()
	{
		cout << "(" << this->a << "+" << this->b << "i" << ")" << endl;//(1+2i)
	}

	//friend Complex & operator+=(Complex &c1, Complex &c2);
	//friend Complex & operator-=(Complex &c1, Complex &c2);

	Complex & operator+=(Complex &another)
	{
		this->a += another.a;
		this->b += another.b;

		return *this;
	}

	Complex &  operator-=(Complex &another)
	{
		this->a -= another.a;
		this->b -= another.b;

		return *this;
	}

private:
	int a;//实数
	int b;//虚数
};

//全局
#if 0
Complex & operator+=(Complex &c1, Complex &c2)
{
	c1.a += c2.a;
	c1.b += c2.b;

	return c1;
}

Complex &operator-=(Complex &c1, Complex &c2)
{
	c1.a -= c2.a;
	c1.b -= c2.b;

	return c1;
}
#endif

int main(void)
{
	
	Complex c1(1, 2);
	Complex c2(2, 4);

	//c2 += c1; operator+=(c2, c1)
	(c1 += c2) += c2; //(  c1.operator+=(c2) ).operator+=(c2)
	//c1 += c2;       // operator+=( ( opertor+=(c1,c2) ), c2)

	c1.printC();


	(c1 -= c2)-=c2;

	c1.printC();


	return 0;
}

单目运算符

#define  _CRT_SECURE_NO_WARNINGS 
#include <iostream>

using namespace std;

class Complex
{
public:
	Complex(int a, int b) {
		this->a = a;
		this->b = b;
	}

	void printC()
	{
		cout << "(" << this->a << "+" << this->b << "i" << ")" << endl;//(1+2i)
	}
	//friend Complex & operator++(Complex &c);
	//friend const  Complex  operator++(Complex &c, int);

#if 1

	//前++
	Complex & operator++()
	{
		this->a++;
		this->b++;

		return *this;
	}

	//后++
	const Complex operator++(int)
	{
		Complex temp(this->a, this->b);
		this->a++;
		this->b++;

		return temp;
	}
#endif

private:
	int a;//实数
	int b;//虚数
};


//全局的  前++ ,如果直接重载++ 默认是重载   前++ 运算符
#if 0
Complex & operator++(Complex &c)
{
	c.a++;
	c.b++;

	return c;
}
//想制定后 ++ 运算  后--
const Complex operator++(Complex &c, int) //亚元  用来区分 前++ 和 后++ 的接口的不同
{
	Complex temp(c.a, c.b);
	c.a++;
	c.b++;

	return temp;
}
#endif


int main(void)
{


	Complex c1(1, 2);


	c1++++;

	c1.printC();

	return 0;
}

左移右移运算符


#define  _CRT_SECURE_NO_WARNINGS 
#include <iostream>

using namespace std;

class Complex
{
public:
	Complex(int a, int b) {
		this->a = a;
		this->b = b;
	}

	void printC()
	{
		cout << "(" << this->a << "+" << this->b << "i" << ")" << endl;//(1+2i)
	}


	friend ostream & operator<<(ostream & os, Complex &c);
	friend istream & operator>>(istream &is, Complex &c);


	//重载<<运算符 不能够写在成员函数中, 否则的只能c1<<cout 反向调用 , 语义不通
#if 0
	ostream & operator<<(ostream &os)
	{
		os << "(" << this->a << "+" << this->b << "i" << ")";
		return os;
	}
#endif

private:
	int a;//实数
	int b;//虚数
};

#if 1
ostream & operator<<(ostream & os, Complex &c)
{
	os << "(" << c.a << "+" << c.b << "i" << ")";

	return os;
}

istream & operator>>(istream &is, Complex &c)
{
	cout << "a:";
	cin >> c.a;
	cout << "b:";
	cin >> c.b;

	return is;
}
#endif

int main(void)
{
	Complex c1(1, 2);
	

	cin >> c1;

	cout << c1 << endl;


	//cout << a << endl; // operator<<(cout, int);
	 // operator<<(cout, c1)

	//cout << c1;//cout.operator<<(c1)
				//c1.operator<<(cout)
	//c1 << cout; 

	return 0;
}


==号操作符重载i

#define  _CRT_SECURE_NO_WARNINGS 
#include <iostream>

using namespace std;

class Student
{
public:

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

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

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

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

		//name 深拷贝
		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) {
			//原理name 有额外的内存,需要释放
			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 << "id = " << this->id << ", name = " << this->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(10, "zhang3");

	Student s2 = s1;//s2的拷贝构造函数 Studetn s2(s1)

	s2.printS();

	Student s3(10, "li4");


	
	s3 = s1; //s3.operator=(s1); //会有一个默认的operator= 函数

	s3.printS();

	
	return 0;
}


自定义数组

#pragma once
#define  _CRT_SECURE_NO_WARNINGS 
#include <iostream>

using namespace std;



//int 类型的数组类
class MyArray
{
public:
	MyArray();
	MyArray(int len);
	~MyArray();
	MyArray(const MyArray & another);

	void setData(int index, int value);
	int getData(int index) const;

	int getLen() const
	{
		return this->len;
	}
	
	//  = 号 重载
	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);

	// == !=
	bool operator==(MyArray &another);
	bool operator!=(MyArray &another);

private:
	int len; //这个连续数组空间目前能够存放的元素个数
	int *space; //space 在堆上开辟连续数组空间的首地址
};



#include "MyArray.h"


MyArray::MyArray()
{
	this->space = NULL;
	this->len = 0;
}

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

		//给space 开辟内存
		this->space = new int[this->len];

		cout << "调用了数组有参构造函数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(const MyArray & another)" << endl;
	}
}

MyArray::~MyArray()
{
	//目的是释放 array 额外开辟的space空间
	if (this->space != NULL)
	{
		delete[] this->space;
		this->len = 0;
		this->space = NULL;
	}
	cout << "调用了数组的析构函数~MyArray()" << endl;
}


void MyArray::setData(int index, int value)
{
	if (this->space != NULL)
	{
		this->space[index] = value;
	}
}

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

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 << "执行了 operator=操作符" << endl;

	return *this;
}

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


//全局函数
//如果一个const修饰的对象,或者const修饰的对象指针, 如果通过这个对象 调用对象的成员方法。
//那么这个成员方法的this指针一定要是const类型的。
ostream& operator<<(ostream &os, const MyArray &array)
{
	os << "遍历整个数组" << endl;

	//MyArray*this =  (MyArray*)&array;
	//int getLen(const MyArray *this) // this = &array;
	// getLen() const
	array.getLen();// getLen(&array)
	//array.getLen();//getLen(&array); const MyArray*const this = &array;
	for (int i = 0; i < array.getLen(); i++) {
		//os << array[i]  << endl; //array.operator[](i)
		os << array.getData(i) << endl;
	}

	os << "调用了 《《 重载" << endl;


	return os;
}

 istream& operator>>(istream &is, MyArray &array)
{
	 cout << "请输入 " << array.len << "个数" << endl;

	 for (int i = 0; i < array.len; i++) {
		 cin >> array[i];
	 }

	 return is;
}

 bool MyArray::operator==(MyArray &another)
 {
	 if (this->len != another.len) {
		 return false;
	 }
	 else {
		 for (int i = 0; i < this->len; i++) {
			 if (this->space[i] != another.space[i]) {
				 return false;
			 }
		 }
		 return true;
	 }
 }

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


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

using namespace std;

int main(void)
{
	MyArray array1(10);

	
	array1[0]= 10;  //array1.operator[](0)--->array1.space[0] = 10;

	cin >> array1;



	cout << array1 << endl; // operator<<(cout, array1) 

	MyArray array2 = array1; //Myarray array2(array1);

	cout << array2 << endl;

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

	MyArray array3;

	array3 = array2;


	cout << array3 << endl;

	if (array1 != array3) { // array1.operator==(array2)
		cout << "不等" << endl;
	}
	else {
		cout << "想等" << endl;
	}

	return 0;
}























  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值