鸿 网 互 联 www.68idc.cn

当前位置 : 服务器租用 > 编程语言开发 > c++ > >

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

来源:互联网 作者:佚名 时间:2018-02-27 09:49
本文为学习笔记 没有说明c++重载的语法细节。具体参考MOOC 程序设计与算法三 , 赋值运算符重载: #includeiostream #includecstring using namespace std ; class String{ private : char *str; public : String():str( new char [ 1 ]){ str[ 0 ]= 0 ; } S

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

赋值运算符重载:

#include<iostream>
#include<cstring>
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<<s.c_str()<<endl;
    s="welcome to AHPU!";
    cout<<s.c_str()<<endl; 
    return 0; 
} 

加号运算符重载:

#include<iostream>
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<<s3.getReal()<<","<<s3.getImag()<<endl;
    cout<<s4.getReal()<<","<<s4.getImag()<<endl;
    cout<<s5.getReal()<<","<<s5.getImag()<<endl;
    return 0;
} 

左移运算符重载:

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

左移和右移运算符重载:

#include<iostream>
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<<c.getReal()<<"+"<<c.getImag()<<"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<<c<<endl;
    cout<<d<<endl;
    return 0;
} 

运算符++ –重载:

#include<iostream>
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<<d++<<",";
    cout<<d<<",";
    cout<<++d<<",";
    cout<<d<<","<<endl;
    cout<<d--<<",";
    cout<<d<<",";
    cout<<--d<<",";
    cout<<d<<endl; 
    return 0; 
} 

这里写图片描述
参考MOOC 程序设计与算法三

 

网友评论
<