c++友元函数类的使用

参考:

复制代码
#include <iostream>
using namespace std;

class Complex{
    friend Complex operator+(Complex &c1, Complex &c2); //声明友元函数
    public:
    Complex(double r=0, double i=0):real(r),image(i){}
    void display(){
        cout<<"("<<real<<","<<image<<")"<<endl;
    }
private:

    double real;
    double image;
};



Complex operator+(Complex &c1, Complex &c2){
    Complex c;
    c.real = c1.real + c2.real; //访问私有的成员变量,不被允许 //声明友元函数后可以访问私有成员变量
    c.image = c1.image + c2.image; //访问私有的成员变量,不被允许

    return c;
}

int main()
{

    Complex c1(2,3), c2(4,5);
    Complex c3(0,0);
    c3 = c1 + c2;
    c3.display();
    return 0;
}
复制代码

全局友元:

复制代码
#include <iostream>
#include <cmath>

using namespace std;

class Point{
    //声明友元函数
    friend float distance(const Point &p1,const Point &p2);
private:
    float _x, _y;
public:
    Point(float x=0, float y=0):_x(x),_y(y){};
    void display(){
        cout<<"("<<_x<<","<<_y<<")"<<endl;
    }

};

//距离计算函数
float distance(const Point &p1,const Point &p2){
    float dx = p1._x - p2._x;
    float dy = p1._y - p2._y;
    //计算两点之间的距离
    return sqrt(dx*dx + dy*dy);

}

int main(){

    Point p1(3,4);
    p1.display();
    Point p2(7,8);
    p2.display();
    float d = distance(p1,p2);
    cout<<"距离 p1 --- p2  "<<d<<endl;
    return 0;

}
复制代码

成员函数友元:

复制代码
#include <iostream>
#include <cmath>
using namespace std;


//!前向声明 是一种不完全类型的声明,不能定义对象,
//! 但可以定义指针和引用,因为指针和引用都是4字节,都是确定的大小
//!作为参数和返回值,仅用在函数声明中

class Point; //前向声明

//成员函数作友元
class ManagerPoint {
public:
    //距离计算函数
    //由于用到的Point,还没定义,结合前向声明的特点,
    //在此处只能声明函数,,这个函数的实现只能放在Point函数实现之后了
    float distance(const Point &p1, const Point &p2);
};

class Point{
    //声明友元函数
    friend float ManagerPoint::distance(const Point &p1,const Point &p2);
private:
    float _x, _y;
public:
    Point(float x=0, float y=0):_x(x),_y(y){};
    void display(){
        cout<<"("<<_x<<","<<_y<<")"<<endl;
    }

};


//距离计算函数
float ManagerPoint::distance(const Point &p1, const Point &p2) {
    float dx = p1._x - p2._x;
    float dy = p1._y - p2._y;
    //计算两点之间的距离
    return sqrt(dx * dx + dy * dy);

}




int main(){

    Point p1(3,4);
    p1.display();
    Point p2(7,8);
    p2.display();

    
    ManagerPoint mp;
    float d = mp.distance(p1,p2);
    cout<<"距离 p1 --- p2  "<<d<<endl;
    return 0;

}
复制代码

友元类:

复制代码
#include <iostream>
#include <cmath>
using namespace std;


class Point{
    //声明友元类
    friend class ManagerPoint;
private:
    float _x, _y;
public:
    Point(float x=0, float y=0):_x(x),_y(y){};
    void display(){
        cout<<"("<<_x<<","<<_y<<")"<<endl;
    }

};


class ManagerPoint {
public:
    //距离计算函数
    //由于用到的Point,还没定义,结合前向声明的特点,
    //在此处只能声明函数,,这个函数的实现只能放在Point函数实现之后了
    float distance(const Point &p1, const Point &p2);
};

//距离计算函数
float ManagerPoint::distance(const Point &p1, const Point &p2) {
    float dx = p1._x - p2._x;
    float dy = p1._y - p2._y;
    //计算两点之间的距离
    return sqrt(dx * dx + dy * dy);

}

int main(){

    Point p1(3,4);
    p1.display();
    Point p2(7,8);
    p2.display();

    //使用友元类
    ManagerPoint mp;
    float d = mp.distance(p1,p2);
    cout<<"距离 p1 --- p2  "<<d<<endl;
    return 0;

}
复制代码

 

https://blog.csdn.net/gopher9511/article/details/140841144

posted @   txwtech  阅读(12)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
点击右上角即可分享
微信分享提示