【1】外部函数访问类内成员
#include
using namespace std;
class person
{
public:
int age;
person(){};
person(int age,int height,string name);
protected:
int height;
private:
string name;
};
//构造函数
person::person(int age,int height,string name)
{
this->age = age;
this->height = height;
this->name = name;
}
//类外的函数
void disp_info(person& pn)
{
cout << "age = " << pn.age << endl; //public的,类外可以访问
cout << "height = " << pn.height << endl; //protected的,类外不可以访问
cout << "name = " << pn.name << endl; //private的,类外不可以访问
}
int main(int argc, char const *argv[])
{
person p1(10,155,"jime");
disp_info(p1);
return 0;
}
【2】友元函数的引入
class person
{
public:
int age;
person(){};
person(int age,int height,string name);
//声明外部函数为类的友元函数
friend void disp_info(person& pn);
protected:
int height;
private:
string name;
};
【3】总结
【1】友元函数的2种实现
//- 友元函数为另一个类中的成员函数的实现代码
#include
using namespace std;
class person; //类的前置声明
class animal
{
public:
void eat(person& pn); //这里只用到一个person类的引用,不涉及person里面的具体细节,所以能编译通过
};
class person
{
public:
int age;
person(){};
person(int age,int height,string name);
friend void animal::eat(person& pn);
protected:
int height;
private:
string name;
};
//构造函数
person::person(int age,int height,string name)
{
this->age = age;
this->height = height;
this->name = name;
}
void animal::eat(person& pn)
{
cout << "animal eat a person nameed" << pn.name << endl;
cout << "animal eat a person age = " << pn.age << endl;
cout << "animal eat a person height = " << pn.height << endl;
}
int main(int argc, char const *argv[])
{
person p1(10,155,"jime");
animal a1;
a1.eat(p1);
return 0;
}
【2】类的前置声明
class animal; //类的前置声明
class person
{
public:
int age;
person(){};
person(int age,int height,string name);
friend void animal::eat(person& pn); //编译报错,不能通过animal类的前置声明就调用animal里面的eat()成员
protected:
int height;
private:
string name;
};
class animal
{
public:
void eat(person& pn);
};
class person; //类的前置声明
class animal
{
public:
void eat(person& pn); //这里只用到一个person类的引用,不涉及person里面的具体细节,所以能编译通过
//person p1; //不可以
person *p2; //定义成指针和引用都可以
};
class person
{
public:
int age;
person(){};
person(int age,int height,string name);
friend void animal::eat(person& pn);
protected:
int height;
private:
string name;
};
【1】友元类的概念和使用
#include
using namespace std;
class person; //类的前置声明
class animal
{
public:
void eat(person& pn);
void love(person& pn);
};
class person
{
public:
int age;
person(){};
person(int age,int height,string name);
friend class animal; //将animal整个类声明为person类的友元类
protected:
int height;
private:
string name;
};
//构造函数
person::person(int age,int height,string name)
{
this->age = age;
this->height = height;
this->name = name;
}
void animal::eat(person& pn)
{
cout << "animal eat a person nameed" << pn.name << endl;
cout << "animal eat a person age = " << pn.age << endl;
cout << "animal eat a person height = " << pn.height << endl;
cout << endl;
}
void animal::love(person& pn)
{
cout << "animal love a person nameed" << pn.name << endl;
cout << "animal love a person age = " << pn.age << endl;
cout << "animal love a person height = " << pn.height << endl;
}
int main(int argc, char const *argv[])
{
person p1(10,155,"jime");
animal a1;
a1.eat(p1);
a1.love(p1);
return 0;
}
【2】友元类总结
【1】使用友元函数的优缺点
【2】使用友元函数的两种情况
【3】运算符重载中使用友元
#include
using namespace std;
class coordinate
{
public:
int x; // x轴坐标
int y; // y轴坐标
coordinate();
coordinate(int x0, int y0);
// 拷贝构造函数
coordinate(const coordinate& rhs);
void print(void);
friend coordinate operator+(const coordinate& a, const coordinate& b);
};
coordinate::coordinate()
{
x = 0;
y = 0;
};
coordinate::coordinate(int x0, int y0)
{
x = x0;
y = y0;
};
// 拷贝构造函数
coordinate::coordinate(const coordinate& rhs)
{
cout << "---copy construtor---" << endl;
this->x = rhs.x;
this->y = rhs.y;
}
void coordinate::print(void)
{
cout << "(" << this->x << ", " << this->y << ")" << endl;
}
// 用独立函数来实现+的运算符重载
coordinate operator+(const coordinate& a, const coordinate& b)
{
coordinate tmp;
tmp.x = a.x + b.x;
tmp.y = a.y + b.y;
return tmp;
}
int main(void)
{
coordinate a(1, 3);
coordinate b(2, 4);
coordinate c;
c = a + b; // a+b 就相当于是 c = operator+(a, b);
// c = b + a; // 相当于 c = operator+(b, a);
a.print();
b.print();
c.print();
return 0;
}
【4】两个类如何共享数据
【5】友元函数和类的成员函数的区别
【6】共有友元函数