1.友元函数
1.为什么要引入友元函数:
在现实类之间数据共享时,减少系统开销,提高效率。
也就是说:为了使其他类的成员函数直接访问该类的私有成员变量
缺点:友元函数破坏了封装机制,尽量不使用成员函数,除非不得已的情况下才使用友元函数。
2.什么时候使用友元函数:
1)运算符重载的某些场合需要使用友元。
2)两个类要共享数据的时候
3.友元函数的使用:
友元函数的参数:
1.要访问非static成员时,需要对象做参数;
2.要访问static成员或全局变量时,则不需要对象做参数
3.如果做参数的对象时全局对象,则不需要对象做参数
友元函数的位置:
因为友元函数是类外的函数,所以它的声明可以放在类的私有段或公有段且没有区别。
4.友元函数的分类:友元全局函数、友元成员函数
/**********************************************/
1.友元全局函数
2.友元成员函数
/**********************************************/
1.友元全局函数
#ifndef TIME_H
#define TIME_H
/*Time.h*/
#include<iostream>
#include"Match.h"
using namespace std;
class Time
{
//声明友元函数
friend void printTime(Time &t);
public:
Time(int hour,int min,int sec);
private:
int m_iHour;
int m_iMinute;
int m_iSecond;
};
#endif
/*Time.cpp*/
#include"Time.h"
Time::Time(int hour,int min ,int sec)
{
m_iHour = hour;
m_iMinute = min;
m_iSecond = sec;
}
/*demo.cpp*/
#include<iostream>
#include"Time.h"
#include"Match.h"
using namespace std;
//函数声明
void printTime(Time &t);
int main(void)
{
Time t(6,35,24);
printTime(t);
system("pause");
return 0;
}
//函数定义
void printTime(Time &t)
{
cout << t.m_iHour << ":" << t.m_iMinute << ":" << t.m_iSecond << endl;
}
#ifndef TIME_H
#define TIME_H
/*Time.h*/
#include<iostream>
#include"Match.h"
using namespace std;
class Time
{
//声明友元函数
friend void Match::printTime(Time &t);
public:
Time(int hour,int min,int sec);
private:
int m_iHour;
int m_iMinute;
int m_iSecond;
};
#endif
/*Time.cpp*/
#include"Time.h"
Time::Time(int hour,int min ,int sec)
{
m_iHour = hour;
m_iMinute = min;
m_iSecond = sec;
}
/*Match.c*/
#ifndef STUDENT_H
#define STUDENT_H
class Time;
class Match
{
public:
void printTime(Time &t);
};
#endif
/*Match.cpp*/
#include<iostream>
#include"Match.h"
#include"Time.h"
using namespace std;
void Match::printTime(Time &t)
{
cout << t.m_iHour << ":" << t.m_iMinute << ":" << t.m_iSecond << endl;
}
/*demo.cpp*/
#include<iostream>
#include"Time.h"
#include"Match.h"
using namespace std;
int main(void)
{
Time t(6,35,24);
Match m;
m.printTime(t);
system("pause");
return 0;
}
运行结果:
2.友元类
/*********************************************************************************/
友元类:声明为友元类之后,一个类就可以使用另一个类的私有成员
/*********************************************************************************/
/*Time.h*/
#ifndef TIME_H
#define TIME_H
//只是先告诉机器有这个类
class Match;
class Time
{
//声明友元类
friend Match;
public:
Time(int hour,int min,int sec);
private:
void printTime();
int m_iHour;
int m_iMinute;
int m_iSecond;
};
#endif
/*Time.cpp*/
#include"Time.h"
#include<iostream>
using namespace std;
Time::Time(int hour,int min ,int sec)
{
m_iHour = hour;
m_iMinute = min;
m_iSecond = sec;
}
void Time::printTime()
{
cout << m_iHour << "时" << m_iMinute << "分" << m_iSecond <<"秒"<< endl;
}
/*Match.c*/
#ifndef STUDENT_H
#define STUDENT_H
#include"Time.h"
class Match
{
public:
Match(int hour,int min,int sec);
void testTime();
private:
Time m_tTime; //申请一个Time类型的私有成员变量
};
#endif
/*Match.cpp*/
#include<iostream>
#include"Match.h"
using namespace std;
Match::Match(int hour,int min,int sec):m_tTime(hour ,min,sec)
{
//初始化列表,通过初始化Match来初始化m_tTime的三个参数
}
void Match::testTime()
{
m_tTime.printTime(); //证明了Match类的成员可以访问Time的私有成员(因为Match是Time的友元类)
cout << m_tTime.m_iHour << ":" << m_tTime.m_iMinute << ":" << m_tTime.m_iSecond << endl;
}
/*demo.cpp*/
#include<iostream>
#include"Time.h"
#include"Match.h"
#include<stdlib.h>
using namespace std;
int main(void)
{
Match m(6, 35, 24);
m.testTime();
system("pause");
return 0;
}
运行结果如下:
6时35分24秒是调用Time的私有成员函数printTime()输出的,而6:35:24是调用Time的私有数据成员直接输出的。由此可以体会到友元类的作用。