友元可以是一个函数,该函数被称为 友元函数 ;友元也可以是一个类,该类被称为 友元类 。 友元可以理解为 原类 的 朋友,友元(友元函数和友元类)不是原类的成员, 友元可以访问 原类 的所有成员 。
【 1. 友元函数 】
类的 友元函数定义在类外部,但有权访问类的所有私有(private)成员和保护(protected)成员,即可以访问类的所有成员 。 友元函数并不是成员函数,但是需要在原类的定义中用 关键字 friend 声明 。如下所示:
class Box
{
double width;
public :
double length;
friend void printWidth ( Box box ) ;
void setWidth ( double wid ) ;
} ;
# include <iostream>
using namespace std;
class Box
{
double width;
public :
friend void printWidth ( Box box ) ;
void setWidth ( double wid ) ;
} ;
void Box :: setWidth ( double wid )
{
width = wid;
}
void printWidth ( Box box )
{
cout << "Width of box : " << box. width << endl;
}
int main ( )
{
Box box;
box. setWidth ( 10.0 ) ;
printWidth ( box ) ;
return 0 ;
}
【 2. 友元类 】
整个友元类及其所有成员都是友元,友元类中的成员可以访问原类中的所有成员 。友元类不是成员,但是需要 在 原类 定义的内部用关键字 friend class 进行声明 ,如下所示:
class CStudents
{
private :
char ID[ 100 ] ;
char Name[ 100 ] ;
double Score;
public :
CStudents ( const char * , const char * , double ) ;
CStudents ( ) ;
friend void Display1 ( CStudents& ) ;
friend class CTeacher ;
} ;
# pragma once
# include <iostream>
using namespace std;
class CStudents
{
private :
char ID[ 100 ] ;
char Name[ 100 ] ;
double Score;
public :
CStudents ( const char * , const char * , double ) ;
CStudents ( ) ;
friend void Display1 ( CStudents& ) ;
friend class CTeacher ;
} ;
class CTeacher
{
public :
char * GetName ( CStudents& Temp)
{
return Temp. Name;
}
void Modify ( CStudents& Temp)
{
cin >> Temp. Score;
}
void Display2 ( CStudents& Temp) const
{
double x = Temp. Score;
if ( x < 60 ) cout << "不及格" << endl;
else if ( x >= 60 && x <= 69 ) cout << "及格" << endl;
else if ( x >= 70 && x <= 79 ) cout << "中等" << endl;
else if ( x >= 80 && x <= 89 ) cout << "良好" << endl;
else cout << "优秀" << endl;
}
} ;
# include "CStudents.h"
# include <iostream>
# include <string>
# include <iomanip>
using namespace std;
CStudents :: CStudents ( ) : ID ( "1024" ) , Name ( "MR_Promethus" ) , Score ( 100 )
{
} ;
CStudents :: CStudents ( const char * MyId, const char * MyName, double MyScore)
{
strcpy_s ( ID, MyId) ;
strcpy_s ( Name, MyName) ;
Score = MyScore;
} ;
void Display1 ( CStudents& Temp)
{
double x = Temp. Score;
if ( x < 60 ) cout << Temp. Name << "\t" << Temp. Score << "\t" << "不及格" << "\t" << endl;
else if ( x >= 60 && x <= 69 ) cout << Temp. Name << "\t" << Temp. Score << "\t" << "及格" << "\t" << endl;
else if ( x >= 70 && x <= 79 ) cout << Temp. Name << "\t" << Temp. Score << "\t" << "中等" << "\t" << endl;
else if ( x >= 80 && x <= 89 ) cout << Temp. Name << "\t" << Temp. Score << "\t" << "良好" << "\t" << endl;
else cout << Temp. Name << "\t" << Temp. Score << "\t" << "优秀" << "\t" << endl;
}
# include <iostream>
# include "CStudents.h"
# include <cstring>
using namespace std;
int main ( void )
{
int i;
char temp[ 100 ] ;
int flag = 0 ;
int cunzai;
int j;
CTeacher teach;
CStudents s[ 4 ] =
{
{ "01" , "LeBron Jams" , 90 } ,
{ "02" , "Michael Jordan" , 98 } ,
{ "03" , "Chris Bosh" , 95 } ,
{ "04" , "Stephen Curry" , 96 }
} ;
for ( i = 0 ; i < 4 ; ++ i)
{
Display1 ( s[ i] ) ;
}
cout << "请输入要修改成绩的姓名" << endl;
cin. getline ( temp, 20 ) ;
for ( i = 0 ; i < 4 ; ++ i)
{
flag = 0 ;
for ( j= 0 ; temp[ j] != teach. GetName ( s[ i] ) [ j] ; ++ j)
{
flag = 1 ;
}
if ( flag == 0 ) { cunzai = i; break ; } ;
}
if ( flag== 0 )
{
cout << "请输入成绩:" ;
teach. Modify ( s[ cunzai] ) ;
teach. Display2 ( s[ cunzai] ) ;
}
if ( flag== 1 )
cout << "此人不存在" << endl;
return 0 ;
}