目录
一、运算符重载的概念
1、实现“+”号运算符的重载
2、运算符重载的规则
(1)不是所有的运算符都能重载
不能重载的运算符: :: ?: sizeof . .*
(2)重载不能改变运算符的优先级
(3)重载不能改变运算符的操作数
(4)运算符重载不允许有默认值
(5)运算符重载函数既可以是全局函数,也可以是类成员函数
3、运算符重载的步骤
(1)写出函数名 operator...
(2)根据操作数写出函数的形参
(3)根据使用场景确定函数的返回值
4、重载输出运算符(<<)
5、重载单目运算符
6、逻辑运算符重载
7、数组重载、==号重载、=号重载
8、智能指针
作业:自己实现字符串类
一、运算符重载的概念
所谓运算符重载,就是给运算符赋予新的功能,同一个运算符具有不同的功能
运算符重载的本质:函数的重载
1、实现“+”号运算符的重载
重载功能:使实列对象可以直接相互加减,从而使类中的成员变量相互加减
#includeusing namespace std; class Complex { //friend Complex operator+(const Complex &c1,const Complex &c2); //友元函数 private: int m_a; int m_b; public: void print() { cout m_b = b; } Complex operator+(const Complex &c) { Complex t(0,0); t.m_a = this->m_a + c.m_a; t.m_b = this->m_b + c.m_b; return t; } }; int main(int argc, char *argv[]) { Complex c1(1,2); Complex c2(2,3); Complex t(0,0); t = c1 + c2; // t = operator+(c1,c2); t.print(); return 0; }
2、运算符重载的规则
(1)不是所有的运算符都能重载
不能重载的运算符: :: ?: sizeof . .*
(2)重载不能改变运算符的优先级
(3)重载不能改变运算符的操作数
(4)运算符重载不允许有默认值
(5)运算符重载函数既可以是全局函数,也可以是类成员函数
3、运算符重载的步骤
(1)写出函数名 operator...
(2)根据操作数写出函数的形参
(3)根据使用场景确定函数的返回值
4、重载输出运算符(<<)
不能重载的运算符: :: ?: sizeof . .*
(2)重载不能改变运算符的优先级
(3)重载不能改变运算符的操作数
(4)运算符重载不允许有默认值
(5)运算符重载函数既可以是全局函数,也可以是类成员函数
3、运算符重载的步骤
(1)写出函数名 operator...
(2)根据操作数写出函数的形参
(3)根据使用场景确定函数的返回值
4、重载输出运算符(<<)
(3)重载不能改变运算符的操作数
(4)运算符重载不允许有默认值
(5)运算符重载函数既可以是全局函数,也可以是类成员函数
3、运算符重载的步骤
(1)写出函数名 operator...
(2)根据操作数写出函数的形参
(3)根据使用场景确定函数的返回值
4、重载输出运算符(<<)
(5)运算符重载函数既可以是全局函数,也可以是类成员函数
3、运算符重载的步骤
(1)写出函数名 operator...
(2)根据操作数写出函数的形参
(3)根据使用场景确定函数的返回值
4、重载输出运算符(<<)
(1)写出函数名 operator...
(2)根据操作数写出函数的形参
(3)根据使用场景确定函数的返回值
4、重载输出运算符(<<)
(3)根据使用场景确定函数的返回值
4、重载输出运算符(<<)
功能:可以直接通过输出类名,来访问类的成员变量(输出成员变量)
#includeusing namespace std; class Complex { friend ostream& operator<<(ostream &out, const Complex &c); //友元函数 private: int m_a; int m_b; public: Complex(int a, int b) { this->m_a = a; this->m_b = b; } }; ostream& operator<<(ostream &out, const Complex &c) //外部重载 { out< 5、重载单目运算符
功能:将实例的对象经过单目运算符的计算,从而完成对象内部的成员变量的计算
#includeusing namespace std; class Complex { friend ostream& operator<<(ostream &out, const Complex &c); private: int m_a; int m_b; public: Complex(int a, int b) { this->m_a = a; this->m_b = b; } Complex operator++() //前置++ { this->m_a++; this->m_b++; return *this; } Complex operator++(int) //后置++ { Complex t = *this; this->m_a++; this->m_b++; return t; } Complex operator--() //前置-- { this->m_a--; this->m_b--; return *this; } Complex operator--(int) //后置-- { Complex t = *this; this->m_a--; this->m_b--; return t; } }; ostream& operator<<(ostream &out, const Complex &c) { out< 6、逻辑运算符重载
功能:可以直接将两个对象进行逻辑&&运算比较,从而判断真或假
#includeusing namespace std; class Complex { private: int a; //实部 int b; //虚部 public: Complex(int a,int b) { this->a = a; this->b = b; } bool operator&&(const Complex &c) { return (this->a && c.a)&&(this->b && c.b); } }; int main(int argc, char const *argv[]) { Complex c1(1,1); Complex c3(0,0); if(c1 && c3) //c1.operator&&(c3); //一般不重载逻辑&&运算符,不遵循短路原则 { cout<<"成立"< 7、数组重载、==号重载、=号重载
功能:对象通过[ ] 访问数组元素;可以让对象直接比较(==);对象间赋值(=)
#includeusing namespace std; class Array { private: int *data; int size; public: Array(int a) { size = a; data = new int[size]; } int& operator[](int i) { return data[i]; } bool operator==(const Array &a) { return (data == a.data)&&(size == a.size); } Array operator=(const Array &a) { if(*this == a) { return *this; } size = a.size; delete []data; data = new int[size]; for(int i = 0; i < size; i++) { data[i] = a.data[i]; } return *this; } }; int main(int argc, char const *argv[]) { Array a1(10); //构造函数 for (int i = 0; i < 10; i++) { a1[i] = i + 1; //[]重载 } for (int i = 0; i < 10; i++) { cout< 8、智能指针
为了解决跨函数释放空间的问题。
#include#include #include using namespace std; class Test { public: Test() { cout<<"Test的构造函数"< my_memory(new Test); my_memory->print(); //my_memory.operator->(print()) } int main(int argc, char const *argv[]) { //f1(); f2(); return 0; } 作业:自己实现字符串类
补全下面的代码(多重载)
#includeusing namespace std; class MyString { private: char *pstring; int size; }; int main(int argc, char const *argv[]) { MyString s1("helloworld"); //构造函数 MyString s2(s1); //构造函数 cout< 补全后的代码如下:
#include#include using namespace std; class MyString { private: char *pstring; int size; public: void print(); MyString(); MyString(const char * str); MyString(const MyString &s); char& operator[](int i); //重载[] void operator=(const char *str); //重载= MyString& operator=(const MyString &obj); //重载= MyString& operator+(const char * s); //重载+ MyString& operator+(const MyString &obj); //重载+ bool operator==(const MyString &obj); //重载== ~MyString(); }; void MyString::print() { cout< pstring< pstring = NULL; this->size = 0; } MyString::MyString(const char *str) { this->size = strlen(str) + 1; this->pstring = new char[size]; strcpy(pstring, str); } MyString::MyString(const MyString &s) { this->size = s.size; this->pstring = new char[this->size]; strcpy(this->pstring, s.pstring); } char& MyString::operator[](int i) { return pstring[i]; } void MyString::operator=(const char *str) { this->size = strlen(str) + 1; if (this->pstring == NULL) { this->pstring = new char[this->size]; } else { delete []this->pstring; this->pstring = new char[this->size]; } strcpy(this->pstring, str); } MyString& MyString::operator=(const MyString &obj) { if (*this == obj) { return *this; } this->size = strlen(obj.pstring) + 1; if (this->pstring == NULL) { this->pstring = new char[this->size]; } else { delete []this->pstring; this->pstring = new char[this->size]; } strcpy(this->pstring, obj.pstring); return *this; } MyString& MyString::operator+(const char * s) { if (this->pstring == NULL) { this->size = strlen(s) + 1; this->pstring = new char[strlen(s) + 1]; } else { this->size = this->size + strlen(s); char *p = new char[this->size]; strcpy(p, this->pstring); delete []this->pstring; this->pstring = new char[(this->size + strlen(s))]; strcpy(this->pstring, p); delete []p; p = NULL; } strcat(this->pstring, s); //cout< pstring< pstring == NULL) { this->size = obj.size; this->pstring = new char[obj.size]; } else { this->size = this->size + obj.size - 1; char *p = new char[this->size]; strcpy(p, this->pstring); delete []this->pstring; this->pstring = new char[(this->size + obj.size - 1)]; strcpy(this->pstring, p); delete []p; p = NULL; } strcat(this->pstring, obj.pstring); return *this; } bool MyString::operator==(const MyString &obj) { return (this->size == obj.size)&&(this->pstring == obj.pstring); } MyString::~MyString() { cout<<"系够函数"< pstring != NULL) { delete []this->pstring; this->pstring = NULL; } } int main(int argc, char const *argv[]) { MyString s1("helloworld"); //构造函数 MyString s2(s1); //构造函数 cout<



