友元

一句话总结:要想访问我的保护信息,必须存在于我的Friend列表中。注意头文件不要相互包含


友元类:

//  CFriend.hpp

//  CppProduct

//

//  Created by zhaojunyan on 17/6/17.

//  Copyright © 2017 zhaojunyan. All rights reserved.

//

#ifndef CFriend_hpp

#define CFriend_hpp


#include <stdio.h>

class CPerson;

//CPerson并不在CFriend友元名单里,无法直接访问CFriend的保护信息

class CFriend

{

public:

    CFriend();

    ~CFriend();

    int getPersonSalary(CPerson &Person);

};


#endif /* CFriend_hpp */


//  CFriend.cpp

//  CppProduct

//

//  Created by zhaojunyan on 17/6/17.

//  Copyright © 2017 zhaojunyan. All rights reserved.

//

#include "CFriend.hpp"

#include <iostream>


CFriend::CFriend()

{}


CFriend::~CFriend()

{}


int CFriend::getPersonSalary(CPerson &Person)

{

    int salary = Person.m_salary;

    std::cout<<"salary: "<<salary<<std::endl;

    return salary;

}


//  CPerson.hpp

//  CppProduct

//

//  Created by zhaojunyan on 17/6/17.

//  Copyright © 2017 zhaojunyan. All rights reserved.

//

#ifndef CPerson_hpp

#define CPerson_hpp


#include <stdio.h>

#include <stdio.h>

//#include "CFriend.hpp"


class CPerson

{

public:

    CPerson(int age, int level, int salary);

    ~CPerson();

    int getAge();

    

public:

    int m_age;

    friend class CFriend; //友元类,若想访问我的保护信息,需存在于我的友元名单里

protected:

    int m_level;

private:

    int m_salary;

};


#endif /* CPerson_hpp */



//  CPerson.cpp

//  CppProduct

//

//  Created by zhaojunyan on 17/6/17.

//  Copyright © 2017 zhaojunyan. All rights reserved.

//

#include "CPerson.hpp"


//初始化列表仅与定义顺序有关

CPerson::CPerson(int age, int level, int salary):m_age(age),m_level(level),m_salary(salary)

{}


CPerson::~CPerson()

{}


int CPerson::getAge()

{

    return m_age;

}


//  main.cpp

//  CppProduct

//

//  Created by zhaojunyan on 17/6/17.

//  Copyright © 2017 zhaojunyan. All rights reserved.

//

#include <iostream>

#include "CFriend.hpp"

#include "CPerson.hpp"

using namespace std;

int main(int argc, const char * argv[]) {

    // insert code here...

    CPerson Person(23,4,10000);

    CFriend Friend;

    Friend.getPersonSalary(Person);

    

    return 0;

}


友元函数:

//getPersonSalary(CPerson &Person)仅在CFriend.hpp声明,实现在CPerson.cpp


//  CPerson.hpp

//  CppProduct

//

//  Created by zhaojunyan on 17/6/17.

//  Copyright © 2017 zhaojunyan. All rights reserved.

//


#ifndef CPerson_hpp

#define CPerson_hpp


#include <stdio.h>

#include "CFriend.hpp"


class CPerson

{

    friend int CFriend::getPersonSalary(CPerson &Person);//友元成员函数

public:

    CPerson(int age, int level, int salary);

    ~CPerson();

    int getAge();

    

public:

    int m_age;

    //friend class CFriend; //友元类

protected:

    int m_level;

private:

    int m_salary;

};


#endif /* CPerson_hpp */


//  CPerson.cpp

//  CppProduct

//

//  Created by zhaojunyan on 17/6/17.

//  Copyright © 2017 zhaojunyan. All rights reserved.

//


#include "CPerson.hpp"

#include <iostream>


//初始化列表仅与定义顺序有关

CPerson::CPerson(int age, int level, int salary):m_age(age),m_level(level),m_salary(salary)

{}


CPerson::~CPerson()

{}


int CPerson::getAge()

{

    return m_age;

}


int CFriend::getPersonSalary(CPerson &Person)

{

    //int salary = Person.m_salary;

    int salary = Person.m_salary;

    std::cout<<"salary: "<<salary<<std::endl;

    return salary;

}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值