友元和运算符重载 (c++)

1.前言

类实现了数据的隐藏与封装,类的数据成员一般定义为私有成员,仅能通过类的成员函数才能读写。如果数据成员定义为公共的,则又破坏了封装性。但是某些情况下,需要频繁读写类的数据成员,特别是在对某些成员函数多次调用时,由于参数传递、类型检查和安全性检查等都需要时间开销,而影响程序的运行效率。

友元主要是用于运算符重载

2.友元函数

定义:定义在类外部的普通函数,需要在类中用friend关键字进行说明。就可以访问类中所有的成员

注意事项

  1. 函数参数是对象引用。
  2. 不是类内函数,不受类内权限修饰符限制。友元函数的声明可以写在类的任意位置
#include <iostream>
using namespace std;
class Person{
private:
    string phoneNum;
    string ID;
public:
    Person(string phoneNum,string ID){
        this->phoneNum=phoneNum;
        this->ID=ID;
    }
    friend void test(Person& p);//声明了Test是Person类的友元函数
};
void test(Person& p){
    cout<<"身份证号:"<<p.ID<<" 电话号码:"<<p.phoneNum<<endl;
}
int main()
{
    Person p1("180XX888","3729XXX2020");
    test(p1);

}

3.友元类

(1)当类B是类A的友元类时,类B就可以访问类A中所有的成员

#include <iostream>
using namespace std;
class A{
private:
    int value;
public:
    A(int v):value(v){}
    friend class B;
};
class B{
public:
    void test(A& t){
        cout<<t.value<<endl;
    }
};
int main()
{
    A aa(100);
    B bb;
    bb.test(aa);
}

友元关系的特点:

1.友元关系是单向的  2.友元关系不可传递  3. 友元关系不可继承 、

4.运算符重载

定义:在C++中,运算符的操作对象只能是基本数据类型。自定义类型是不能直接使用,所有运算重载对已有的运算符赋予多重含义。使同一个运算符作用于不同类型数据做出不同的行为

例如:& 取地址也可以表示引用   << 位运算也可以配合cout使用

特点:

1.运算符重载的本质是函数重载,也是C++多态的一种体现

2.运算符重载增强了C++的可扩充性,使得C++代码更加直观、易读

C++提供的运算符重载机制,重载运算符是具有特殊名字的函数:它们的名字由关键字operator和其后要重载的运算符共同组成。

加号运算符重载

友元函数的加号运算符重载

友元函数是类外函数,没有隐含this指针。两个参数都需要是对象的引用

#include <iostream>
using namespace std;
class Rice{
private:
    int production; //产量
    int defense;   //抗性
public:
    Rice(int p,int d){
        production=p;
        defense=d;
    }
    void show(){
        cout<<"产量"<<production<<" 抗性"<<defense<<endl;
    }
    friend Rice operator +(Rice& n,Rice& m);

};
Rice operator +(Rice& n,Rice& m){ //返回值是对象类型的
    int p=(n.production+m.production)/2;
    int d=(n.defense+m.defense)/2;
    Rice r(p,d);
    return r;
}

int main()
{
    Rice r1(1000,70);
    Rice r2(2000,30);
    Rice r3=r1+r2;   //相当于Rice r3=operator+(r1,r2);
    r3.show();

}

成员函数的加号运算符重载

成员函数,会有一个隐含的this指针,在qt编译环境下占据第一个参数位

哪个对象调用函数,this指针就指向哪个对象

#include <iostream>
using namespace std;
class Rice{
private:
    int production; //产量
    int defense;   //抗性
public:
    Rice(int p,int d){
        production=p;
        defense=d;
    }
    void show(){
        cout<<"产量"<<production<<" 抗性"<<defense<<endl;
    }
    Rice operator +(Rice& other); //声明
};
//定义
Rice Rice::operator +(Rice& other){
    int pro=(this->production+other.production)/2;
    int def=(this->defense+other.defense)/2;
    Rice r(pro,def);
    return r;
}
int main()
{
    Rice r1(1000,70);
    Rice r2(2000,30);
    Rice r3=r1+r2;   //相当于Rice r3=r1.operator +(r2);
    r3.show();

}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值