Linux 学习记录43(C++篇)

本文详细介绍了C++中的友元机制,包括友元函数和友元类的使用以及注意事项。同时,讲解了常成员函数和常对象的概念,以及mutable关键字的作用。此外,重点探讨了运算符重载的原理、格式和常见运算符的重载实现,包括算数、赋值、关系和自增/自减运算符等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Linux 学习记录43(C++篇)

在这里插入图片描述

一、友元

友元,就意味着友元函数或者友元类访问自己的所有成员
友元是单向的

1. 友元函数

(1. 全局函数作为友元函数

全局函数可以访问类中所有的变量

class test
{
private:
    int num;
    string str;
public:
    void set(int a)
    {
        num = a;
    }
    //将全局的show函数声明为友元函数
    friend void show (test buf);
};

void show (test buf)
{
    cout << buf.num <<endl;
}

(2. 类中的成员函数作为友元

该成员函数,可以访问另一个类中所有成员
如果类中的成员函数作为另一个类的友元函数,要求该函数:类内声明,类外定义
class的前置声明,只起到有该类的作用,不能起到识别类中函数的作用

#include <iostream>
using namespace std;
class B;  //类的前置声明,表明有该类

class A
{
    int num;
public:
    void set(int a)
    {
        num = a;
    }
    //把全局的show函数声明为友元函数
    friend void show(A a1);
    //声明了一个show成员函数
    //show是B类的一个友元函数,类内声明类外定义
    void set(B b1,int a);
};
class B
{
public:
    int high;
private:
    //set是一个私有的成员函数
    void set(int num)
    {
        cout << "B中的set" << endl;
        high = num;
    }
public:
    //声明了A中的成员函数show为B类的友元函数
    friend void A::set(B b1,int a);
    void show()
    {
        cout << "B中的show:" << high << endl;
    }
};

//全局函数
void show(A a1)
{
    cout << "全局的show:  ";
    cout << a1.num << endl;
}
//A类中set函数的类外定义
void A::set(B b1,int a)
{
    num = a;
    cout << "A中的set:" << endl;
    b1.set(100);
}

int main()
{
    A p;
    B p2;
    p2.high = 18;
    p.set(90);
    //show(p);
    p.set(p2,90);
    //调用B中的成员函数
    p2.show();
    return 0;
}

2. 友元类

把一个类声明为另一个类的友元
把A类声明为B类的友元(friend class A),A类内可以访问B类中的所有成员。


3. 友元的注意事项

1.  类的前置声明,只是表明了有这个类,但是并不能获取到类中有哪些成员变量和成员函数
2. 友元是单向的,A是B的友元,并不能说明B是A的友元类
3. 友元没有传递性,A是B的友元,B是C的友元,并不能说明A是C的友元
4. 友元需要谨慎的使用,如果添加了友元后,访问权限就没有作用了,会破坏类的封装性
5. 如果需要成员函数左右另一个类的友元,一定要在类内声明成员函数在类外定义成员函数
6. 必须使用友元的场景:输入输出运算符重载

二、常成员函数和常对象(const)

1.常成员函数

常成员函数使用了const修饰,不能修改常成员属性,通常用于保护成员属性不被随意修改

格式:
返回值类型 函数名() const
{
	函数体;
} 
非静态成员函数this指针:类名 *const this;
非静态的常成员函数this指针:const 类名 *const this;
class test
{
private:
    int num;
public:
    /*定义常成员函数set*/
    void set (void)const
    {
        num = 20;//这里常成员函数不能修改所以会报错
    }
};
============================================
class test
{
private:
    int num;
public:
    /*定义常成员函数set*/
    void show (void)const
    {
        cout << num << endl;//这里没有修改,只是输出
    }
};

2. 常对象

使用const修饰的类对象

1. 常对象不能调用非常成员函数,默认隐式调用函数除外
2. 常对象默认只能调用常成员函数

3. mutable 关键字

用于修改必须修改成员属性时,可以给变量前加上nutable关键字

三、运算符重载

为了能够让运算符对自定义的类(数据类型)也能生效

1. 常见的运算符

在这里插入图片描述

2. 运算符重载的要

1. 只能对已有的运算符的重载
2. 运算符不能更改运算符的本质

3. 统一格式

类名 operatot 运算符(形参)
如拷贝赋值运算符就算对=运算符的重载

4. 调用时机

当使用该运算符运算时,系统自动调用

调用规则:要符合左调右参的原则(成员函数)

5. 运算符重载函数的格式

每个运算符重载函数都有两个版本 1.成员函数版 2.全局函数版

+运算符为例

  1. 成员函数版:c1+c2
Complex operator+(Complex &c2)
this指针自动指向类对象
  1. 成员函数版:c1+c2
Complex operator+(Complex &c1,Complex &c2)

还需要将全局函数声明为友元函数

注意事项
1. 返回值和参数不能同时是基本类型
2. 全局函数版需要为友元函数

6. 算数运算符重载

(1. 成员函数版

class test
{
private:
    int num1;
    int num2;
public:
    test()
    {}
    test(int a,int b):num1(a),num2(b)
    {}
    test operator+(test &a)
    {
        test buf;//用于存储临时运算结果
        buf.num1 = num1+a.num1;
        buf.num2 = num2+a.num2;
        return  buf;
    }
    void show(void)
    {
        printf("num1:%d num2:%d",num1,num2);
    }
};
int main()
{
    test date1(1,2);
    test dete2(3,4);
    test date3;
    date3 = date1+dete2;
    date3.show();
    return 0;
}

输出结果:
在这里插入图片描述

(2. 全局函数版

class test
{
private:
    int num1;
    int num2;
public:
    test()
    {}
    test(int a,int b):num1(a),num2(b)
    {}
    void show(void)
    {
        printf("num1:%d num2:%d",num1,num2);
    }
    /*需要将全局函数声明为友元函数*/
    friend test operator+(test &a,test &b);
};

test operator+(test &a,test &b)
{
    test buf;//用于存储临时运算结果
    buf.num1 = a.num1+b.num1;
    buf.num2 = a.num2+b.num2;
    return  buf;
}

int main()
{
    test date1(1,2);
    test dete2(3,4);
    test date3;
    date3 = date1+dete2;
    date3.show();
    return 0;
}

输出结果:
在这里插入图片描述

7. 赋值运算符重载

如+=,-=,*=等等

以(*=)为例

class test
{
private:
    int num1;
    int num2;
public:
    test()
    {}
    test(int a,int b):num1(a),num2(b)
    {}
    void show(void)
    {printf("num1:%d num2:%d",num1,num2);}
    /*返回值为返回本身*/
    test &operator*=(test &b)
    {//因为只需要修改类本身,使用不需要再存放临时值
        num1 *= b.num1;
        num2 *= b.num2;
        return  *this;//返回的也是本身
    }

};
int main()
{
    test date1(1,2);
    test dete2(3,4);
    test date3;
    date1 *= dete2;
    date1.show();
    return 0;
}

8. 关系运算符重载

如==,>,<,<=,>=等等
a关系运算符b
左操作数和右操作数可以互换
返回值/运算结果:bool类型的右值

以>成员函数版为例为例

class test
{
private:
    int num1;
    int num2;
public:
    test()
    {
    }
    test(int a,int b):num1(a),num2(b)
    {
    }
    void show(void)
    {
        printf("num1:%d num2:%d",num1,num2);
    }
    bool operator<(test b)
    {
        return num1<b.num1 && num2<num2;
    }
};


int main()
{
    test date1(1,2);
    test dete2(3,4);
    cout << (date1<dete2) << endl;
    return 0;
}

9. 自增/自减运算符

只有一个操作数,返回自身

成员函数版

  1. complex &operator++() 前自增(无哑元)
  2. complex &operator++(int) 后自增(有哑元)

全局函数版

  1. complex &operator++(complex &a) 前自增(无哑元)
  2. complex &operator++(complex &a,int) 后自增(有哑元)

(1. 前自增/自减

++a,–b

class test
{
private:
    int num1;
    int num2;
public:
    test()
    {
    }
    test(int a,int b):num1(a),num2(b)
    {
    }
    void show(void)
    {
        printf("num1:%d num2:%d",num1,num2);
    }
    test &operator++()
    {//前自增
        num1++;
        num2++;
        /*返回自增后的值*/
        return *this;
    }
    test &operator--()
    {//前自减
        num1--;
        num2--;
        /*返回自增后的值*/
        return *this;
    }
};


int main()
{
    test date1(1,2);
    test date2(3,4);

    ++date1;
    --date2;

    return 0;
}

(2. 后自增/自减

a++,b–

class test
{
private:
    int num1;
    int num2;
public:
    test()
    {
    }
    test(int a,int b):num1(a),num2(b)
    {
    }
    void show(void)
    {
        printf("num1:%d num2:%d",num1,num2);
    }
    test &operator++(int)
    {//后自增
        static test buf;//存放自增前的变量

        buf.num1 = num1++;
        buf.num2 = num2++;
        /*返回自增后的值*/
        return buf;//返回自增前的变量
    }
    test &operator--(int)
    {//后自减
        static test buf;//存放自增前的变量

        buf.num1 = num1--;
        buf.num2 = num2--;
        /*返回自增后的值*/
        return buf;//返回自增前的变量
    }
};


int main()
{
    test date1(1,2);
    test date2(3,4);

    date1++;
    date2--;

    return 0;
}

10. 不能重载的运算符

1. 域运算符 ::
2. sizeof()
3. 条件运算符(三目运算符)  ?:
4. .*
5. .

11. 插入/提取/输入输出运算符

插入/提取运算符 没有成员函数版

(1. 插入重载

cin是intream类的对象 >>
cout是ontream类的对象 <<
重载函数名:operator>>和operator<<
以 cout << a; 为例
函数名:operator>>
返回值:cout本身,输出流对象
需要的操作数:cout , a(输出类对象)

========全局函数版========
class test
{
private:
    int num1;
    int num2;
public:
    test(){}
    test(int a,int b):num1(a),num2(b){}
    /*将其声明为友元函数*/
    friend ostream &operator<<(ostream &out,test &buf);
};

ostream &operator<<(ostream &out,test &buf)
{
    out << buf.num1 <<endl;
    out << buf.num2 <<endl;
    return out;
}
int main(void)
{
    test buf1(1,2);
    test buf2(3,4);
    cout << buf1 << buf2 <<endl;
    return 0;
}

输出:
在这里插入图片描述

(2. 提取重载

class test
{
private:
    int num1;
    int num2;
public:
    test(){}
    test(int a,int b):num1(a),num2(b){}
    friend ostream &operator<<(ostream &out,test &buf);
    /*声明输入/提取重载函数为友元函数*/
    friend istream &operator>>(istream &in,test &buf);
};

ostream &operator<<(ostream &out,test &buf)
{
    out << buf.num1 <<endl;
    out << buf.num2 <<endl;
    return out;
}

/*定义了输入/提取重载 需要注意>>符号*/
istream &operator>>(istream &in,test &buf)
{
    in >> buf.num1 >> buf.num2;
    return in;
}

int main()
{
    test buf1;
    test buf2;

    cout << "请输入:" <<endl;
    cin >> buf1 >> buf2;
    cout << "输出" <<endl;
    cout << buf1 << buf2 <<endl;

    return 0;
}

在这里插入图片描述
输出

四、

思维导图

在这里插入图片描述

练习

1. 剩余常用符号函数重载

#include <iostream>
#include <iomanip>
#include <cstring>
#include <cstdio>
using namespace std;

class test
{
private:
    int num1;
    int num2;
public:
    test()
    {
    }
    test(int a,int b):num1(a),num2(b)
    {
    }
    void show(void)
    {
        printf("num1:%d num2:%d",num1,num2);
    }
/***********运算符重载***********/
    // !
    test operator+(test &a)
    {
        test buf;//用于存储临时运算结果
        buf.num1 = !a.num1;
        buf.num2 = !a.num2;
        return  buf;
    }

    // ~
    test &operator~()
    {
        num1=~num1;
        num2=~num2;
        return  *this;
    }

    test &operator++()
    {//前自增
        num1++;
        num2++;
        /*返回自增后的值*/
        return *this;
    }
    test &operator--()
    {//前自减
        num1--;
        num2--;
        /*返回自增后的值*/
        return *this;
    }

    test &operator++(int)
    {//后自增
        static test buf;//存放自增前的变量

        buf.num1 = num1++;
        buf.num2 = num2++;
        /*返回自增后的值*/
        return buf;//返回自增前的变量
    }
    test &operator--(int)
    {//后自减
        static test buf;//存放自增前的变量

        buf.num1 = num1--;
        buf.num2 = num2--;
        /*返回自增后的值*/
        return buf;//返回自增前的变量
    }

    // -
    test &operator-()
    {
        num1=-num1;
        num2=-num2;
        return *this;
    }

    // -
    test operator-(test a)
    {
        test buf;
        buf.num1 = num1 - a.num1;
        buf.num2 = num2 - a.num2;
        return buf;
    }

    // *
    test operator*(test a)
    {
        test buf;
        buf.num1 = num1 *a.num1;
        buf.num2 = num2 *a.num2;
        return buf;
    }

    // /
    test operator/(test a)
    {
        test buf;
        buf.num1 = num1 /a.num1;
        buf.num2 = num2 /a.num2;
        return buf;
    }

    // %
    test operator%(test a)
    {
        test buf;
        buf.num1 = num1 %a.num1;
        buf.num2 = num2 %a.num2;
        return buf;
    }

    // +
    test operator+(test a)
    {
        test buf;
        buf.num1 = num1 +a.num1;
        buf.num2 = num2 +a.num2;
        return buf;
    }

    // <
    bool operator<(test a)
    {
        if(num1<a.num1) return true;
        else return false;
    }

    // <=
    bool operator<=(test a)
    {
        if(num1<=a.num1) return true;
        else return false;
    }

    // >
    bool operator>(test a)
    {
        if(num1>a.num1) return true;
        else return false;
    }

    // >=
    bool operator>=(test a)
    {
        if(num1>=a.num1) return true;
        else return false;
    }

    // ==
    bool operator==(test a)
    {
        if(num1==a.num1) return true;
        else return false;
    }

    // !=
    bool operator!=(test a)
    {
        if(num1!=a.num1) return true;
        else return false;
    }

    // &&
    bool operator&&(test a)
    {
        if(num1&&a.num1) return true;
        else return false;
    }

    // ||
    bool operator||(test a)
    {
        if(num1||a.num1) return true;
        else return false;
    }

    // =
    test operator=(test a)
    {
        num1 = a.num1;
        num2 = a.num2;
        return *this;
    }

    // +=
    test operator+=(test a)
    {
        num1 += a.num1;
        num2 += a.num2;
        return *this;
    }

    // -=
    test operator-=(test a)
    {
        num1 -= a.num1;
        num2 -= a.num2;
        return *this;
    }

    // *=
    test operator*=(test a)
    {
        num1 *= a.num1;
        num2 *= a.num2;
        return *this;
    }

    // /=
    test operator/=(test a)
    {
        num1 /= a.num1;
        num2 /= a.num2;
        return *this;
    }

    // %=
    test operator%=(test a)
    {
        num1 %= a.num1;
        num2 %= a.num2;
        return *this;
    }

};
int main()
{
    return 0;
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值