c++多态性 运算符重载

多态是指同样的消息被不同类型的对象接收时导致不同的行为。

         :  重载多态
              强制多态
              包含多态
              参数多态




运算符重载


因为c++中的运算符操作对象只适合基本数据类型,所以对用户自定义的类型如类进行运算符重载后使用。

: 重载为类的非静态成员函数和重载为非成员函数。

语法形式如下:

返回类型 operator 运算符(形参表)
{
      函数体
}
当重载为非成员函数时,有时需要访问的运算符参数涉及类的私有成员,可以把该函数的函数声明为该类的友元函数。


重载复数类:

重载为两类的区别:1.形参个数区别
                                    2.当重载为函数成员时,左操作数必须是与函数返回类型相应的自定义类型;而重载为非函数成员时左右操作数任何类型都可以。
                               



重载为非成员函数
#include<iostream>
using namespace std;
class complax
{
public:
    complax(double r= 0, double i= 0): real(r), imag(i){}
    friend complax operator+(complax &a, complax &b);
    friend complax operator-(complax &a, complax &b);
    friend ostream &operator<<(ostream &out, complax &c);
private:
    double real, imag;
};

complax operator+(complax &a, complax &b)    //无friend
{
    return complax(a.real+b.real, a.imag+b.imag);
}

complax operator-(complax &a, complax &b)
{
    return complax(a.real-b.real, a.imag-b.imag);
}

ostream &operator<<(ostream &out, complax &c)
{
    out<<c.real<<"+"<<c.imag<<"i";
    return out;
}

int main()
{
    complax a(3, 4), b(5, 6), c;
    cout<<a<<endl;
    cout<<b<<endl;
    c = a+ b;
    cout<<c<<endl;
    c = a-b;
    cout<<c<<endl;
    return 0;
}


重载为成员函数


#include<iostream>
using namespace std;
class complax
{
public:
    complax(double r= 0, double i= 0): real(r), imag(i){}
    complax operator+( complax &b);
    complax operator-( complax &b);
    friend ostream &operator<<(ostream &out, complax &c);
private:
    double real, imag;
};

complax complax :: operator+(complax &b)
{
    return complax(real+b.real, imag+b.imag);
}

complax complax ::operator-(complax &b)                    //当不是友元函数是函数定义要这样写    返回值类型  作用域  函数名{函数体}
{
    return complax(real-b.real, imag-b.imag);
}

ostream &operator<<(ostream &out, complax &c)
{
    out<<c.real<<"+"<<c.imag<<"i";
    return out;
}

int main()
{
    complax a(3, 4), b(5, 6), c;
    cout<<a<<endl;
    cout<<b<<endl;
    c = a+ b;
    cout<<c<<endl;
    c = a-b;
    cout<<c<<endl;
    return 0;
}


单目运算符的前置与后置

#include<iostream>
using namespace std;
class Time
{
public:
    Time(int hour , int minute , int second);
    void showTime()const;
    Time& operator++();
    Time operator++(int);
private:
    int hour, minute, second;
};

Time ::Time(int hour, int minute, int second)
{
    if(hour >= 0&&hour<= 24&&minute>=0&&minute<60&&second>=0&&second<60)
    {
        this->hour = hour;
        this->minute = minute;    //相当于hour= h。就是将形参赋值给类里的成员
        this->second = second;
    }
    else
        cout<<"Time is error!"<<endl;
}

void Time::showTime() const
{
    cout<<hour<<":"<<minute<<":"<<second<<endl;
}

Time &Time::operator++()
{
    second++;
    if(second>=60)
    {
        second-=60;
        minute++;
        if(minute>=60)
        {
            minute-=60;

            hour =(hour+1)%24;
        }
    }
    return *this;
}

Time Time::operator++(int)
{
    Time ord = *this;
    ++(*this);
    return ord;
}


int main()
{
    Time T(23, 59, 59);
    cout<<"Frist time output"<<endl;
    T.showTime();
    cout<<"show T++"<<endl;
    (T++).showTime();
    cout<<"show ++T"<<endl;
    (++T).showTime();
    return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值