#include <iostream>
using namespace std;
//运算符重载使得用户自定义的数据以一种更简洁的方式工作
class Complex
{
private:
int a;
int b;
friend Complex operator+(Complex &c1,Complex &c2);
//前置++
friend Complex& operator++(Complex &c2);
//后置++
friend Complex operator++(Complex &c2,int);
public:
void printCom()
{
cout<<a<<"+"<<b<<"i"<<endl;
}
Complex(int a=0,int b=0)
{
this->a=a;
this->b=b;
}
//通过类实现-操作
Complex operator-(Complex &c2)
{
Complex tmp;
tmp.a=this->a-c2.a;
tmp.b=this->b-c2.b;
return tmp;
}
//前置--
Complex& operator--()
{
this->a--;
this->b--;
return *this;
}
protected:
};
Complex& operator++(Complex &c2)
{
c2.a++;
c2.b++;
return c2;
}
Complex operator+(Complex &c1,Complex &c2)
{
Complex temp;
temp.a=c1.a+c2.a;
temp.b=c1.b+c2.b;
return temp;
}
//后置++
Complex operator++(Complex &c2,int)
{
//先使用c2的属性,然后让属性++
/*return c2;
c2.a++;
c2.b++;*/
Complex tmp;
tmp=c2;
c2.a++;
c2.b++;
return tmp;
}
//1操作符重载,首先是通过函数实现的
void main()
{
Complex c1(1,2),c2(3,4);
//c1=c1+c2;//c1 的类型是Complex ,这种类型,是自定义类型。编译器根本不知道如何加,但是c++ 编译器会提供一个机制,让你实现自定义类型加
//Complex c3=ComAdd(c1,c2);
//Complex c3=operator+(c1,c2);
//2 + 操作符 有两个参数 左操作数和右操作数
//3
Complex c3=c1+c2;
// 通过类的成员函数,完成操作符重载
//1,要承认操作符号重载是一个函数,要写函数原型
//2,写出函数调用语言
//c1.operator-(c2);
// c3.printCom();
Complex c4=c1-c2;
//c4.printCom();
//Complex& operator++(Complex &c2);
++c2;
c2.printCom();
/*--c1;
c1.printCom()*/;
c1++;
system("pause");
}
#include <iostream>
using namespace std;
//运算符重载使得用户自定义的数据以一种更简洁的方式工作
class Complex
{
private:
int a;
int b;
friend Complex operator+(Complex &c1,Complex &c2);
//前置++
friend Complex& operator++(Complex &c2);
//后置++
friend Complex operator++(Complex &c2,int);
friend ostream& operator<<(ostream&out,Complex&c1 );
public:
void printCom()
{
cout<<a<<"+"<<b<<"i"<<endl;
}
Complex(int a=0,int b=0)
{
this->a=a;
this->b=b;
}
//通过类实现-操作
Complex operator-(Complex &c2)
{
Complex tmp;
tmp.a=this->a-c2.a;
tmp.b=this->b-c2.b;
return tmp;
}
//前置--
Complex& operator--()
{
this->a--;
this->b--;
return *this;
}
protected:
};
ostream& operator<<(ostream&out,Complex&c1 )
{
out<<"print"<<endl;
out<<c1.a<<".."<<c1.b<<endl;
return out;
}
Complex& operator++(Complex &c2)
{
c2.a++;
c2.b++;
return c2;
}
Complex operator+(Complex &c1,Complex &c2)
{
Complex temp;
temp.a=c1.a+c2.a;
temp.b=c1.b+c2.b;
return temp;
}
//后置++
Complex operator++(Complex &c2,int)
{
//先使用c2的属性,然后让属性++
/*return c2;
c2.a++;
c2.b++;*/
Complex tmp;
tmp=c2;
c2.a++;
c2.b++;
return tmp;
}
//1操作符重载,首先是通过函数实现的
void main1()
{
Complex c1(1,2),c2(3,4);
//c1=c1+c2;//c1 的类型是Complex ,这种类型,是自定义类型。编译器根本不知道如何加,但是c++ 编译器会提供一个机制,让你实现自定义类型加
//Complex c3=ComAdd(c1,c2);
//Complex c3=operator+(c1,c2);
//2 + 操作符 有两个参数 左操作数和右操作数
//3
Complex c3=c1+c2;
// 通过类的成员函数,完成操作符重载
//1,要承认操作符号重载是一个函数,要写函数原型
//2,写出函数调用语言
//c1.operator-(c2);
// c3.printCom();
Complex c4=c1-c2;
//c4.printCom();
//Complex& operator++(Complex &c2);
++c2;
c2.printCom();
/*--c1;
c1.printCom()*/;
c1++;
system("pause");
}
void main()
{
Complex c1(1,2),c2(3,4);
cout<<c1;
//函数返回值当左值的时候,需要返回一个对象的引用
cout<<c2<<"链式编程"<<endl;
//没有方法在cout 类里面添加函数 operator<<,智能通过全局函数实现
system("pause");
}
教师源码:
#ifndef _ARRAY_H_
#define _ARRAY_H_
class Array
{
private:
int mLength;
int* mSpace;
public:
Array(int length);
Array(const Array& obj);
int length();
void setData(int index, int value);
int getData(int index);
~Array();
public:
int& operator[] (int i);
Array& operator=(Array &a2);
bool operator==(Array &a1);
bool operator!=(Array &a1);
};
//[] = == !=
//
#endif
#include "iostream"
#include "Array.h"
using namespace std;
Array::Array(int length)
{
if( length < 0 )
{
length = 0;
}
mLength = length;
mSpace = new int[mLength];
}
Array::Array(const Array& obj)
{
mLength = obj.mLength;
mSpace = new int[mLength];
for(int i=0; i<mLength; i++)
{
mSpace[i] = obj.mSpace[i];
}
}
int Array::length()
{
return mLength;
}
void Array::setData(int index, int value)
{
mSpace[index] = value;
}
int Array::getData(int index)
{
return mSpace[index];
}
Array::~Array()
{
mLength = -1;
delete[] mSpace;
}
//操作符重载
//printf("array %d: %d\n", i, a1[i]) ;
//a1[i] = 1;
int& Array::operator[] (int i)
{
return mSpace[i];
}
//Array a3(100);
//a3 = a2; //执行=操作
// 1)如果a3已经分配内存需要释放
// 2)根据a2开辟内存空间 把a2的值copy到a3中
Array& Array::operator=(Array &a2)
{
if (this->mSpace != NULL)
{
delete[] mSpace;
mLength = 0;
}
this->mLength = a2.mLength;
this->mSpace = new int[this->mLength];
for (int i=0; i<this->mLength; i++)
{
mSpace[i] = a2[i];
}
return *this;
}
//a2.operator==(a1);
//if (a2==a1)
bool Array::operator==(Array &a1)
{
//a1和a2的长度是不是相等
if (this->mLength != a1.mLength)
{
return false;
}
//a1 a2的每一个数组元素是不是相等
for (int i=0; i<this->mLength; i++)
{
if (this->mSpace[i] != a1[i])
{
return false;
}
}
return true;
}
//if (a2 != a1)
bool Array::operator!=(Array &a1)
{
return !(*this == a1 );
}
#include "iostream"
#include "Array.h"
using namespace std;
int main()
{
Array a1(10);
for(int i=0; i<a1.length(); i++)
{
//a1.setData(i, i);
a1[i] = i+100;
//a1.operator[](i) = i;
//给数组的元素赋值 需要把数组元素本身给返回出来()
//0 = i;
}
for(int i=0; i<a1.length(); i++)
{
//printf("array %d: %d\n", i, a1.getData(i));
// printf("array %d: %d\n", i, a1[i]) ;
}
Array a2 = a1;
for(int i=0; i<a2.length(); i++)
{
//printf("array %d: %d\n", i, a2.getData(i));
//printf("array %d: %d\n", i, a2[i]);
}
//
Array a3(100);
a3 = a2; //执行=操作
// 1)如果a3已经分配内存需要释放
// 2)根据a2开辟内存空间 把a2的值copy到a3中。
printf("a3:len %d", a3.length());
for(int i=0; i<a3.length(); i++)
{
printf("array %d: %d\n", i, a3[i]);
}
a1 = a3 = a2; //执行=操作
// a1.operator=(a3.operator=(a2));
// a1.operator=(void)
//a2.operator==(a1);
if (a2==a1)
{
printf("相等");
}
else
{
printf("不相等");
}
if (a2 != a1)
{
printf("不相等");
}
else
{
printf("相等");
}
system("pause");
return 0;
}
项目开发操作符重载难点
数组操作符的应用场景有两个
1,放在等号的右边 int operator[](int i);
2, 放在等号的左遍 int & operator[](int i);
=操作符也有两个应用场景
1,a3=a2, 不需要operator= 这个函数的返回值
Array operator=(Array & a2);
2,a1=a3=a2 ;需要用operator= 这个函数的返回值
Array& operator=(Array & a2);