重载

  • 实质:重载的实质就是写一个重载函数
  • 语法:函数类型 operator 运算符名称 {对运算符的重载处理}
  • 说明:如果要对加号进行重载,则 “operator +” 就是函数名。
  • 方法
    (1)成员函数法
    定义:把运算符重载的函数作为类的成员函数
    说明:在该方法中,重载函数只有一个参数,因为由于重载函数是Complex类中的成员函数,因此有一个参数是隐含的,运算符函数是用this指针隐式访问类对象的成员。如:this –> real + c2.real。this –> real就是 c1.real。

    (2)非成员函数法
    定义:运算符重载的函数不是类的成员函数(可以是一个普通函数),放在类外,在类中把它声明为友元函数。
    优点:代码看起来更加清晰。

例1: 通过重载实现复数相加(i.e. 对运算符 “+” 的重载)

法一:成员函数法

#include <iostream>
using namespace std;

//Step 1: 定义Complex类,及声明有关函数:复数相加函数
class Complex 
{
    private:
        double real;
        double imag;
    public:
        Complex(){real=0;imag=0;} //定义构造函数
        Complex(double r,double i){real=r;imag=i;} //构造函数重载
        Complex operator + (Complex &c2);  //声明重载运算符+的函数
        void display();//声明输出函数
};

//Step 2: 定义重载运算符+的函数: operator +()
Complex Complex::operator +(Complex &c2)
{
    Complex c;
    c.real=real+c2.real;
    c.imag=imag+c2.imag;
    return c;
}

//Step 3: 定义输出函数display()
void Complex::display()
{cout<<"("<<real<<","<<imag<<"i)"<<endl;}


//Step 4: 在主函数内调用其他函数
int main()
{
    Complex c1(3,4),c2(5,-10),c3;
    c3=c1+c2;
    cout<<"c1="; c1.display();
    cout<<"c2="; c2.display();
    cout<<"c1+c2="; c3.display();
    return 0;
}

输出:
这里写图片描述

法二:非成员函数法——友元函数法


#include <iostream>
using namespace std;

//Step 1: 定义Complex类,及声明有关函数:复数相加函数
class Complex 
{
    private:
        double real;
        double imag;
    public:
        Complex(double r=0,double i=0); //声明构造函数的同时初始化(即:赋初值)
        friend Complex operator + (Complex c1,Complex c2);  //声明重载函数作为友元函数
        void display();//声明输出函数
};

//Step 2: 定义构造函数
Complex::Complex(double r,double i)
{  
    real=r;imag=i;
} 

//Step 3: 定义重载运算符+的函数: operator +()
Complex operator +(Complex c1,Complex c2)
    {return Complex(c1.real+c2.real,c1.imag+c2.imag);}

//Step 4: 定义输出函数display()
void Complex::display()
{cout<<"("<<real<<","<<imag<<"i)"<<endl;}


//Step 5: 在主函数内调用其他函数
int main()
{
    Complex c1(3,4),c2(5,-10),c3;
    c3=c1+c2;
    cout<<"c1="; c1.display();
    cout<<"c2="; c2.display();
    cout<<"c1+c2="; c3.display();
    return 0;
}
//ps:这里之所以要初始化对象数据成员,是因为最后建立对象c3时,没有传递实参,因此必须要有初始化的值才行

例2: 通过重载实现复数相乘(i.e. 对运算符 “*” 的重载)

这里用友元函数法来实现

#include <iostream>
using namespace std;

//Step 1: 定义Complex类,及声明有关函数:复数相加函数
class Complex 
{
    private:
        double real;
        double imag;
    public:
        Complex(double r=0,double i=0); //声明构造函数
        friend Complex operator * (Complex c1,Complex c2);  //声明重载函数作为友元函数
        void display();//声明输出函数
};

//Step 2: 定义构造函数
Complex::Complex(double r,double i)
{  
    real=r;imag=i;
} 


//Step 3: 定义重载运算符*的函数: operator *()
Complex operator *(Complex c1,Complex c2)
{
    Complex c;
    c.real=c1.real*c2.real-c1.imag*c2.imag;
    c.imag=c1.real*c2.imag+c1.imag*c2.real;
    return c;    
}


//Step 4: 定义输出函数display()
void Complex::display()
{cout<<"("<<real<<","<<imag<<"i)"<<endl;}


//Step 5: 在主函数内调用其他函数
int main()
{
    Complex c1(3,4),c2(5,-10),c3;
    c3=c1*c2;
    cout<<"c1="; c1.display();
    cout<<"c2="; c2.display();
    cout<<"c1*c2="; c3.display();
    return 0;
}

这里写图片描述

例3: 有两个矩阵a和b,均为2行3列。求两个矩阵之和。重载运算符“+”,使之能用于矩阵相加(如c=a+b)

#include <iostream>
using namespace std;

//Step 1: 定义Matrix类,及声明有关函数:矩阵相加函数
class Matrix            //定义Matrix类
 {
    public:
        Matrix();                                          //声明构造函数
        friend Matrix operator+(Matrix ,Matrix );        //重载运算符“+”
        void input();                                      //输入数据函数
        void display();                                    //输出数据函数
    private:
        int mat[2][3];
 };

//Step 2: 定义构造函数
Matrix::Matrix()          
{
    for(int i=0;i<2;i++)
        for(int j=0;j<3;j++)
            mat[i][j]=0; //初始化对象们的公有成员mat[i][j]
}

//Step 3: 定义重载运算符+的函数: operator +()
Matrix operator+(Matrix a,Matrix b)      //定义重载运算符“+”函数
{
    Matrix c;
    for(int i=0;i<2;i++)
        for(int j=0;j<3;j++)
            {c.mat[i][j]=a.mat[i][j]+b.mat[i][j];}
    return c;
}


//Step 4: 定义输入函数input()和输出函数display()
void Matrix::input()           //定义输入数据函数
{
    cout<<"input value of matrix:"<<endl;
    for(int i=0;i<2;i++)
        for(int j=0;j<3;j++)
            cin>>mat[i][j];
}

void Matrix::display()           //定义输出数据函数
{
    for (int i=0;i<2;i++)
    {
        for(int j=0;j<3;j++)
        {cout<<mat[i][j]<<" ";}
        cout<<endl;
    }
}

//Step 5: 在主函数内调用其他函数
int main()
{
    Matrix a,b,c;
    a.input();
    b.input();
    cout<<endl<<"Matrix a:"<<endl;
    a.display();
    cout<<endl<<"Matrix b:"<<endl;
    b.display();
    c=a+b;           //用重载运算符“+”实现两个矩阵相加
    cout<<endl<<"Matrix c = Matrix a + Matrix b :"<<endl;
    c.display();
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值