zoukankan      html  css  js  c++  java
  • C++学习012友元

    何为友元,我的理解,友元就是把另一个类当作是我的朋友,朋友之间,是可以访问一些私有的变量的。
    所以,当我们将一个累声明为自己的友元类的时候,那么这个类就可以访问我们自己类中的某些私有变量等
    当我把某个函数声明为自己的友元函数的时候,这个函数也就可以获取到我们自己的稀有变量了

    如下代码

    #include <iostream>
    #include <stdio.h>
    using namespace std;
    class CHand
    {
    
    private:
        int fingernum;
        void write();
    public:
        CHand();
        friend class CMan;
        friend int getfingetfriend();
    };
    class CMan
    {
    public:
        CHand hand;
        int getfingernum();
        void handwrite();
    
    };
    int CMan::getfingernum()
    {
        return hand.fingernum;
    }
    void CMan::handwrite()
    {
    
        hand.write();
    }
    CHand::CHand()
    {
        fingernum =5;
    }
    void CHand::write()
    {
        cout<<"Hand can write
    ";
    }
    
    int getfingetfriend()
    {
        CHand hand;
        return hand.fingernum;
    
    }
    
    
    int main()
    {
    
        CMan man;
        cout<<"the man finget nunber is:"<<man.getfingernum()<<endl;
        cout<<"thr firned function get finget number is:"<<getfingetfriend();
        cout<<endl;
        man.handwrite();
        return 0;
    }
    

  • 相关阅读:
    取目标描述
    DCLF RCVF SNDF SNDRCVF等用法
    CL过程监控JOB的错误消息
    取用户配置文件属性
    SNDBRKMSG 例子
    信息操作
    文件下载解决中文乱码
    table行的上移下移 上下移动
    常用表操作Sql语句
    sql删除重复行
  • 原文地址:https://www.cnblogs.com/DreamDog/p/9160102.html
Copyright © 2011-2022 走看看