C++学习:运算符重载
简介:
类是用户自定义的数据类型,使用重载运算符好可以实现一下的逻辑
1:对象3 = 对象1+对象2+...如描述复数的类,描述字符串的类等等
2:提高程序的可读性.
重载赋值运算符
如果一个类没有赋值运算符,系统会默认提供一个
如果一个类已经提供了一个拷贝构造函数(非默认),那么也需要提供一个重载的赋值运算符
如:
Student & Student ::operate=(const Student &stu){
if(this == &stu){
return *this;
}
delete[] m_data;
int length = strlen(other.m_data);
m_data = new char[length+1];
strcpy(m_data,other.m_data);
return *this;
}
提示:
本文博主:章飞_906285288
博客地址:http://blog.csdn.net/qq_29924041
运算符重载
运算符重载特点及注意事项:
C++语言规定:
1:重载的运算符要保持原来运算符的意义
2:只能对已有的运算符,而不能对新的运算符
3:重载运算符不会改变原先的优先级和结合性
运算符重载的形式:
1:成员函数形式
2:友元函数的形式
运算符重载的限制:
通过一张图的形式展示下:

从上面的图中可以看到,以上的一些运算符号都是可以进行重载的.
而不能重载的运算符有以下一些:
以上两张图都是取自百度图片
简单归纳一下一些常用的可重载运算符:
+ - *
= > <
<= >= <<
>> [] ()
-> new new[]
delete delete[]
实例代码部分:
友元函数重载
/*
* ===========================================================================
*
* Filename: studentTest.cpp
* Description:
* Version: 1.0
* Created: 2017年06月13日 23时01分07秒
* Revision: none
* Compiler: gcc
* Author: (),
* Company:
*
* ===========================================================================
*/
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
using namespace::std;
class Student{
public:
Student(const char* name = "NULL",int stu_math_score = ):math_score(stu_math_score){
m_name = (char *)malloc(strlen(name)*sizeof(char));
strncpy(m_name,name,strlen(name)*sizeof(char));
cout<<"Student constructor"<<endl;
}
~Student(){
free(m_name);
cout<<"Student desctructor"<<endl;
}
Student(const Student &stu){
//深拷贝
m_name = (char *)malloc(strlen(stu.m_name)*sizeof(char));
strncpy(m_name,stu.m_name,strlen(stu.m_name)*sizeof(char));
math_score = stu.math_score;
}
void print_stu_info(){
cout<<"================="<<endl;
cout<<"student name:"<<m_name<<endl;
cout<<"student math_score:"<<math_score<<endl;
cout<<"================="<<endl;
}
friend int operator+(Student &,Student&);//输出两个学生的成绩和
friend int operator-(Student &,Student&);//输出两个学生的成绩差
friend bool operator>(Student &,Student &);//判断第一个学生的数学成绩是否大于第二个学生的数学成绩
friend Student operator<(Student &,Student &);//将数学成绩少的学生成绩输出
private:
char *m_name;
int math_score;
};
int operator+(Student &stu1,Student &stu2){
int ret = stu1.math_score + stu2.math_score;
return ret;
}
int operator-(Student &stu1,Student &stu2){
int ret = stu1.math_score - stu2.math_score;
return ret;
}
bool operator>(Student &stu1,Student &stu2){
return stu1.math_score > stu2.math_score;
}
Student operator<(Student &stu1,Student &stu2){
Student stu = stu1.math_score < stu2.math_score ? stu1 :stu2;
return stu;
}
int main(int agrc,char *argv[]){
Student stu_zhangsan("zhangsan",);
stu_zhangsan.print_stu_info();
Student stu_lisi("lisi",);
stu_lisi.print_stu_info();
int result = stu_zhangsan +stu_lisi;
cout<<"add:"<<result <<endl;
result = stu_zhangsan - stu_lisi;
cout<<"sub:"<<result <<endl;
Student stu = stu_zhangsan < stu_lisi;
stu.print_stu_info();
bool is_greater_than = stu_zhangsan > stu_lisi;
cout<<is_greater_than<<endl;
return ;
}
输出的结果为:
Student constructor
=================
student name:zhangsan
student math_score:85
=================
Student constructor
=================
student name:lisi
student math_score:83
=================
add:168
sub:2
=================
student name:lisi
student math_score:83
=================
1
Student desctructor
Student desctructor
Student desctructor
从上面的测试代码中可以很直观的看到,stu_zhansan 和stu_lisi两个对象,他们之间可以去直接进行运算符之间的加减.很直观.也是使用友元函数来进行的操作的(友元函数是不依赖于类的,注意友元函数在定义的时候的写法)
成员运算符重载
二元成员运算符重载的时候,参数只要传一个就行了,另外一个由this指针进行传入
/*
* ===========================================================================
*
* Filename: studentTest.cpp
* Description:
* Version: 1.0
* Created: 2017年06月13日 23时01分07秒
* Revision: none
* Compiler: gcc
* Author: (),
* Company:
*
* ===========================================================================
*/
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
using namespace::std;
class Student{
public:
int english_score;
int math_score;
Student(const char* name = "NULL",int stu_math_score = ,int english_score = ):math_score(stu_math_score),english_score(english_score){
m_name = (char *)malloc(strlen(name)*sizeof(char));
strncpy(m_name,name,strlen(name)*sizeof(char));
cout<<"Student constructor"<<endl;
}
~Student(){
free(m_name);
cout<<"Student desctructor"<<endl;
}
Student(const Student &stu){
//深拷贝
m_name = (char *)malloc(strlen(stu.m_name)*sizeof(char));
strncpy(m_name,stu.m_name,strlen(stu.m_name)*sizeof(char));
math_score = stu.math_score;
english_score = stu.english_score;
}
void print_stu_info(){
cout<<"================="<<endl;
cout<<"student name:"<<m_name<<endl;
cout<<"student math_score:"<<math_score<<endl;
cout<<"student english_score:"<<english_score<<endl;
cout<<"================="<<endl;
}
//二元运算符重载的时候只需要传一个参数,另一个参数由this指针进行传入
int operator+(int english);//输出学生的成绩和
int operator-(int english);//输出学生的成绩差
bool operator>(int english);//判断学生的数学成绩是否大于学生的英语成绩
friend Student operator<(Student &,Student &);//将数学成绩少的学生成绩输出
int operator()(int english);//对象可以使用(),来进行运算符重载,利用重载()来构建可变参数列表
int operator()(int a,int b,int c,int d);
private:
char *m_name;
};
int Student::operator+(int english){
int ret = this->math_score + english;
return ret;
}
int Student::operator-(int english){
int ret = this->math_score - english;
return ret;
}
bool Student::operator>(int english){
return this->math_score > english;
}
Student operator<(Student &stu1,Student &stu2){
Student stu = stu1.math_score < stu2.math_score ? stu1 :stu2;
return stu;
}
int Student::operator()(int b) {
return this->math_score+b;
}
int Student::operator()(int a,int b,int c,int d){
return a+b+c+d;
}
int main(int agrc,char *argv[]){
Student stu_zhangsan("zhangsan",,);
stu_zhangsan.print_stu_info();
int ret = stu_zhangsan.math_score - stu_zhangsan.english_score;
cout<<"sub:"<<ret<<endl;
ret = stu_zhangsan.math_score +stu_zhangsan.english_score;
cout<<"add:"<<ret<<endl;
bool result= stu_zhangsan.math_score > stu_zhangsan.english_score;
cout<<"bool:"<<result<<endl;
//直接通过()进行运算
ret = stu_zhangsan();
cout<<"ret:"<<ret<<endl;
ret = stu_zhangsan(,,,);
cout<<"ret2:"<<ret<<endl;
return ;
}
输出的结果为:
Student constructor
=================
student name:zhangsan
student math_score:85
student english_score:90
=================
sub:-5
add:175
bool:0
ret:175
ret2:100
Student desctructor