一 运算符重载
1.1重载
1,赋予运算符的另一种功能
返回值类型 operator重载的符号(参数)
{
具体的实现
}
2,运算符重载
(1)运算符重载,把运算符重新定义行的规则
(2)几乎所有的运算符都能被重载 . :: ?: sizeof这些不能做重载的运算符
(3)运算符重载基本上出现再类中和结构中
(4)运算符重载要满足运算符原来的规则
(5)运算符理解为函数的一个表现
3,重载的规律
1.2常见的重载
1,类重载
int operator-(const student& stu){}//-重载
实例
#include <iostream>
using namespace std;
class student
{
int a;
public:
student()
{
a = 1;
}
//因为我们这个运算符重载是在类中,this指针要占一个参数
int operator-(const student& stu)
{
return this->a - stu.a;
}
//重载解引用* 功能就是输出一句 xxxxx
void operator*()
{
cout << "这是对对象解引用" << endl;
}
};
int main()
{
student stu1, stu2;
*stu1;
int c = stu1.operator-(stu2);
cout << c << endl;
system("pause");
return 0;
}
2,++和–重载
student& operator++(){}//重载前++
student operator++(int) {}//重载后++
实例
#include <iostream>
using namespace std;
class student
{
int a;
public:
student()
{
a = 1;
}
student& operator++()//重载前++
{
this->a++;
return *this;
}
student operator++(int)//重载后++
{
student stu = *this;
this->a++;
return stu;
}
void fun()
{
cout << a << endl;
}
};
int main()
{
student stu1;
stu1++;
stu1.fun();
stu1.fun();
system("pause");
return 0;
}
3,=重载
student& operator=(const student&stu){}
实例
#include <iostream>
using namespace std;
class student
{
char* name;
public:
student(const char*name)
{
this->name = new char[strlen(name) + 1];
strcpy(this->name, name);
}
~student()
{
if (name != NULL)
{
delete []name;
name = NULL;
}
}
void operator()(int a,int b,int c)//仿函数
{
cout << a << endl << b << endl << c << endl;
}
student& operator=(const student&stu)
{
if (this->name != NULL)
{
delete[]name;
name = NULL;
}
this->name = new char[strlen(stu.name) + 1];
strcpy(this->name, stu.name);
return *this;
}
};
void text()
{
student stu("测试");
stu(1,2,3);
student stu1("hello");
student stu2("today");
student stu3("你好");
stu3 = stu1 = stu2;//赋值不会调用拷贝构造
}
int main()
{
text();
system("pause");
return 0;
}
4,输入输出重载
friend ostream& operator<<(ostream& cout, student& stu);
friend istream& operator>>(istream& cin, student& stu);
实例
#include <iostream>
using namespace std;
class student
{
int a;
public:
student()
{
a = 123456789;
}
friend ostream& operator<<(ostream& cout, student& stu);
friend istream& operator>>(istream& cin, student& stu);
//输出输出不能再类中进行重载
};
//输入输出,对一个对象直接用cout输出,或者是cin输入
ostream& operator<<(ostream& cout, student& stu)
{
cout << stu.a;
return cout;
}
istream& operator>>(istream& cin, student& stu)
{
cin >> stu.a;
return cin;
}
int main()
{
student stu;
cin >> stu;
cout << stu << endl;
system("pause");
return 0;
}
5,智能指针
用来托管new出来的对象,让对象自动释放
#include <iostream>
using namespace std;
class student
{
int a;
public:
student(int a)
{
this->a = a;
}
~student()
{
cout << "指针析构了" << endl;
}
student(const student& stu)
{
cout << "拷贝构造" << endl;
this->a = stu.a;
}
void fun()
{
cout << a << endl;
}
};
class ZhiN
{
student* stu;//要托管的指针
public:
ZhiN(student* p)
{
stu = p;//接管new出来的对象
}
~ZhiN()
{
cout << "智能指针析构了" << endl;
if (stu != NULL)
{
delete stu;//释放对象
stu = NULL;
}
}
};
void text()
{
ZhiN sp(new student(2));
}
int main()
{
text();
system("pause");
return 0;
}
二 注意点
1、运输符重载需要考虑:考虑返回值,两个数相加,不可能返回地址也不能返回名义个操作数,只能返回一个新的变量,只能用于拷贝
。
2、operator<(需要重载的符号)>来表示函数名,参数列表,因为通过对象的成员函数来实现运算符重载,所以this算一个参
,只能是第一个参
3、给运算符重载,其实是做一些直接用运算符做不到的操作,对于运算符重载,可能会把这个运算符重载成其他的操作
(不输入这个运算符的操作,比如+,结果操作不是+),所以对于运算符重载要认真使用
4、对于运算符的重载的调用,可以直接用运算符,也可以显示调用(运算符重载是属于类的,不是友元
)
三 练习
实现一个字符串类,功能:基本的比较,存储,赋值等。
#include<iostream>
#include<string.h>
using namespace std;
class QString
{
char *name;
int len;
public:
QString();
QString(const char *stu)
{
this->name = new char[strlen(stu) + 1];
strcpy(this->name, stu);
}
~QString()
{
if (name != NULL)
{
delete[]name;
name = NULL;
}
}
QString& operator=(const QString &stu)
{
this->name = new char[strlen(stu.name) + 1];
strcpy(this->name, stu.name);
return *this;
}
bool operator<(const QString &stu)
{
if (strcmp(this->name, stu.name) < 0)
{
return 1;
}
return 0;
}
char operator[](int i)
{
int len = strlen(name) - 1;
if (i>len)
{
return '\0';
}
else
return name[i];
}
int operator+(const QString& stu)
{
return this->len + stu.len;
}
void print()
{
cout << name << endl;
}
friend ostream& operator<<(ostream& cout, QString& stu);
};
ostream& operator<<(ostream& cout, QString &stu)
{
cout << stu.name;
return cout;
}
int main()
{
QString str1 = "abcdefg";
char ch = str1[2];
cout << ch << endl;
QString str2 = "hijkqlnm";
int c = str1 < str2;
cout << c << endl;
QString m = str1 = str2;
m.print();
int st1 = 2;
int st2 = 3;
cout << st1 + st2 << endl;
system("pause");
return 0;
}