C++运算符重载+、-、=

一个例子:

class Complex {
    public:
        Complex(double r = 0.0, double i = 0.0) {
            real = r;
            imaginary = i;
        }
        double real;
        double imaginary;
};

Complex operator+(const Complex &a, const Complex &b) {  //real和imaginary是公有,相对来说不安全
    return Complex(a.real + b.real, a.imaginary + b.imaginary);
}


int main() {
    Complex a(1, 2), b(2, 3), c;
    c = a + b; // operater+(a, b)
    cout << c.real << "+" << c.imaginary << "i";
    return 0;
}

运行结果如下:

3+5i

缺点:形参数目多;类的real和imaginary是公有,相对来说不安全。

改进后的代码:

class Complex {
    double real;
    double imaginary;
public:
    Complex(double r = 0.0, double i = 0.0) {
        real = r;
        imaginary = i;
    }
    Complex operator+(const Complex &);
    Complex operator-(const Complex &);
    void get_complex() {
        cout << real << "+(" << imaginary << "i)" << endl;
    }
};

Complex Complex::operator+(const Complex &operand2) {
    return Complex(real + operand2.real, imaginary + operand2.imaginary);
}

Complex Complex::operator-(const Complex &operand2) {
    return Complex(real - operand2.real, imaginary - operand2.imaginary);
}

int main() {
    Complex x, y(2.3, 5.4), z(66.3, 888.2);
    x = y + z;  // x = y.operator+(z);
    x.get_complex();
    x = y - z;
    x.get_complex();
    return 0;
}

运行结果如下:

68.6+(893.6i)
-64+(-882.8i)

输出结果需要改进一下,下次再说。

接下来是运算符=的重载,举个例子:

#include <iostream>
using namespace std;

class String{
    char *str; //allocate dynamic RAM? space;
public:
    String():str(NULL) {}
    String(String &s);  //拷贝构造函数
    const char *c_str() {   return str;}  //不能通过c_str修改它指向的函数的内容
    char* operator=(const char *s);
    String& operator=(const String &s);
    ~String();
};

String::String(String &s) {  //深拷贝,赋值构造函数
    if(s.str) {
        str = new char[strlen(s.str) + 1];
        strcpy(str, s.str);
    } else {
        str = NULL;
    }
}

char* String::operator=(const char *s) {  //重载时不修改s的值,浅拷贝,拷贝后地址相同
    if(str) delete []str;
    if(s) {  
        str = new char[strlen(s) + 1];  //分配新的内存空间
        strcpy(str, s);
    } else {
        str = NULL;
    }
    return str;
} 

String& String::operator=(const String &s){  //深拷贝,拷贝后地址不同,对于两个不同对象的赋值用深拷贝
    if(str == s.str)  return *this;
    if(str) {
        delete []str;
    }
    str = new char[strlen(s.str) + 1];
    strcpy(str, s.str);
    return *this;
}

String::~String() {
    if(str)  delete []str;
}

int main() {
    String s, s1, s2;
    s = "Good Luck!";
    cout << s.c_str() << endl;
    // String s2 = "Hello wprld"; 会出错,初始化语句不调用=重载函数
    s = "HAHAHAHAHA!";
    cout << s.c_str() << endl;

    s1 = "this";
    s2 = "that";
    s1 = s2;  //s1, s2分别指向不同的地址,但内容相同
    cout << &s1 << endl;
    cout << s1.c_str() << endl;
    cout << &s2 << endl;
    cout << s2.c_str() << endl;

    String s3(s2);
    cout << &s3 << endl;
    cout << s3.c_str() << endl;

    return 0;
}

运行结果如下:

Good Luck!
HAHAHAHAHA!
0xffb7e580
that
0xffb7e57c
that
0xffb7e578
that

注意深拷贝(复制构造函数、对象的赋值)和浅拷贝(将字符串赋值给对象)的使用场合。

课程笔记。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值