c++友元

作者: arkliu | 来源:发表于2022-11-15 07:39 被阅读0次

    友元函数

    #include <iostream>
    #include <math.h>
    
    using namespace std;
    
    class Point{
        private:
            int m_x;
            int m_y;
        public:
            Point(int x, int y) {
                m_x = x;
                m_x = y;
            }
            int getX() {
                return m_x;
            }
            int getY() {
                return m_y;
            }
            // 声明getDistance2是类Point的友元函数,因此在getDistance2里可以直接通过对象访问其私有成员变量
            friend void getDistance2(Point& p1, Point& p2);
    };
    
    void getDistance(Point& p1, Point& p2) {
        double dis;
        int x_dis = p1.getX() - p2.getX();
        int y_dis = p2.getY() - p2.getY();
        dis = sqrt(x_dis * x_dis + y_dis * y_dis);
        cout<<"getDistance dis = "<<dis<<endl;
    }
    
    void getDistance2(Point& p1, Point& p2) {
        double dis;
        int x_dis = p1.m_x - p2.m_x;
        int y_dis = p2.m_y - p2.m_y;
        dis = sqrt(x_dis * x_dis + y_dis * y_dis);
        cout<<"getDistance2 dis = "<<dis<<endl;
    }
    
    int main() {
        Point p1(2,3);
        Point p2(3,4);
        getDistance(p1, p2);
        getDistance2(p1, p2);
        return 0;
    }
    
    #include <iostream>
    #include <math.h>
    
    using namespace std;
    
    class Point;
    
    class PointManager {
        public:
            void getDistance(Point& p1, Point& p2);
    };
    
    class Point{
        private:
            int m_x;
            int m_y;
        public:
            Point(int x, int y) {
                m_x = x;
                m_x = y;
            }
            int getX() {
                return m_x;
            }
            int getY() {
                return m_y;
            }
            friend void PointManager::getDistance(Point& p1, Point& p2);
    };
    
    void PointManager::getDistance(Point& p1, Point& p2) {
        double dis;
        int x_dis = p1.m_x - p2.m_x;
        int y_dis = p2.m_y - p2.m_y;
        dis = sqrt(x_dis * x_dis + y_dis * y_dis);
        cout<<"getDistance dis = "<<dis<<endl;
    }
    
    int main() {
        Point p1(2,3);
        Point p2(3,4);
        PointManager pm;
        pm.getDistance(p1, p2);
        return 0;
    }
    

    友元类

    #include <iostream>
    
    using namespace std;
    
    class ClassA {
        private:
            int m_a;
        public:
            ClassA(int a) {
                m_a = a;
            }
            friend class ClassB; //声明ClassB为当前类的友元类后,在ClassB中就可以通过ClassA的对象,直接访问其私有成员
    };
    
    class ClassB {
        private:
            int m_b;
        public:
            ClassB(int b) {
                m_b = b;
            }
            void printB() {
                ClassA clazzA(22);
                cout << "m_b = " << m_b << "  clazzA.m_a = "<<clazzA.m_a<<endl;
            }
    };
    
    int main() {
        ClassB b(44);
        b.printB();
        return 0;
    }
    

    相关文章

      网友评论

          本文标题:c++友元

          本文链接:https://www.haomeiwen.com/subject/ammrtdtx.html