C++面向对象2:运算符重载与友元函数

目录

1.运算符重载(重新赋予运算符新的含义)

回顾函数重载

OpenCV运算符重载

运算符重载

2.自己实现字符串重载

3.友元函数

4.<<重载

5.类型转换

6.自增自减运算符重载

7.c++中的运算符


1.运算符重载(重新赋予运算符新的含义)

回顾函数重载

函数名一样,参数列表不同

Mat add(Mat& A, Mat& B);
Mat add(Mat& A, float b);
Mat add(float a, Mat& B);
Mat mul(Mat& A, Mat& B);
Mat mul(Mat& A, float b);
Mat mul(float a, Mat& B);

OpenCV运算符重载

Mat A, B;
float a, b;
//…
Mat C = A + B;//A矩阵里每个像素和B矩阵里面的每个像素相加
Mat D = A * B;
Mat E = a * A;
#include <iostream>
#include <opencv2/opencv.hpp>

using namespace std;

int main()
{
    float a[6]={1.0f, 1.0f, 1.0f, 2.0f, 2.0f, 2.0f};
    float b[6]={1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f};
    cv::Mat A(2, 3, CV_32FC1, a); 
    cv::Mat B(3, 2, CV_32FC1, b);

    cv::Mat C = A * B;// 运算符重载

    cout << "Matrix C = " << endl 
         << C << endl;// 运算符重载
   return 0;
}

输出结果 

运算符重载

对于自定义的类型,我们要把这个运算符要做什么进行自定义

std::string s("Hello");//std::string(类) 
s += "C";//运算符重载(字符串的链接并修改字符串的内容)
s.operator+=("and CPP!");//与上面等价

用char类型的数组作为字符串的表达方式,非常容易越界,字符串的连接,拷贝,不是特别安全

2.自己实现字符串重载

class MyTime
{
    int hours;
    int minutes;
  public:
    MyTime(): hours(0), minutes(0){}//构造函数
    MyTime(int h, int m): hours(h), minutes(m){}//带有参数(构造函数重载)
//格式:
函数类型 operator运算符名(参数列表)
    MyTime operator+(const MyTime & t) const//运算符重载是类里面特殊的函数
//常成员函数可以访问常对象中的数据成员,但仍然不允许修改对象中数据成员的值
    {
        MyTime sum;
        sum.minutes = this->minutes + t.minutes;//当前对象+参数对象	
        sum.hours = this->hours + t.hours;
        sum.hours += sum.minutes / 60;
        sum.minutes %= 60;
        return sum;
    }
    std::string getTime() const;
};

MyTime t1(2, 40);
MyTime t2 = t1 + 20;
//两个MyTime相加,但是+一个整数就不行
MyTime operator+(int m) const
{
    MyTime sum;
    sum.minutes = this->minutes + m;
    sum.hours = this->hours;
    sum.hours += sum.minutes / 60;
    sum.minutes %= 60;
    return sum;
}

MyTime t1(2, 40);
MyTime t2 = t1 + "one hour";
//加字符串
MyTime operator+(const std::string str) const
{
    MyTime sum = *this;//sum=当前对象
    if(str=="one hour")//字符串内容相同
        sum.hours = this->hours + 1;
    else
        std::cerr<< "Only \"one hour\" is supported." << std::endl;
    return sum;
}
//如果20 + t1;//20不是对象,不能重载

3.友元函数

是一个朋友函数

        A)声明在类的内部

        B)可以获取公有和私有的成员变量

        C)但它不是类的成员

class MyTime
{
  // ...
  public:
    friend MyTime operator+(int m, const MyTime & t)
    {
        return t + m;//调用重载运算符
    }
};
第二种写法
class MyTime
{
  // ...
  public:
    friend MyTime operator+(int m, const MyTime & t);//声明
};
MyTime operator+(int m, const MyTime & t)//定义写在外面,他是友元函数不是成员函数,没有::MyTime ::operator+    没有用类作限定的函数,非成员函数
{
    return t + m;
}

4.<<重载

cout << t1;std::ostream<< MyTime//cout << t1第一个操作数是std::ostream
friend std::ostream & operator<<(std::ostream & os, const MyTime & t)
{
    std::string str = std::to_string(t.hours) + " hours and " 
             + std::to_string(t.minutes) + " minutes.";
    os << str;
    return os;//一定要返回
}

5.类型转换

将一个类的对象转换成另一类型数据

自定义类型转换

一般形式:operator 类型名(){

        实现转换的语句

}

注意:类型转换函数只能作为成员函数,不能作为友元函数或普通函数

//implicit conversion
operator int() const
{
    return this->hours * 60 + this->minutes;
}//当需要的时候系统会自动调用
//explicit conversion
explicit operator float() const// 加了explicit ,不可进行隐式类型转换,必须显示,否则语法错误
{
    return float(this->hours * 60 + this->minutes);
}
MyTime t1(1, 20);
int minutes = t1; //implicit conversion调用operator int() const

float f = float(t1); //explicit conversion. 

Int转Mytime

第一种:借助于构造函数

MyTime(int m): hours(0), minutes(m)
{
    this->hours += this->minutes / 60;
    this->minutes %= 60;
}
MyTime t2 = 70;//只会执行构造函数,相当于初始化

第二种:运算符重载

MyTime & operator=(int m)
{
    this->hours = 0;
    this->minutes = m;
    this->hours = this->minutes / 60;
    this->minutes %= 60;
    return *this;
}
MyTime t3;//创建对象
t3 = 80;//赋值操作

6.自增自减运算符重载

// 前置(修改后的值返回)
MyTime& operator++()//mytime类型引用
{
    this->minutes++;
    this->hours += this->minutes / 60;
    this->minutes = this->minutes % 60;
    return *this; 
}
// 后置()
MyTime operator++(int)
{
MyTime old = *this; //以前的值保留
operator++(); //调用第一个重载
return old; 
}

7.c++中的运算符

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值