运算符重载

来源:互联网 发布:怎么知道我的网络 编辑:程序博客网 时间:2024/06/05 06:51

运算符重载:自己的理解就是定义一个成员函数或者友元函数对运算符进行新的功能,使其可以完成更加复杂的数据类型的操作。

下面介绍几种运算符的实现:

一.+运算符的实现

class stu{private:int a;int b;public:friend stu operator+(const stu &c1, const stu &c2);//声明友元函数stu(int x = 0,int y = 0)//定义构造函数{this->a = x;this->b = y;}stu operator+(const stu &c2)//定义成员函数的运算符重载函数{stu c3(this->a + c2.a, this->b + c2.b);return c3;}void print()//打印输出函数{cout << this->a << " " << this->b << endl;}};stu operator+(const stu &c1, const stu &c2)//因为要访问私有成员,所以将其声明为友元函数{stu c3(c1.a + c2.a, c1.b + c2.b);//初始化对象3return c3;//创建匿名对象返回去,用c3去初始化}int main(){stu c1(1, 2), c2(3, 4);stu c3 = c1 + c2;//调用运算符重载函数c3.print();system("pause");return 0;}

二.前置++

class stu{private:int a;int b;public:friend stu &operator++(stu &c1);//声明友元函数stu(int x = 0,int y = 0)//定义构造函数{this->a = x;this->b = y;}//stu &operator++()//定义成员函数的运算符重载函数,里面隐含this指针不需要参数//{//this->a++;//this->b++;//return *this;//}void print()//打印输出函数{cout << this->a << " " << this->b << endl;}};stu &operator++(stu &c1)//因为要访问私有成员,所以将其声明为友元函数{c1.a++;c1.b++;return c1;}int main(){stu c1(1, 2);++c1;c1.print();system("pause");return 0;}

三.后置++

class stu{private:int a;int b;public:friend stu operator++(stu &c1,int);//声明友元函数,int为占位符为了与前置++区分stu(int x = 0,int y = 0)//定义构造函数{this->a = x;this->b = y;}//stu operator++(int)//定义成员函数的运算符重载函数,里面隐含this指针不需要参数//{//stu tmp = *this;//this->a++;//this->b++;//return tmp;//}void print()//打印输出函数{cout << this->a << " " << this->b << endl;}};stu operator++(stu &c1,int)//因为要访问私有成员,所以将其声明为友元函数{stu tmp = c1;c1.a++;c1.b++;return tmp;}int main(){stu c1(1, 2);c1++;c1.print();system("pause");return 0;}

四.<<操作符

class stu{private:int a;int b;public:friend ostream &operator<<(ostream &out, const stu &c1);//声明友元函数stu(int x = 0,int y = 0)//定义构造函数{this->a = x;this->b = y;}void print()//打印输出函数{cout << this->a << " " << this->b << endl;}};ostream &operator<<(ostream &out,const stu &c1)//因为要访问私有成员,所以将其声明为友元函数,                                              //注意:<<操作符重载只能用友元函数实现,不能用成员函数实现{out << c1.a << " " << c1.b;return out;}int main(){stu c1(1, 2);cout << c1 <<"   "<< "abcd";system("pause");return 0;}
五>>操作符

class stu{private:int a;int b;public:friend istream &operator>>(istream &input,  stu &c1);//声明友元函数stu(int x = 0,int y = 0)//定义构造函数{this->a = x;this->b = y;}void print()//打印输出函数{cout << this->a << " " << this->b << endl;}};istream &operator>>(istream &input, stu &c1)//因为要访问私有成员,所以将其声明为友元函数,                                           {input >> c1.a>>c1.b;return input;}int main(){stu c1;cin >> c1;c1.print();system("pause");return 0;}

六=操作符

class stu{private:char *pt;int len;public:stu(const char *src)//定义构造函数{this->len = strlen(src);pt= new char [len+1];strcpy(pt, src);}stu(const stu &m)//定义深拷贝函数,用于对象初始化对象{this->len = m.len;this->pt = new char[len + 1];strcpy(this->pt, m.pt);}stu& operator=(const stu &n)//定义成员函数重载=操作符,用来对象=对象{if (this->pt != NULL)//先将已经初始化好了对象清空,注意与深拷贝构造函数区分{delete[] pt;this->len = 0;}this->len = n.len;this->pt = new char[len + 1];strcpy(this->pt, n.pt);return *this;}~stu(){if (pt != NULL){delete[] pt;pt = NULL;len = 0;}}};int main(){stu c1("hello,world");stu c2 = c1;//注意这里是用对象初始话对象,调用的是深拷贝函数stu c3("hehe");c3 = c2;//用c2给c3赋值,在这里调用运算符重载函数system("pasue");return 0;}

对于每个函数的调用自己可以进行测试。

七.以数组为基础数据进行[],==,!=的运算符重载函数

class stu{private:int len;int *pt;public:stu(int n){this->len = n;this->pt = new int[len];for (int i = 0; i < len; i++){   pt[i] = i;}}~stu(){if (pt != NULL){delete[]pt;len = 0;}}int& operator[](int n)//[]运算符重载函数{return pt[n];}bool operator==(stu &n)//==运算符重载{if (this->len != n.len){return false;}for (int i = 0; i < len; i++){if (this->pt[i] != n[i])//因为上面有[]运算符重载所以可以写成这个样子,其实是n.pt[i]{return false;}}return true;}bool operator!=(stu &n)//!=运算符重载函数{if (this->len != n.len){return true;}for (int i = 0; i < len; i++){if (this->pt[i] != n[i]){return true;}}return false;}void print(){for (int i = 0; i < len; i++)cout << pt[i]<<" ";cout << endl;}};int main(){//1.测试[]运算符重载内容//stu c1(5);//c1.print();//stu c2(3);//c2.print();//for (int i = 0; i < 10; i++)//c1[i] = i;//调用[]重载函数//for (int i = 0; i < 10; i++)//{//cout << c1[i] << " ";//}//cout << endl;//2.测试==和!=运算符重载内容stu c1(5);stu c2(6);//if (c1 == c2)//调用==运算符重载函数//cout << "ture";//else//cout << "false";if (c1 != c2)//调用!=运算符的重载cout << "ture";elsecout << "false";getchar();return 0;}


最后用自己定义的Mystring类综合实现运算符的操作,如下

Mytring.h

#include<iostream>#include<string.h>using namespace std;class String{private:int len;char *pt;public:void print();String();String(const char *p);String(const String &m);//~String();String& operator=(const String&n);//=操作符,参数为对象String& operator=(const char *p);//=操作符,参数为字符串char& operator[](const int i);//[]friend istream& operator>>(istream &in, String &n);friend ostream& operator<<(ostream &out,const String &n);//<<bool operator==(const char *p);//== 参数为字符串bool operator==(const String &n);//== 参数为对象bool operator!=(const char *p);//!= 参数为字符串bool operator!=(const String &n);//!= 参数为对象int operator<(const String &n);//> 参数为对象int operator<(const char *p);//>  参数为字符串int operator>(const String &n);//<  参数为对象int operator>(const char *p);//<  参数为字符串};
Mystring.cpp

#include"Mystring.h"String::String()//无参构造函数{this->len = 0;this->pt = new char[len + 1];strcpy(this->pt, "");}String::String(const char *p)//有参数构造函数{this->len = strlen(p);this->pt = new char[len + 1];strcpy(this->pt, p);} String::String(const String &m)//拷贝构造函数{ this->len = m.len; this->pt = new char[len + 1]; strcpy(this->pt, m.pt);}String::~String()//析构函数{if (this->pt != NULL){this->len = 0;delete[] pt;this->pt = NULL;}}void String::print()//打印函数{cout << this->len << endl;cout << this->pt << endl;}String& String::operator=(const String&n)//传参为对象,等号重载操作符必须把原来的清空{if (this->pt != NULL){delete[] this->pt;this->len = 0;this->pt = NULL;}this->len = n.len;this->pt = new char[len + 1];strcpy(this->pt, n.pt);return *this;}String& String::operator = (const char *p)//传参为字符串{if (this->pt != NULL){delete[] this->pt;this->len = 0;this->pt = NULL;}this->len = strlen(p);this->pt = new char[len + 1];strcpy(this->pt, p);return *this;}char& String::operator[](const int i)//[]重载{return this->pt[i];}ostream& operator<<(ostream &out, const String &n){out << n.pt << endl;return out;}istream& operator>>(istream &in, String &n){in >> n.pt;return in;}bool String::operator==(const char *p){if (p == NULL){return false;}if (strcmp(this->pt, p) == 0){return true;}else{return false;}}bool String::operator==(const String &n){if (n.pt == NULL){return false;}if (strcmp(this->pt, n.pt) == 0){return true;}else{return false;}}bool String::operator!=(const char *p){if (p == NULL){return true;}if (strcmp(this->pt, p) != 0){return true;}else{return false;}}bool String::operator!=(const String &n){if (n.pt == NULL){return true;}if (strcmp(this->pt, n.pt) != 0){return true;}else{return false;}}int String::operator<(const String &n)//> 参数为对象{return strcmp(n.pt, this->pt);//如果大于返回1,为真}int String::operator<(const char *p)//>  参数为字符串{return strcmp(p,this->pt);}int String::operator>(const String &n)//<  参数为对象{return strcmp(this->pt,n.pt);}int String::operator>(const char *p)//<  参数为字符串{return strcmp(this->pt,p);}
test.cpp

#include"Mystring.h"int main(){String c1("aaa");String c2 = "bbb";int ret = (c1 >c2);cout << ret;if (ret>0){cout << "true";}elsecout << "false";system("pause");return 0;}
其中test.c是测试部分,自己可根据实际情况进行测试

原创粉丝点击