【C++知识点】重载

简介: 【C++知识点】重载

重载

函数重载

类里面跟普通函数重载类似:

class PrintFunc{
public: 
    void print(int i) {
        cout << i << endl; 
    } 
    void print(double f) {
        cout << f << endl;
    } 
    void print(char c[]) { 
        cout <<c << endl;
    }
};

运算符重载

运算符重载的本质为函数重载,但有一定的规则需要遵循。

  1. 重载运算符时,运算符的运算顺序和优先级不变,操作数个数不变。
  2. 不能创造新的运算符,只能重载C++中已有的运算符,并且规定有6个运算符不能重载,如表所示。
  3. 微信截图_20230619210618.png运算符重载是针对新类型的实际需求,对原有的运算符进行适当的改造。一般来讲,重载后的运算符的功能应当与运算符的实际意义相符。

运算符重载形式有两种,重载为类的成员函数和重载为类的友元函数。

运算符重载为类的成员函数的一般语法形式为: 
函数类型 operator 运算符(形参表)
{  
    函数体;
} 
运算符重载为类的友元函数的一般语法形式为:
friend 函数类型 operator 运算符(形参表) 
{  
    函数体;
}

由于运算符种类与数量较多,以以下4大类中的一些符号来讲解。4大类:

  1. 赋值运算符的重载
  2. 算术运算符的重载
  3. 关系运算符的重载
  4. 其他较为特殊的重载

赋值运算符的重载

class Student{
private:
    string name;
    int age;
public:
    Student(){
        name = "zs";
        age = 20;
    }
    Student(string name,int age){
        this->name = name;
        this->age = age;
    }
    void operator=(const Student &t){
        this->name = t.name;
        this->age = t.age;
    }
    void printInfo(){
        cout << this->name << " " << this->age <<endl;
    }
};
int main(){
    Student s1,s2("王五",30);
    s2.printInfo();
    s2 = s1; //使用赋值运算符
    s2.printInfo();
    return 0;
}

算术运算符的重载

class complex{
public:
    complex(){}
    complex(double r,double i){
        this->r = r;
        this->i = i;
    }
    complex operator+(complex &c){
        complex c2;
        c2.r = this->r + c.r;
        c2.i = this->i + c.i;return c2; 
    }
    complex operator-(complex &c){
        complex c2;
        c2.r = this->r - c.r;
        c2.i = this->i - c.i;
        eturn c2; 
    }
    void print(){
        cout << this->r <<" " << this->i <<endl;
    }
private:
    double r,i;
};
int main(){
    complex c1(11,12),c2(10,11),c3,c4;
    c3 = c1 + c2;
    c3.print();
    c4 = c1 - c2;
    c4.print();
}

关系运算符的重载

class Student{
    private:
    string name;
    int age;
    public:
    Student(){}
    Student(string name,int age){
        this->name = name;
        this->age = age;
    }
    bool operator>(Student &t){
        if(this->name > t.name){
            return true;
        }else{
            return false;
        }
    }
};
int main(){
    Student s1("aaa",11),s2("bbb",22);
    bool b = s2 > s1; //bool b = s1 < s2; bool b = s2 > s1;
    cout << b << endl;
    return 0;
}

其他较为特殊的重载

const int SIZE = 5;
class Safearr{
    private:
    int arr[SIZE];
    public:
    Safearr(){
        register int i = 0;
        for(;i<SIZE;i++){
            arr[i] = i;
        }
    }
    int operator[](int i){
        if(i >=SIZE){
            cout << "下标超过最大值" <<endl;
            return -1;
        }
        return arr[i];
    }
};
int main(){
    Safearr arr;
    cout << arr[1] <<endl;cout << arr[100] <<endl;
    return 0;
}
目录
相关文章
|
1月前
|
C++
C++类自加自减与<<运算符的重载实现
C++类自加自减与<<运算符的重载实现
|
2月前
|
存储 人工智能 算法
【一站式备考指南】一文掌握 C++ 程序设计 课程 知识点
【一站式备考指南】一文掌握 C++ 程序设计 课程 知识点
97 0
|
2月前
|
存储 算法 编译器
【C++ 内存管理 重载new/delete 运算符 新特性】深入探索C++14 新的/删除的省略(new/delete elision)的原理与应用
【C++ 内存管理 重载new/delete 运算符 新特性】深入探索C++14 新的/删除的省略(new/delete elision)的原理与应用
59 0
|
2月前
|
算法 C++ 开发者
【C++运算符重载】深入理解C++中的流运算符 >>和<<重载
【C++运算符重载】深入理解C++中的流运算符 >>和<<重载
38 0
|
2天前
|
C++ 编译器
|
7天前
|
编译器 C++
【C++】类与对象(运算符重载、const成员、取地址重载)
【C++】类与对象(运算符重载、const成员、取地址重载)
12 2
|
7天前
|
编译器 C语言 C++
【C++】基础知识讲解(命名空间、缺省参数、重载、输入输出)
【C++】基础知识讲解(命名空间、缺省参数、重载、输入输出)
12 1
【C++】基础知识讲解(命名空间、缺省参数、重载、输入输出)
|
12天前
|
C++
【C++小小知识点】重载、覆盖(重写)、隐藏(重定义)的对比【详解】(23)
【C++小小知识点】重载、覆盖(重写)、隐藏(重定义)的对比【详解】(23)
|
2月前
|
算法 安全 编译器
【C++运算符重载】深入理解C++中的类型转换与重载
【C++运算符重载】深入理解C++中的类型转换与重载
32 0
|
2月前
|
算法 程序员 C++
【C++运算符重载】探究C++中的下标运算符[]重载
【C++运算符重载】探究C++中的下标运算符[]重载
14 0