pbootcms网站模板|日韩1区2区|织梦模板||网站源码|日韩1区2区|jquery建站特效-html5模板网

什么是訪問說明符?我應該繼承私有的、受保護的

What are access specifiers? Should I inherit with private, protected or public?(什么是訪問說明符?我應該繼承私有的、受保護的還是公共的?)
本文介紹了什么是訪問說明符?我應該繼承私有的、受保護的還是公共的?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我對訪問修飾符在繼承方面的含義感到困惑.涉及 privateprotectedpublic 關鍵字的繼承之間有什么區別?

解決方案

什么是訪問說明符?

C++ 中的類/結構/聯合有 3 個訪問說明符.這些訪問說明符定義了如何訪問類的成員.當然,一個類的任何成員都可以在該類中訪問(在同一個類的任何成員函數中).繼續討論訪問說明符的類型,它們是:

Public - 聲明為 Public 的成員可以通過類的對象從類外部訪問.

Protected - 聲明為 Protected 的成員只能在從它派生的類中BUT的外部訪問.

私人 - 這些成員只能從班級內部訪問.不允許外部訪問.

源代碼示例:

class MyClass{民眾:一個;受保護:國際b;私人的:國際 c;};int main(){MyClass obj;目標.a = 10;//允許obj.b = 20;//不允許,給出編譯錯誤obj.c = 30;//不允許,給出編譯錯誤}

<小時>

繼承和訪問說明符

C++ 中的繼承可以是以下類型之一:

  • 私有繼承
  • 公共繼承
  • 受保護繼承

以下是與這些相關的成員訪問規則:

<塊引用>

第一個也是最重要的規則Private一個類的成員不能從任何地方訪問,除了同一個類的成員.

公共繼承:

<塊引用>

基類的所有Public成員成為派生類的Public成員&
基類的所有 Protected 成員都成為派生類的 Protected 成員.

即成員的訪問權限沒有變化.我們之前討論的訪問規則會進一步應用于這些成員.

代碼示例:

類庫{民眾:一個;受保護:國際b;私人的:國際 c;};派生類:公共基礎{void doSomething(){一 = 10;//允許b = 20;//允許c = 30;//不允許,編譯錯誤}};int main(){派生對象;目標.a = 10;//允許obj.b = 20;//不允許,編譯錯誤obj.c = 30;//不允許,編譯錯誤}

私有繼承:

<塊引用>

基類的所有公共成員成為派生類的私有成員&
基類的所有 Protected 成員都成為派生類的 Private 成員.

代碼示例:

類庫{民眾:一個;受保護:國際b;私人的:國際 c;};class Derived:private Base//不提private也可以,因為類默認是private{void doSomething(){一 = 10;//允許b = 20;//允許c = 30;//不允許,編譯錯誤}};類派生2:公共派生{void doSomethingMore(){一 = 10;//不允許,編譯器錯誤,a現在是Derived的私有成員b = 20;//不允許,編譯錯誤,b現在是Derived的私有成員c = 30;//不允許,編譯錯誤}};int main(){派生對象;目標.a = 10;//不允許,編譯錯誤obj.b = 20;//不允許,編譯錯誤obj.c = 30;//不允許,編譯錯誤}

受保護的繼承:

<塊引用>

基類的所有Public成員成為派生類的Protected成員&
基類的所有 Protected 成員都成為派生類的 Protected 成員.

代碼示例:

類庫{民眾:一個;受保護:國際b;私人的:國際 c;};派生類:受保護的基類{void doSomething(){一 = 10;//允許b = 20;//允許c = 30;//不允許,編譯錯誤}};類派生2:公共派生{void doSomethingMore(){一 = 10;//允許,派生&里面的a是受保護的成員Derived2 是 Derived 的公共派生,現在是 Derived2 的受保護成員b = 20;//允許,b是Derived &里面的受保護成員Derived2 是 Derived 的公共派生,b 現在是 Derived2 的受保護成員c = 30;//不允許,編譯錯誤}};int main(){派生對象;目標.a = 10;//不允許,編譯錯誤obj.b = 20;//不允許,編譯錯誤obj.c = 30;//不允許,編譯錯誤}

請記住,相同的訪問規則適用于繼承層次結構中的類和成員.

<小時>

注意事項:

- 訪問規范是針對每個類的,而不是針對每個對象的

請注意,訪問規范 C++ 是在每個類的基礎上工作的,而不是在每個對象的基礎上工作的.
一個很好的例子是,在復制構造函數或復制賦值運算符函數中,可以訪問被傳遞對象的所有成員.

- 派生類只能訪問自己基類的成員

考慮以下代碼示例:

class Myclass{受保護:整數 x;};派生類:公共Myclass{民眾:void f( Myclass& obj ){obj.x = 5;}};int main(){返回0;}

它給出了一個編譯錯誤:

<塊引用>

prog.cpp:4: 錯誤:'int Myclass::x' 受保護

因為派生類只能訪問其自己的基類的成員.請注意,這里傳遞的對象 obj 與訪問它的 derived 類函數沒有任何關系,它是一個完全不同的對象,因此 derived 成員函數無法訪問其成員.

<小時>

什么是朋友?friend 如何影響訪問規范規則?

你可以將一個函數或類聲明為另一個類的friend.當您這樣做時,訪問規范規則不適用于 friend ed 類/函數.類或函數可以訪問該特定類的所有成員.

<塊引用>

那么friend會破壞封裝嗎?

不,他們沒有,相反,他們增強了封裝性!

friendship 用于表示兩個實體之間的有意強耦合.
如果兩個實體之間存在特殊關系,以至于一個需要訪問其他privateprotected 成員,但您不希望每個人 要通過使用 public 訪問說明符獲得訪問權限,那么您應該使用 friendship.

I am confused about the meaning of access modifiers with respect to inheritance. What is the difference between inheritance involving the private, protected and public keywords?

解決方案

what are Access Specifiers?

There are 3 access specifiers for a class/struct/Union in C++. These access specifiers define how the members of the class can be accessed. Of course, any member of a class is accessible within that class(Inside any member function of that same class). Moving ahead to type of access specifiers, they are:

Public - The members declared as Public are accessible from outside the Class through an object of the class.

Protected - The members declared as Protected are accessible from outside the class BUT only in a class derived from it.

Private - These members are only accessible from within the class. No outside Access is allowed.

An Source Code Example:

class MyClass
{
    public:
        int a;
    protected:
        int b;
    private:
        int c;
};

int main()
{
    MyClass obj;
    obj.a = 10;     //Allowed
    obj.b = 20;     //Not Allowed, gives compiler error
    obj.c = 30;     //Not Allowed, gives compiler error
}


Inheritance and Access Specifiers

Inheritance in C++ can be one of the following types:

  • Private Inheritance
  • Public Inheritance
  • Protected inheritance

Here are the member access rules with respect to each of these:

First and most important rule Private members of a class are never accessible from anywhere except the members of the same class.

Public Inheritance:

All Public members of the Base Class become Public Members of the derived class &
All Protected members of the Base Class become Protected Members of the Derived Class.

i.e. No change in the Access of the members. The access rules we discussed before are further then applied to these members.

Code Example:

Class Base
{
    public:
        int a;
    protected:
        int b;
    private:
        int c;
};

class Derived:public Base
{
    void doSomething()
    {
        a = 10;  //Allowed 
        b = 20;  //Allowed
        c = 30;  //Not Allowed, Compiler Error
    }
};

int main()
{
    Derived obj;
    obj.a = 10;  //Allowed
    obj.b = 20;  //Not Allowed, Compiler Error
    obj.c = 30;  //Not Allowed, Compiler Error

}

Private Inheritance:

All Public members of the Base Class become Private Members of the Derived class &
All Protected members of the Base Class become Private Members of the Derived Class.

An code Example:

Class Base
{
    public:
      int a;
    protected:
      int b;
    private:
      int c;
};

class Derived:private Base   //Not mentioning private is OK because for classes it  defaults to private 
{
    void doSomething()
    {
        a = 10;  //Allowed 
        b = 20;  //Allowed
        c = 30;  //Not Allowed, Compiler Error
    }
};

class Derived2:public Derived
{
    void doSomethingMore()
    {
        a = 10;  //Not Allowed, Compiler Error, a is private member of Derived now
        b = 20;  //Not Allowed, Compiler Error, b is private member of Derived now
        c = 30;  //Not Allowed, Compiler Error
    }
};

int main()
{
    Derived obj;
    obj.a = 10;  //Not Allowed, Compiler Error
    obj.b = 20;  //Not Allowed, Compiler Error
    obj.c = 30;  //Not Allowed, Compiler Error

}

Protected Inheritance:

All Public members of the Base Class become Protected Members of the derived class &
All Protected members of the Base Class become Protected Members of the Derived Class.

A Code Example:

Class Base
{
    public:
        int a;
    protected:
        int b;
    private:
        int c;
};

class Derived:protected Base  
{
    void doSomething()
    {
        a = 10;  //Allowed 
        b = 20;  //Allowed
        c = 30;  //Not Allowed, Compiler Error
    }
};

class Derived2:public Derived
{
    void doSomethingMore()
    {
        a = 10;  //Allowed, a is protected member inside Derived & Derived2 is public derivation from Derived, a is now protected member of Derived2
        b = 20;  //Allowed, b is protected member inside Derived & Derived2 is public derivation from Derived, b is now protected member of Derived2
        c = 30;  //Not Allowed, Compiler Error
    }
};

int main()
{
    Derived obj;
    obj.a = 10;  //Not Allowed, Compiler Error
    obj.b = 20;  //Not Allowed, Compiler Error
    obj.c = 30;  //Not Allowed, Compiler Error
}

Remember the same access rules apply to the classes and members down the inheritance hierarchy.


Important points to note:

- Access Specification is per-Class not per-Object

Note that the access specification C++ work on per-Class basis and not per-object basis.
A good example of this is that in a copy constructor or Copy Assignment operator function, all the members of the object being passed can be accessed.

- A Derived class can only access members of its own Base class

Consider the following code example:

class Myclass
{ 
    protected: 
       int x; 
}; 

class derived : public Myclass
{
    public: 
        void f( Myclass& obj ) 
        { 
            obj.x = 5; 
        } 
};

int main()
{
    return 0;
}

It gives an compilation error:

prog.cpp:4: error: ‘int Myclass::x’ is protected

Because the derived class can only access members of its own Base Class. Note that the object obj being passed here is no way related to the derived class function in which it is being accessed, it is an altogether different object and hence derived member function cannot access its members.


What is a friend? How does friend affect access specification rules?

You can declare a function or class as friend of another class. When you do so the access specification rules do not apply to the friended class/function. The class or function can access all the members of that particular class.

So do friends break Encapsulation?

No they don't, On the contrary they enhance Encapsulation!

friendship is used to indicate a intentional strong coupling between two entities.
If there exists a special relationship between two entities such that one needs access to others private or protected members but You do not want everyone to have access by using the public access specifier then you should use friendship.

這篇關于什么是訪問說明符?我應該繼承私有的、受保護的還是公共的?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Algorithm to convert RGB to HSV and HSV to RGB in range 0-255 for both(將 RGB 轉換為 HSV 并將 HSV 轉換為 RGB 的算法,范圍為 0-255)
How to convert an enum type variable to a string?(如何將枚舉類型變量轉換為字符串?)
When to use inline function and when not to use it?(什么時候使用內聯函數,什么時候不使用?)
Examples of good gotos in C or C++(C 或 C++ 中好的 goto 示例)
Significance of ios_base::sync_with_stdio(false); cin.tie(NULL);(ios_base::sync_with_stdio(false) 的意義;cin.tie(NULL);)
Is TCHAR still relevant?(TCHAR 仍然相關嗎?)
主站蜘蛛池模板: 电销卡_稳定企业大语音卡-归属地可选-世纪通信 | 电气控制系统集成商-PLC控制柜变频控制柜-非标自动化定制-电气控制柜成套-NIDEC CT变频器-威肯自动化控制 | 【灵硕展览集团】展台展会设计_展览会展台搭建_展览展示设计一站式服务公司 | 锂电叉车,电动叉车_厂家-山东博峻智能科技有限公司 | 多米诺-多米诺世界纪录团队-多米诺世界-多米诺团队培训-多米诺公关活动-多米诺创意广告-多米诺大型表演-多米诺专业赛事 | 成都热收缩包装机_袖口式膜包机_高速塑封机价格_全自动封切机器_大型套膜机厂家 | 铝镁锰板厂家_进口钛锌板_铝镁锰波浪板_铝镁锰墙面板_铝镁锰屋面-杭州军晟金属建筑材料 | 单螺旋速冻机-双螺旋-流态化-隧道式-食品速冻机厂家-广州冰泉制冷 | 南京种植牙医院【官方挂号】_南京治疗种植牙医院那个好_南京看种植牙哪里好_南京茀莱堡口腔医院 尼龙PA610树脂,尼龙PA612树脂,尼龙PA1010树脂,透明尼龙-谷骐科技【官网】 | 河北码上网络科技|邯郸小程序开发|邯郸微信开发|邯郸网站建设 | 异噻唑啉酮-均三嗪-三丹油-1227-中北杀菌剂厂家 | 碳化硅,氮化硅,冰晶石,绢云母,氟化铝,白刚玉,棕刚玉,石墨,铝粉,铁粉,金属硅粉,金属铝粉,氧化铝粉,硅微粉,蓝晶石,红柱石,莫来石,粉煤灰,三聚磷酸钠,六偏磷酸钠,硫酸镁-皓泉新材料 | 东莞海恒试验仪器设备有限公司 | 网站制作优化_网站SEO推广解决方案-无锡首宸信息科技公司 | 塑料薄膜_PP薄膜_聚乙烯薄膜-常州市鑫美新材料包装厂 | 跨境物流_美国卡派_中大件运输_尾程派送_海外仓一件代发 - 广州环至美供应链平台 | 智能汉显全自动量热仪_微机全自动胶质层指数测定仪-鹤壁市科达仪器仪表有限公司 | 真空搅拌机-行星搅拌机-双行星动力混合机-广州市番禺区源创化工设备厂 | EFM 022静电场测试仪-套帽式风量计-静电平板监测器-上海民仪电子有限公司 | 全自动包装秤_全自动上袋机_全自动套袋机_高位码垛机_全自动包装码垛系统生产线-三维汉界机器(山东)股份有限公司 | 广东护栏厂家-广州护栏网厂家-广东省安麦斯交通设施有限公司 | 广州网站建设_小程序开发_番禺网站建设_佛山网站建设_粤联网络 | 正压送风机-多叶送风口-板式排烟口-德州志诺通风设备 | 工业胀紧套_万向节联轴器_链条-规格齐全-型号选购-非标订做-厂家批发价格-上海乙谛精密机械有限公司 | 武汉画册印刷厂家-企业画册印刷-画册设计印刷制作-宣传画册印刷公司 - 武汉泽雅印刷厂 | 四合院设计_四合院装修_四合院会所设计-四合院古建设计与建造中心1 | 全自动包装秤_全自动上袋机_全自动套袋机_高位码垛机_全自动包装码垛系统生产线-三维汉界机器(山东)股份有限公司 | 湖南自考_湖南自学考试网| FAG轴承,苏州FAG轴承,德国FAG轴承-恩梯必传动设备(苏州)有限公司 | 聚丙烯酰胺_厂家_价格-河南唐达净水材料有限公司 | 蓄电池在线监测系统|SF6在线监控泄露报警系统-武汉中电通电力设备有限公司 | 动库网动库商城-体育用品专卖店:羽毛球,乒乓球拍,网球,户外装备,运动鞋,运动包,运动服饰专卖店-正品运动品网上商城动库商城网 - 动库商城 | 伺服电机维修、驱动器维修「安川|三菱|松下」伺服维修公司-深圳华创益 | 北京中创汇安科贸有限公司 | 光照全温振荡器(智能型)-恒隆仪器 | 压片机_高速_单冲_双层_花篮式_多功能旋转压片机-上海天九压片机厂家 | 精密机械零件加工_CNC加工_精密加工_数控车床加工_精密机械加工_机械零部件加工厂 | 仿古瓦,仿古金属瓦,铝瓦,铜瓦,铝合金瓦-西安东申景观艺术工程有限公司 | 引领中高档酒店加盟_含舍·美素酒店品牌官网| 电动高压冲洗车_价格-江苏速利达机车有限公司 | 新能源汽车教学设备厂家报价[汽车教学设备运营18年]-恒信教具 |