栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

c++运算符重载(复习)

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

c++运算符重载(复习)

目录

何为运算符重载?

 为什么需要运算符重载?

 运算符重载的分类?

如何运算符重载?

友元函数

 或者类成员函数

练习题

1.矩阵相乘(运算符重载)

2.学生生日差值计算(运算符重载)

3自增运算符



何为运算符重载?

运算符重载,就是对已有的运算符重新进行定义,赋予其另一种功能,以适应不同的数据类型。

太过抽象了?举个栗子

比如下面这段代码

#include
uisng namespace;
class student{
    int num;
    public:
    student(int n){
       num=n;
    }

};
int main(){
    student s1(1);
    student s2;
    s2=s1;   //利用了隐含的运算符重载“=”
}

上面这段代码我们可以看到用student 定义了s1和s2两个对象,s1的num初始值为1,s2=s1则是利用了隐含的运算符重载“=”,将s1的num赋值给了s2的num

 为什么需要运算符重载?

上面的例子我们也可以看到运算符重载的作用是”悄无声息“而且“作用巨大”的。

除了上面这个栗子,还能有其他栗子吗?

如果我们定义了运算符重载,增加代码的可读性,达到了一定的封装的目的,可以将其运用于时钟类的自增和自减,线性代数的矩阵加减乘法等等。。。 

 运算符重载的分类?

解决了为什么需要运算符重载后,我们来看看哪些运算符可以重载

这五个运算符     .       .*      ::      ?  :    ->*     不能重载,也不能创造新的运算符如    @

以下是可重载运算符

双目算术运算符+ (加),-(减),*(乘),/(除),% (取模)
关系运算符==(等于),!= (不等于),< (小于),> (大于),<=(小于等于),>=(大于等于)
逻辑运算符||(逻辑或),&&(逻辑与),!(逻辑非)
单目运算符+ (正),-(负),*(指针),&(取地址)
自增自减运算符++(自增),--(自减)
位运算符| (按位或),& (按位与),~(按位取反),^(按位异或),,<< (左移),>>(右移)
赋值运算符=, +=, -=, *=, /= , % = , &=, |=, ^=, <<=, >>=
空间申请与释放new, delete, new[ ] , delete[]
其他运算符()(函数调用),->(成员访问),,(逗号),[](下标)

 看到这里你会惊叹 编译器的operator在无形中帮助我们干了好多事,所有关于类的运算的,只有你想不到的,没有编译器做不到的

如何运算符重载?

实现方式——1友元函数,2类成员函数 

友元函数
#include
using namespace std;
class student{
    int num;
    public:
    student(){}
    student(int n){
       num=n;
    }
    friend student operator+(const student &s1,const student &s2);
    void show(){
        cout< 
 

 或者类成员函数
#include
using namespace std;
class student{
    int num;
    public:
    student(){}
    student(int n){
       num=n;
    }
    student operator+(const student &s1){//这里
        student s;                       //修改
        s.num=s1.num+this->num;         //了
        return s;                       //代码
    }                                   //了
    void show(){
        cout< 
 

练习题

现在我们学会了,来写几道题目试试把!

1.矩阵相乘(运算符重载)
#include
using namespace std;
int c,n;//c是输入矩阵的次数,n是矩阵的阶数
class myMatrix{
    int a[10][10];//可以用int **a;
    int n;//定义n*n的矩阵
    public:
    myMatrix(int _n){n=_n;}
    friend myMatrix operator*(myMatrix& c1, myMatrix& c2);
    void input(){
        for(int i=0;i>a[i][j];
            }
        }
    }
    void show(){
        for(int i=0;ic;
    cin>>n;
    function();
    return 0;
}

2.学生生日差值计算(运算符重载)
#include
#include
using namespace std;
bool isyear(int year){
        return (year%4==0&&year%100!=0 )||(year%400==0);
}
class date{
    public:
    int year,month,day;
    date(){}
    void set(int y,int m,int d){
        year=y;
        month=m;
        day=d;
    }
    int getDayofYear(){// 两个数的只要把他放里面就可以
        int sum=day;
        int a[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
        int b[13]={0,31,29,31,30,31,30,31,31,30,31,30,31};
        for(int i=0;id.year){
            for(int i=1;igetDayofYear()+f-d.getDayofYear();
        }
        else if(yeargetDayofYear();
            
        }
        else{
            sum=abs(d.getDayofYear()-this->getDayofYear());
        }
        return sum;//8+365-1=372
    }

};
class student{
    string name;
    date birth;
    public:
    student(){}
    string getname(){return name;}
    void set(string n,int y,int m,int d){
        name=n;
        birth.set(y,m,d);

    }
    friend int operator-(student &s1,student & s2);
};
    int operator-(student &s1,student & s2){
        int s;
        s=s1.birth.compare(s2.birth);//s1和s2
        return s;
    }
    
int main(){
    string name;
    int y,m,d;
    int n;
    cin>>n;
    student *p=new student[n];
    for(int i=0;i>name>>y>>m>>d;
        p[i].set(name,y,m,d);        
    }
    int sum=0,min=0,max=0,s1=0,s2=0;
    for(int i=0;imax) {
				max=sum;
				s1=i;
                s2=j;
			}

        }
    }
    //Tom和Jimmy年龄相差最大,为372天。
    cout< 
 

3自增运算符

前缀++

Time operator++ ()  

后缀++

Time operator ++(int)

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/604573.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号