【C++学习笔记】常见运算符重载

本文为学习笔记 没有说明c++重载的语法细节。具体参考MOOC 程序设计与算法三 ,

赋值运算符重载:

#include
#include
using namespace std;
class String{
    private:
        char *str;
    public:
        String():str(new char[1]){
            str[0]=0;
        }
        String(const String & s){//重写赋值构造函数 
            str=new char[strlen(s.str)+1];
            strcpy(str,s.str);
        }
        const char * c_str(){
            return str;
        }
        String & operator =(const char *s);//解决s1=“hello world”问题 
        ~String(){
            delete [] str;
        }
        String & operator =(const String & s);//解决s1=s2问题 
};

String & String::operator = (const char * s){ 
    delete [] str;
    str=new char[strlen(s)+1];
    strcpy(str,s);
    return *this;
}
String & String:: operator =(const String & s){
    if(this==&s){    //防止出现s=s问题 
        return *this;
    }
    delete [] str;
    str=new char[strlen(s.str)+1];
    strcpy(str,s.str);
    return *this;
}
int main(void){
    String s;
    s="good luck";
    cout<"welcome to AHPU!";
    cout<return 0; 
} 

加号运算符重载:

#include
using namespace std;
class Complex{
    private:
        double real,imag;       
    public:
        Complex(){
        }
        Complex(double r,double i):real(r),imag(i){
        }
        Complex operator +(double x);
        Complex operator +(const Complex & c);
        friend Complex operator +(double r,const Complex & c);
        double getReal(){
            return this->real;
        }
        double getImag(){
            return this->imag;
        }
}; 
Complex Complex::operator +(const Complex & c){
    Complex t;
    t.real=this->real+c.real;
    t.imag=this->imag+c.imag;
    return t;
} 
Complex Complex::operator +(double x){
    return Complex(this->real+x,this->imag);
}
Complex operator +(double r,const Complex & c){
    return Complex(c.real+r,c.imag);
} 
int main(void){
    Complex s1(1,2);
    Complex s2(3,4);
    Complex s3;
    s3=s1+s2;
    Complex s4,s5;
    s4=s3+4;
    s5=4+s1;
    cout<","<cout<","<cout<","<return 0;
} 

左移运算符重载:

#include
using namespace std;
class Cstudent{
    public:
        int nAge;        
}; 
ostream &  operator <<(ostream & o,const Cstudent & s){
    o<return o;
} 
int main (void){
    Cstudent s;
    s.nAge=18;
    cout<"hello"<//18hello 
    return 0;
} 

左移和右移运算符重载:

#include
using namespace std;
class Complex{
    private:
        double real,imag;
    public:
        double getReal(){
            return this->real;
        }
        double getImag(){
            return this->imag; 
        }
        Complex(double r=0,double i=0):real(r),imag(i){
        }
        friend istream & operator >>(istream & is,Complex & c);
};
ostream & operator <<(ostream & os,Complex & c){
    os<"+"<"i";
    return os; 
}
istream & operator >>(istream & is,Complex  &c){
    double m,n;
    is>>m>>n;
    c.real=m;
    c.imag=n;
    return is;
}
int main(void){
    int n;
    Complex c(3,4);
    Complex d;
    cin>>d>>n;
    cout<cout<return 0;
} 

运算符++ –重载:

#include
using namespace std;
class cDemo{
    private:
        int num;
    public:
        cDemo(int x=0):num(x){
        }
        operator int(){
            return num;
        }
        cDemo operator ++(int a){
            cDemo tmp(*this);
            (this->num)++;
            return tmp;
        }
        cDemo & operator ++(){
            ++(this->num);
            return *this;
        }
        cDemo operator --(int a){
            cDemo tmp(*this);
            (this->num)--;
            return tmp;
        }
        cDemo & operator -- (){
            --(this->num);
            return *this;
        }        
};
int main(void){
    cDemo d(5);
    cout<",";
    cout<",";
    cout<<++d<<",";
    cout<","<cout<",";
    cout<",";
    cout<<--d<<",";
    cout<return 0; 
} 

【C++学习笔记】常见运算符重载_第1张图片
参考MOOC 程序设计与算法三

你可能感兴趣的:(【C++学习笔记】常见运算符重载)