久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

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

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

問題描述

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

解決方案

什么是訪問說明符?

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

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

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

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

源代碼示例:

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

<小時>

繼承和訪問說明符

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

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

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

<塊引用>

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

公共繼承:

<塊引用>

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

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

代碼示例:

類庫{民眾:一個;受保護:國際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現(xiàn)在是Derived的私有成員b = 20;//不允許,編譯錯誤,b現(xiàn)在是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 的公共派生,現(xiàn)在是 Derived2 的受保護成員b = 20;//允許,b是Derived &里面的受保護成員Derived2 是 Derived 的公共派生,b 現(xiàn)在是 Derived2 的受保護成員c = 30;//不允許,編譯錯誤}};int main(){派生對象;目標.a = 10;//不允許,編譯錯誤obj.b = 20;//不允許,編譯錯誤obj.c = 30;//不允許,編譯錯誤}

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

<小時>

注意事項:

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

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

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

考慮以下代碼示例:

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

它給出了一個編譯錯誤:

<塊引用>

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

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

<小時>

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

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

<塊引用>

那么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模板網(wǎng)!

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

相關文檔推薦

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?(什么時候使用內聯(lián)函數(shù),什么時候不使用?)
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 仍然相關嗎?)
主站蜘蛛池模板: 国内自拍视频在线观看 | 在线日韩 | 日韩欧美在线不卡 | 国产乱码一二三区精品 | 久久这里只有精品首页 | av官网在线 | www.国产精品 | 免费精品久久久久久中文字幕 | 北条麻妃视频在线观看 | 爱草在线 | 天天插天天操 | 伊人网在线看 | 久久这里只有精品首页 | 亚洲高清在线 | 欧美日韩亚| 成人免费在线观看 | 精品国产18久久久久久二百 | 久久精品亚洲欧美日韩久久 | 中文字幕视频在线 | 狠狠插天天干 | 精品二区 | 日韩在线观看一区二区三区 | 国产精品一区二区久久 | 欧美在线视频一区二区 | 午夜小视频在线播放 | 日本三级精品 | 99久久国产| 毛片区 | 澳门永久av免费网站 | 日韩亚洲一区二区 | 日韩三级在线 | 99久久婷婷 | 国产95在线 | 亚洲一区在线观看视频 | www.国产 | 成人超碰在线 | 中文字幕精品一区二区三区精品 | 免费观看av网站 | 亚洲视频一区 | 激情网站在线观看 | 精品国产91 |