06.友元类友元函数和运算符重载

(创建于2017/12/24)

友元函数

友元函数提供了一种外部访问类私有属性的方法,如下,如果modifyA方法不添加friend,则编译失败

 #define  _CRT_SECURE_NO_WARNINGS
#include "iostream"  //包含c++的头文件
using namespace std;

class B {
public:
    B(int a) {
        this->a = a;
    }
private:
    int a = 10;

    friend void modifyA(B*b);
};
void modifyA(B *b) {
    b->a = 20;
}
int main() {
    B b(10);
    system("pause");
}
友元类
 #define  _CRT_SECURE_NO_WARNINGS
#include "iostream"  //包含c++的头文件
using namespace std;
class A {
    friend class B;
private:
    int x;
public :
    void display() {
        cout << "x"<<x<< endl;
    }
};
class B {
private:
    A aObj;
public:
    void set(int i) {
        aObj.x = i;
    }

    void get() {
        aObj.display();
    }
};

int main() {
    B bObj;
    bObj.set(100);
    bObj.get();
    system("pause");
}

#define _CRT_SECURE_NO_WARNINGS
#include<iostream>   //这个头文件和命名空间有一个不存在 cout就无法使用
using namespace std;

class A {
private:
    int i;
public:
    A(int i) {
        this->i = i;
    }
    void myprint() {
        cout << i << endl;
    }
    //友元函数
    friend void modify_i(A *a, int i);
};

//友元函数的实现,再友元函数中可以访问私有的属性
void modify_i(A *a, int b) {
    a->i = b;
}


//友元类
class B {
    friend class C;
private:
    int i;
};
//C这个友元类可以访问B的任何属性
class C {
private:
    B b;
public:
    C() {

    }
    void setAValue() {
        b.i = 20;
        cout << b.i << endl;
    }
};


void main() {
    A* a = new A(10);
    a->myprint();

    modify_i(a,12);
    a->myprint();

    cout << "----------------------------------------------" << endl;

    C *c = new C();
    c->setAValue();
    system("pause");
}
运算符重载
#define  _CRT_SECURE_NO_WARNINGS
#include "iostream"  //包含c++的头文件
using namespace std;
class A {
public:
    A(int a, int b) {

    }
public:
    int x;
};
A operator+(A &a1, A &a2) {
    return A(a1.x, a2.x);
}
int main() {
    A a1(1,2);
    A a2(3,4);
    A a3 = a1 + a2;
    system("pause");
}
运算符重载的两种方式和友元函数结合
 #define  _CRT_SECURE_NO_WARNINGS
#include "iostream"  //包含c++的头文件
using namespace std;
class A {
public:
    A(int a, int b) {

    }
    //实现运算符重载的两种方式之二:成员函数写在类的内部
    A operator-(A&a) {
        return A(this->x,a.x);
    }

    friend A operator+(A &a1, A &a2);
private:
    int x;
};

//实现运算符重载的两种方式之一:全局函数写在类的外边
//当x属性定义为私有的时候,在函数外部无法访问,此时,需要
//将重载后的函数定义为类的友元函数方可
A operator+(A &a1, A &a2) {
    return A(a1.x, a2.x);
}
int main() {
    A a1(1,2);
    A a2(3,4);
    A a3 = a1 + a2;
    system("pause");
}

一元运算符重载(++/--)

一元运算符--和++分前置和后置两种,虽然是同一个运算符但是放置的位置还会影响运算的逻辑,那么对这样一个运算符做重载怎么办,怎么区分是前置还是后置,这就需要在operator++(形参列表)形参列表中增加一个占位作为区别,当运算符是前置的时候,做一般处理,如A& operator--(A &a),传入一个引用,因为是前置,所以先运算再赋值,然后返回这个对象的引用;而当运算符后置的时候,则在形参中增加一个int,用来告诉编译器这个运算符是后置的
friend A operator++(A &a, int),由于后置运算的逻辑,是先返回本身,然后做++或--操作,所以下边的代码是先用一个对象接收这个引用,然后将引用++ --,返回的还是++ -- 之前的那个对象

 #define  _CRT_SECURE_NO_WARNINGS
#include "iostream"
using namespace std;
class A {
public:
    A(int a, int b) {
        this->x = a;
        this->y = b;
    }
    void printA() {
        cout <<"x="<<x<<" y="<<y << endl;
    }
    //前置运算
    friend A& operator--(A &a);
    friend A& operator++(A &a);

    //后置运算
    friend A operator--(A &a, int);
    friend A operator++(A &a, int);
private:
    int x;
    int y;
};
//一元运算符重载(前置)
A& operator--(A &a) {
    --a.x;
    --a.y;
    return a;
}

A& operator++(A &a) {
    ++a.x;
    ++a.y;
    return a;
}

//一元运算符重载(后置)
A operator--(A &a, int) {
    A temp = a;
    a.x--;
    a.y--;
    return temp;
}

A operator++(A &a, int) {
    A temp = a;
    a.x++;
    a.y++;
    return temp;
}

int main() {
    //创建两个对象a a2
    A a(1, 2), a2(3,4);

    A temp1 = --a;
    temp1.printA();

    A temp2 = ++a2;
    temp2.printA();

    //创建两个对象a2 a3
    A a3(5, 6), a4(7, 8);

    A temp3 = a3--;
    temp3.printA();

    A temp4 = a4--;
    temp4.printA();
    system("pause");
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值