面向对象的C++渐进


数值类型:地址?值?指针?类?函数?自定义?
什么可以重载?怎么重载?

渐进C++

运算符重载

运算符基本知识
重载运算符知识

可重载运算符运算符表示不可重载运算符运算符表示
双目算术运算符+ (加),-(减),*(乘),/(除),% (取模)成员访问运算符.*
关系运算符==(等于),!= (不等于),< (小于),> (大于>,<=(小于等于),>=(大于等于)成员指针访问运算符.*, ->*
逻辑运算符| |(逻辑或),&&(逻辑与),!(逻辑非)域运算符:: *
单目运算符+ (正),-(负),*(指针),&(取地址)长度运算符sizeof(int)
自增自减运算符++(自增),- -(自减)条件运算符(a>b)?a :b
位运算符(按位或),& (按位与),~(按位取反),^(按位异或),,<< (左移),>>(右移)预处理符号#
赋值运算符=, +=, -=, *=, /= , % = , &=,|=, ^=, <<=, >>=
空间申请与释放new, delete, new[ ] , delete[]
其他运算符()(函数调用),->(成员访问),,(逗号),

重载运算符

  • 定义重载的运算符就像定义函数,只是该函数的名字是operator@, 这里的@代表了被重载的运算符。函数的参数中参数个数取决于两个因素。运算符是一元(一个参数)的还是二元(两个参数);运算符被定义为全局函数(对于一元是一个参数,对于二元是两个参数)还是成员函数(对于一元没有参数,对于二元是一个参数-此时该类的对象用作二元参数)。

1. + 号运算符重载

  • 加号运算符重载
    1.如果想让自定义数据类型 进行+运算,那么就需要重载 + 运算符;2. 在成员函数 或者 全局函数里 重写一个+运算符的函数 ;3.函数名 operator+ () {};4.运算符重载 也可以提供多个版本。

  • 您可以重定义或重载大部分 C++ 内置的运算符。这样,您就能使用自定义类型的运算符。重载的运算符是带有特殊名称的函数,函数名是由关键字 operator 和其后要重载的运算符符号构成的。与其他函数一样,重载运算符有一个返回类型和一个参数列表。

    Box operator+(const Box&);
    
  • 声明加法运算符用于把两个 Box 对象相加,返回最终的 Box 对象。大多数的重载运算符可被定义为普通的非成员函数或者被定义为类成员函数。如果我们定义上面的函数为类的非成员函数,那么我们需要为每次操作传递两个参数,如下所示:

    Box operator+(const Box1&, const Box2&)
    {
    	Box temp ;
    	temp.a1= Box1.a1+Box2.a1;//a1、a2皆为Box类成员且为基本数据类型
    	temp.a2= Box1.a2+Box2.a2;
    	return temp;
    }
    

2. <<  运算符重载

  • 左移运算符<<。cout << 直接对Person自定义数据类型 进行输出(写到全局函数中 ostream& operator<< ( ostream & cout, Person & p1 ) {});如果重载时候想访问 p1的私有成员,那么全局函数要做Person的友元函数。

    class Person{
    	friend ostream& operator<<(ostream& cout, Person& person);
    public:
    	Person(int id,int age){
    		mID = id;
    		mAge = age;
    	}
    private:
    	int mID;
    	int mAge;
    };
    //cout<<person---->cout
    ostream& operator<<(ostream& cout, Person& person){
    	cout << "ID:" << person.mID << " Age:" << person.mAge;
    	return cout;
    }
    int main(){
    	Person person(1001, 30);
    	//cout << person; //cout.operator+(person)
    	cout << person << " | " << endl;
    	return EXIT_SUCCESS;
    }
    

3. ++ 运算符重载

  • 当编译器看到++a(前置++),它就调用operator++(a),当编译器看到a++(后置++),它就会去调用operator++(a,int).

    class Complex{
    	friend ostream& operator<<(ostream& os,Complex& complex){
    		os << "A:" << complex.mA << " B:" << complex.mB << endl;
    		return os;
    	}
    public:
    	Complex(){
    		mA = 0;
    		mB = 0;
    	}
    	//重载前置++
    	Complex& operator++(){
    		mA++;
    		mB++;
    		return *this;
    	}
    	//重载后置++
    	Complex operator++(int){	
    		Complex temp;
    		temp.mA = this->mA;
    		temp.mB = this->mB;
    		mA++;
    		mB++;
    		return temp;
    	}
    	//前置--
    	Complex& operator--(){
    		mA--;
    		mB--;
    		return *this;
    	}
    	//后置--
    	Complex operator--(int){
    		Complex temp;
    		temp.mA = mA;
    		temp.mB = mB;
    		mA--;
    		mB--;
    		return temp;
    	}
    	void ShowComplex(){
    		cout << "A:" << mA << " B:" << mB << endl;
    	}
    private:
    	int mA;
    	int mB;
    };
    
    void test(){
    	Complex complex;
    	complex++;
    	cout << complex;
    	++complex;
    	cout << complex;
    
    	Complex ret = complex++;
    	cout << ret;
    	cout << complex;
    
    	cout << "------" << endl;
    	ret--;
    	--ret;
    	cout << "ret:" << ret;
    	complex--;
    	--complex;
    	cout << "complex:" << complex;
    }
    

4. -> 运算符重载

  • 指针运算符重载,运算符 -> 通常与指针引用运算符 * 结合使用,用于实现"智能指针"的功能。这些指针是行为与正常指针相似的对象,唯一不同的是,当您通过指针访问对象时,它们会执行其他的任务。比如,当指针销毁时,或者当指针指向另一个对象时,会自动删除对象。

    #include <iostream>
    #include <vector>
    using namespace std;
    // 假设一个实际的类
    class Obj {
       static int i, j;
    public:
       void f() const { cout << i++ << endl; }
       void g() const { cout << j++ << endl; }
    };
    // 静态成员定义
    int Obj::i = 10;
    int Obj::j = 12;
    // 为上面的类实现一个容器
    class ObjContainer {
       vector<Obj*> a;
    public:
       void add(Obj* obj)
       { 
          a.push_back(obj);  // 调用向量的标准方法
       }
       friend class SmartPointer;
    };
    // 实现智能指针,用于访问类 Obj 的成员
    class SmartPointer {
       ObjContainer oc;
       int index;
    public:
       SmartPointer(ObjContainer& objc)
       { 
           oc = objc;
           index = 0;
       }
       // 返回值表示列表结束
       bool operator++() // 前缀版本
       { 
         if(index >= oc.a.size() - 1) return false;
         if(oc.a[++index] == 0) return false;
         return true;
       }
       bool operator++(int) // 后缀版本
       { 
          return operator++();
       }
       // 重载运算符 ->
       Obj* operator->() const 
       {
         if(!oc.a[index])
         {
            cout << "Zero value";
            return (Obj*)0;
         }
         return oc.a[index];
       }
    };
    int main() {
       const int sz = 10;
       Obj o[sz];
       ObjContainer oc;
       for(int i = 0; i < sz; i++)
       {
           oc.add(&o[i]);
       }
       SmartPointer sp(oc); // 创建一个迭代器
       do {
          sp->f(); // 智能指针调用
          sp->g();
       } while(sp++);
       return 0;
    }
    

其它操作符重载

其它内容:


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Echo一

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值