c++ 友元函数

01_point.cpp

#include<iostream>
#include<math.h>
using namespace std;

class Point{
    double x,y;
    public:
    Point()=default;
    Point(double rx ,double ry)
        :x(rx),y(ry){}
    ~Point(){}
    double get_x(){return x;}
    double get_y(){return y;}
friend double get_p2p(Point p1,Point p2);
};
double get_p2p(Point p1,Point p2)
{
    double lx=p1.x-p2.x;
    double ly=p1.y-p2.y;
    double line = sqrt(lx*lx + ly*ly);
    return line;
}


int main()
{
    Point p1(10,10);
    Point p2(110,210);
    cout<<get_p2p(p1,p2)<<endl;
    return 0;
}

02_cycle.cpp

#include<iostream>
#include<math.h>
using namespace std;
class Cycle;
class Point{
    double x,y;
    public:
    Point()=default;
    Point(double rx ,double ry)
        :x(rx),y(ry){}
    ~Point(){}
    double get_x(){return x;}
    double get_y(){return y;}
friend double get_p2p(Point p1,Point p2);
friend double get_p2c(Point p, Cycle c);

};
class Cycle{
    double x,y,r;
    public:
    Cycle()=default;
    Cycle(double rx ,double ry, double rr):x(rx),y(ry),r(rr){}
    ~Cycle(){}
friend double get_p2c(Point p, Cycle c);
};
double get_p2p(Point p1,Point p2)
{
    double lx=p1.x-p2.x;
    double ly=p1.y-p2.y;
    double line = sqrt(lx*lx + ly*ly);
    return line;
}

double get_p2c(Point p, Cycle c)
{
    double lx=p.x-c.x;
    double ly=p.y-c.y;
    double line = sqrt(lx*lx + ly*ly);

    return fabs(line -c.r);
}

int main()
{
    Point p1(10,10);
    Cycle c1(110,210,500);
    cout<<get_p2c(p1,c1)<<endl;
    return 0;
}

03_point_cycle.cpp

#include<iostream>
#include<math.h>
using namespace std;
class Cycle;
class Point{
    double x,y;
    public:
    Point()=default;
    Point(double rx ,double ry)
        :x(rx),y(ry){}
    ~Point(){}
    double get_x(){return x;}
    double get_y(){return y;}
friend double get_p2p(Point p1,Point p2);
friend double get_p2c(Point p, Cycle c);
    friend class Cycle;//友元类
};
class Cycle{
    Point cp;
    double /*x,y,*/r;
    public:
    Cycle()=default;
    Cycle(double rx ,double ry, double rr):/*(cp.x)(rx),(cp.y)(ry),*/r(rr){
    cp.x=rx;cp.y=ry;
    }
    ~Cycle(){}
friend double get_p2c(Point p, Cycle c);
};
double get_p2p(Point p1,Point p2)
{
    double lx=p1.x-p2.x;
    double ly=p1.y-p2.y;
    double line = sqrt(lx*lx + ly*ly);
    return line;
}

double get_p2c(Point p, Cycle c)
{
    double lx=p.x-c.cp.x;
    double ly=p.y-c.cp.y;
    double line = sqrt(lx*lx + ly*ly);

    return fabs(line -c.r);
}

int main()
{
    Point p1(10,10);
//  cout<<p1.x<<endl;
    Cycle c1(110,210,500);
//  cout<<c1.cp.x<<endl;
    cout<<get_p2c(p1,c1)<<endl;
    return 0;
}

04_student_teacher.cpp

#include<iostream>
using namespace std;

struct Teacher;
struct Student{
    int id;string name;int age;
    Student()=default;
    Student(int i,string n,int a,int s)
    :id(i),name(n),age(a),score(s){}
    Student(Student &s){
        id=s.id;name=s.name;age=s.age;}
    ~Student(){}
    Student * next=nullptr;
    friend struct Teacher;
    private:
    int score;
};
struct Teacher{
    int id;string name;
    Teacher()=default;
    Teacher(int i, string n):id(i),name(n){}
    ~Teacher(){}
    void modify_stu_score(Student &s,int score)
    {
        s.score = score;
    }
    void show_stu_score(Student &s)
    {
        cout<<s.score<<endl;
    }
};
int main()
{
    Student s(1001,"lisi",20,59);
    Teacher t(8000,"wangming");
    t.modify_stu_score(s,60);
    t.show_stu_score(s);
}

05_teacher_friend_function.cpp

#include<iostream>
using namespace std;

struct Student;
struct Teacher{
    int id;string name;
    Teacher()=default;
    Teacher(int i, string n):id(i),name(n){}
    ~Teacher(){}
    void modify_stu_score(Student &s,int score);
    void show_stu_score(Student &s);
    void modify_stu_id(Student &s,int id);
};
struct Student{
    string name;int age;
    Student()=default;
    Student(int i,string n,int a,int s)
    :id(i),name(n),age(a),score(s){}
    Student(Student &s){
        id=s.id;name=s.name;age=s.age;}
    ~Student(){}
    Student * next=nullptr;
    friend void Teacher::modify_stu_score(Student &s,int score);
    friend void Teacher::show_stu_score(Student &s);
    private:
    int score;
    int id;
};
void Teacher::modify_stu_score(Student &s,int score)
{
    s.score = score;
}
void Teacher::show_stu_score(Student &s)
{
    cout<<s.score<<endl;
}
void Teahcer::modify_stu_id(Student &s,int id)
{
    s.id = id;
}
int main()
{
    Student s(1001,"lisi",20,59);
    Teacher t(8000,"wangming");
    t.modify_stu_score(s,60);
    t.show_stu_score(s);
}
原文地址:https://www.cnblogs.com/Sico2Sico/p/5384252.html