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