Friends in C + +--the old king in the programming world

Source: Internet
Author: User

The Friend class in C + + can access all fields of the class, which is simply the old king of the programming world.

Lao Wang can know the neighbor Lee's name, household registration, and even *wife**.

But the old king does not necessarily know the neighbor Lee's son's biological father.

Lao Wang's son, can through Lao Wang know neighbor Li Shutong gossip news.

But Lao Wang's son, like the old king, could not know the neighbor Li Shutong son's biological father.

#include <iostream>using namespace STD;classli{friend classLaowang; Public: Li (): Name (1), M_home (2), M_wife (3) {} ~li () {} Public:intNameprotected:intM_home;Private:intM_wife;};classLiSon: Publicli{ Public: LiSon (): Truefather (4){}Private:intTruefather;};classlaowang{ Public:Virtual voidLookConstli& Li) {cout<<"Laowang know Li ' s"<< Endl;cout<<"Name:"<< Li.name <<"Home:"<< Li.m_home <<"Wife:"<< Li.m_wife <<"."<< Endl; }};classLaowangson: Publiclaowang{ Public:voidLookConstli& Uncleli) {//! cout << "Name:" << uncleli.name << "Home:" << uncleli.m_home << "wife:" << ; uncleli.m_wife<< Endl;        cout<<"Laowang ' s son know"<< Endl; This->laowang::look (Uncleli); }};intMainint,Char* *) {li li;    Laowang Laowang; Laowang.look (LI);cout<<"========"<< Endl;    Laowangson Laowangson; Laowangson.look (LI);cout<<"========"<< Endl;cout<<"Laowang only know LiSon ' s father ' s."<< Endl;    LiSon LiSon; Laowang.look (LiSon);cout<<"========"<<"oh! The firend mean the Laowang. "<< Endl;return 0;}

Note When using friend classes:
* Friend relationships cannot be inherited.
* Friend relationship is one-way and not commutative. If Class B is a friend of Class A, class A is not necessarily a friend of Class B, it depends on whether there is a corresponding declaration in the class.
* Friend relationships do not have transitivity. If Class B is a friend of Class A, Class C is a friend of B, Class C is not necessarily a friend of Class A, it also depends on whether there is a corresponding declaration in the class.

Your lap is so messy.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Friends in C + +--the old king in the programming world

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.