友谊和继承有什么区别? [英] What's the difference between friendship and inheritance?

查看:95
本文介绍了友谊和继承有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设有两个A和B类:

class A {};
class B {};

以下两个例子在哪些方面有所区别?

In what aspects differ the two examples below?

示例1:

class C : public A, public B {}; 

示例2:

class C
{
    //private
    friend class A;
    friend class B;
}


推荐答案

A 朋友可以触摸任何朋友的私人部分(只是有点故意!;)),但没有任何 A B C 的一部分 - 它只是意味着 A B 可以触及 C 的私有位)。 A B 私人。 c $ c>,所以如果 C 受保护公开会员,也将可用。

A friend can touch the private parts (pun only slightly intentional! ;) ) of whatever it is friend of, but nothing of A and B are part of C - it just means that "A and B can touch C's private bits"). Anything "less" than private is of course also available to A and B, so if C has protected or public members, that will also be available.

当你继承时, A B 成为 C 的一部分。 A B 的任何私有部分均不可用于 C 。在is-a与has-a命名法中, C 现在是-$ A 并且是-a B - 换句话说,它继承自 A ,所以它表现得像从接口角度看

When you inherit, the A and B becomes part of C. Any private sections of A and B are not available to C. In the "is-a" vs. "has-a" nomenclature, C now is-a A and is-a B - in other words, it's inherited from A, so it "behaves like A from an interface perspective.

这篇关于友谊和继承有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆