15、C++类的友元

C++中的友元

作用:使用友元可以使非成员函数通过形参对象,直接访问对象或者引用的私有成员

,而不需要通过调用公有函数去访问。

友元函数:在该类中声明时以friend修饰,可以是一个普通函数,也可以是一个其

它类的成员函数。定义可以放在类内,或者其它位置。

友元函数可以访问类的私有成员,但是它并不是成员函数,所以不能直接访问对象的

数据成员,也不能通过this指针访问对象的数据成员,,它必须通过入口参数传递进来

的对象名,或者引用,来访问引用对象中的数据成员。

友元类:将A类声明为B类的友元类,那么A类的所有函数都可以通过将B类

的对象或者指针,或者 引用作为入口参数来访问B类中的所有数据成员。

方法:

class B{

friend  class  A;

}

测试实例:

文件 :main.h

#include <iostream>

#include <string.h>

using namespace std;

//一般在.h文件中声明,在同名.cpp文件中定义,然后在需要使用这个类的文件中 include .h文件。

class CComplex;

class C

{

    private :

    int c;

    public :

    double getRCI(const CComplex &com);

    double getnothing(const CComplex &com);

};

class A

{

    private:

    int a;

    public :

    double getRdI(const CComplex & com);

    double getnothing(const CComplex &com);

};

class CComplex

{

  private:

    double real;

    double imag;

  public:

      CComplex(double r,double i);//构造函数

      CComplex(const CComplex  &com);//复制构造函数

      CComplex & operator =(const CComplex & com);//赋值操作符

      ~CComplex();//析构函数

      double getReal() ;

      double getImag() ;

    friend  class A;//声明A类为该类的友元类

    friend double getRplusI(const CComplex &com);

    friend  double C::getRCI(const CComplex &com);//将C类中的某一个成员函数声明为该类的友元。

};

//所有的友元函数不是类的成员,所以它不能直接访问对象的数据成员,也不能通过this指针访问对象的数据成员

//它必须通过作为入口参数传递进来的对象名来访问。或者引用。

//不然一般的函数,是不可以通过对象访问私有成员的,只有友元和成员函数,以及成员函数的对象参数。


文件:mian.cpp

#include <iostream>

#include <string.h>

#include "main.h"

using namespace std;

CComplex::CComplex(double r=1,double i=1):real(r),imag(i)

{

    cout<<"调用类的构造函数!"<<endl;

}

double CComplex::getReal() {

    return real;

}

double CComplex::getImag()

{

    return imag;

}

double getRplusI(const CComplex &com)

{

    return (com.real+com.imag);

}

double A::getRdI(const CComplex & com)

{

    return (com.real-com.imag);

}

double A::getnothing(const CComplex &com)

{

    return com.real;//正确

}

double C::getRCI(const CComplex &com)

{

    return com.real*com.imag;

}

double C::getnothing(const CComplex &com)

{

    //return com.real;//错误

    return 1;

}

CComplex::CComplex(const CComplex &com)

{

    //com.getReal();常对象引用只能调用常成员函数

    real=com.real;

    imag=com.imag;

    cout<<"调用复制构造函数"<<endl;

}

CComplex & CComplex:: operator =(const CComplex & com)

{

     //com.getReal();常对象引用只能调用常成员函数

    real=com.real;

    imag=com.imag;

    cout<<"调用赋值操作函数"<<endl;

    return *this;

}

CComplex::~CComplex()

{

    cout<<"调用类的析构函数!"<<endl;

}

int main()

{

    CComplex com1(20,10);

    A a;

    C c;

    cout<<getRplusI(com1)<<endl;

    cout<<a.getRdI(com1)<<endl;

    cout<<c.getRCI(com1)<<endl;

    cout<<a.getnothing(com1)<<endl;

    cout<<c.getnothing(com1)<<endl;

    return 0;

}


程序输出结果:

调用类的构造函数!

30

10

200

20

1

调用类的析构函数!

Process returned 0 (0x0)   execution time : 0.249 s

Press any key to continue.


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值