zoukankan      html  css  js  c++  java
  • C++笔记(6)友元

    下面我们来看一段代码,看看我们是如何利用友元来访问类的一切成员的。

    #include <iostream>   
    using namespace std;
    class Internet   
    {   
    public:   
        Internet(
    char *name,char *address)   
        
    {   
            strcpy(Internet::name,name);   
            strcpy(Internet::address,address);    
        }

    friend 
    void ShowN(Internet &obj);//友元函数的声明
    public:   
        
    char name[20];
        
    char address[20];
    }
    ;
     
     
    void ShowN(Internet &obj)//函数定义,不能写成,void Internet::ShowN(Internet &obj)
    {
        cout
    <<obj.name<<endl;
    }

    void main()   
    {
        Internet a(
    "中国软件开发实验室","www.cndev-lab.com");
        ShowN(a);
        cin.
    get();
    }
     



    一个普通函数可以是多个类的友元函数,对上面的代码我们进行修改,注意观察变化:

    #include 
    <iostream>   
    using namespace std;
    class Country;
    class Internet   
    {   
    public:   
        Internet(
    char *name,char *address)   
        
    {   
            strcpy(Internet::name,name);   
            strcpy(Internet::address,address);    
        }

    friend 
    void ShowN(Internet &obj,Country &cn);//注意这里
    public:   
        
    char name[20];
        
    char address[20];
    }
    ;
     
    class Country
    {
    public:
        Country()
        
    {
            strcpy(cname,
    "中国");
        }

    friend 
    void ShowN(Internet &obj,Country &cn);//注意这里
    protected:
        
    char cname[30];
    }
    ;
     
    void ShowN(Internet &obj,Country &cn)
    {
        cout
    <<cn.cname<<"|"<<obj.name<<endl;
    }

    void main()   
    {
        Internet a(
    "中国软件开发实验室","www.cndev-lab.com");
        Country b;
        ShowN(a,b);
        cin.
    get();
    }


      一个类的成员函数函数也可以是另一个类的友元,从而可以使得一个类的成员函数可以操作另一个类的数据成员,我们在下面的代码中增加一类Country,注意观察:

    #include 
    <iostream>   
    using namespace std;
    class Internet;
     
    class Country
    {
    public:
        Country()
        
    {
            strcpy(cname,
    "中国");
        }

        
    void Editurl(Internet &temp);//成员函数的声明
    protected:
        
    char cname[30];
    }
    ;
    class Internet
    {   
    public:   
        Internet(
    char *name,char *address)   
        
    {   
            strcpy(Internet::name,name);   
            strcpy(Internet::address,address);  
        }

        friend 
    void Country::Editurl(Internet &temp);//友元函数的声明
    protected:   
        
    char name[20];
        
    char address[20];
    }
    ;
    void Country::Editurl(Internet &temp)//成员函数的外部定义
    {
        strcpy(temp.address,
    "edu.cndev-lab.com");
        cout
    <<temp.name<<"|"<<temp.address<<endl;
    }

    void main()
    {
        Internet a(
    "中国软件开发实验室","www.cndev-lab.com");
        Country b;
        b.Editurl(a);
        cin.
    get();
    }


      整个类也可以是另一个类的友元,该友元也可以称做为友类。友类的每个成员函数都可以访问另一个类的所有成员。

    示例代码如下:

    #include 
    <iostream>   
    using namespace std;
    class Internet;
     
    class Country
    {
    public:
        Country()
        
    {
            strcpy(cname,
    "中国");
        }

        friend 
    class Internet;//友类的声明
    protected:
        
    char cname[30];
    }
    ;
    class Internet
    {
    public:   
        Internet(
    char *name,char *address)   
        
    {   
            strcpy(Internet::name,name);   
            strcpy(Internet::address,address);  
        }

        
    void Editcname(Country &temp);
    protected:   
        
    char name[20];
        
    char address[20];
    }
    ;
    void Internet::Editcname(Country &temp)
    {
        strcpy(temp.cname,
    "中华人民共和国");  
    }

    void main()
    {
        Internet a(
    "中国软件开发实验室","www.cndev-lab.com");
        Country b;
        a.Editcname(b);
        cin.
    get();
    }


      在上面的代码中我们成功的通过Internet类Editcname成员函数操作了Country类的保护成员cname。

      在编程中,我们使用友元的另外一个重要原因是为了方便重载操作符的使用,这些内容我们将在后面的教程着重讨论!

    参考:
    http://www.pconline.com.cn/pcedu/empolder/gj/c/0503/571623_1.html
  • 相关阅读:
    upc组队赛16 Melody【签到水】
    upc组队赛16 WTMGB【模拟】
    upc组队赛15 Supreme Number【打表】
    upc组队赛15 Lattice's basics in digital electronics【模拟】
    upc组队赛15 Made In Heaven【第K短路 A*】
    upc组队赛14 As rich as Crassus【扩展中国剩余定理】
    upc组队赛14 Floating-Point Hazard【求导】
    upc组队赛14 Communication【并查集+floyd /Tarjan】
    upc组队赛14 Bus stop【签到水】
    2018/12/7 数据库事务和分布式事务
  • 原文地址:https://www.cnblogs.com/cutepig/p/1375969.html
Copyright © 2011-2022 走看看