+ -运算符重载

#include<iostream>
using namespace std;
/*常函数可以访问非const数据成员,非const成员函数可以读取常数据成员,
常成员函数不可以调用非const成员函数*/
class A {
private:
int x;
int y;
public:
A(int x1 = 0, int y1 = 0) :x(x1), y(y1) {}
void show() const;
A operator+(const A &a) const;
friend A operator-(const A &a1, const A &a2);
};
void A::show() const
{
cout << x << ' ' << y << endl;
}
A A::operator+(const A &a) const
{
return A(x + a.x, y + a.y);
}
A operator-(const A &a1, const A & a2)
{
return A(a1.x - a2.x, a1.y - a2.y);
}
int main()
{
A a1(1, 3);
A a2(4, 5);
A a;
a1.show();
a2.show();
a = a1 + a2;
a.show();
a = a1 - a2;
a.show();
system("pause");
return 0;
}

前置++ ++(int)重载

```
#include<iostream>
using namespace std;
class A {
private:
int x;
int y;
public:
A(int x1 = 0, int y1 = 0) :x(x1), y(y1) {}
void show() const;
A operator++();
A operator++(int);//后置
};
void A::show() const
{
cout << x << ' ' << y << endl;
}
A A::operator++()
{
++x;
++y;
return *this;
}
A A::operator++(int)
{
A a = *this;
++(*this);
return a;
}
int main()
{
A a1(1, 2);
A a2(3, 4);
(a1++).show();
(++a2).show();
system("pause");
return 0;
}
//    输入输出重载
#include<iostream>
using namespace std;
class A {
private:
int x;
int y;
public:
A(int x1 = 0, int y1 = 0) :x(x1), y(y1) {}
friend ostream& operator<<(ostream &os, const A &a);
friend istream& operator >> (istream &is, A &a);
};
ostream& operator<<(ostream &os, const A &a)
{
os << a.x << ' ' << a.y << endl;
return os;
}
istream& operator >> (istream &is, A &a)
{
is >> a.x >> a.y;
return is;
}
int main()
{
A a1(1, 2);
A a2(3, 4);
cout << a1;
cout << a2;
cin >> a2;
cout << a2;
system("pause");
return 0;
}
#include<iostream>
using namespace std;
class Student {
private:
int id;
double score;
public:
friend bool operator==(const Student &st1, const Student &st2);
friend ostream& operator<<(ostream &os, const Student &st)
{
os << st.id << ' ' << st.score << endl;
return os;
}
friend istream& operator >> (istream &is, Student &st)
{
is >> st.id >> st.score;
return is;
}
};
bool operator==(const Student &st1, const Student &st2)
{
return (st1.score == st2.score);
}
int main()
{
Student st1, st2;
cin >> st1 >> st2;
if (st1 == st2)
cout << st1;
system("pause");
return 0;
}

赋值运算符重载

#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<cstring>
using namespace std;
class Internet {
public:
char *name;
char *url;
public:
Internet(char *name, char *url);
Internet(Internet &temp);
~Internet()
{
delete[] name;
delete[] url;
}
Internet& operator=(Internet &temp);
};
Internet::Internet(char *name, char *url)
{
this->name = new char[strlen(name) + 1];
this->url = new char[strlen(url) + 1];
if (name)
strcpy(this->name, name);
if (url)
strcpy(this->url, url);
}
Internet::Internet(Internet &temp)
{
this->name = new char[strlen(temp.name) + 1];
this->url = new char[strlen(temp.url) + 1];
if (name)
strcpy(this->name, temp.name);
if (url)
strcpy(this->url,temp.url);
}
Internet& Internet::operator=(Internet &temp)
{
delete[] name;
delete[] url;
this->name = new char[strlen(temp.name) + 1];
this->url = new char[strlen(temp.url) + 1];
if (name)
strcpy(this->name, temp.name);
if (url)
strcpy(this->url, temp.url);
return *this;
}
int main()
{
Internet a("lixain", "hahah");
cout << a.name << ' ' << a.url << endl;
Internet b(a);
cout << b.name << ' ' << b.url << endl;
Internet c("uigj", "ouihknp");
cout << c.name << ' ' << c.url << endl;
b = c;
cout << b.name << ' ' << b.url << endl;
system("pause");
return 0;
}

下标运算符重载

//可以像普通数组一样哈
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;
class Array {
private:
int size;
char *buf;
public:
Array(int n);
Array(char *src);
Array()
{
delete[] buf;
}
char& operator[](int n);
void show()
{
for (int i = 0; i < size; i++)
{
cout << buf[i];
}
cout << endl;
}
};
Array::Array(int n)
{
size = n;
buf = new char[n + 1];
*(buf + size) = '\0';
}
Array::Array(char *src)
{
buf = new char[strlen(src) + 1];
strcpy(buf, src);
size = strlen(buf);
}
char& Array::operator[](int n)
{
static char ch = 0;//
if (n > size || n < 0)
{
cout << "yuejie" << endl;
return ch;
}
else
return *(buf + n);
}
int main()
{
/*在构造arr1对象时,指定了数组的大小,然后通过【】运算符来给数组赋值*/
Array arr1(20);
for (int i = 0; i < 20; i++)
{
arr1[i] = 65 + i;
}
arr1.show();
/*由一个字符串直接创建一个数组,然后调用【】运算符读取某一个字符元素,也可以进行修改*/
Array arr2("hahahahah");
cout << arr2[6] << endl;
arr2[6] = 'l';
arr2.show();
system("pause");
return 0;
}
``
//类型转换函数
#include<iostream>
using namespace std;
class Swap {
private:
int a, b;
public:
Swap(int m, int n) :a(m), b(n) {}
operator char()
{
return static_cast<char>(a);
}
void show()
{
cout << a << ' ' << b << endl;;
}
};
int main()
{
Swap s1(65, 2);
s1.show();
char ch = s1;
cout << ch;
system("pause");
return 0;
}