什么是访问限定符?我应该继承私人,保护还是公共? [英] What are access specifiers? Should I inherit with private, protected or public?

查看:200
本文介绍了什么是访问限定符?我应该继承私人,保护还是公共?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对于访问修饰符相对于继承的含义感到困惑。涉及 private protected public 关键字?

解决方案

什么是访问规范?



是C ++中的类/ struct / Union的3 访问说明符。这些访问说明符定义如何访问类的成员。当然,类中的任何成员都可以在该类中访问(在同一类的任何成员函数内部)。继续前往访问类型说明符,它们是:



公开 - 声明为Public的成员可通过班上。



受保护 - 声明为Protected的成员只能在从其派生的类中 BUT 之外访问。



私人 - 这些成员只能从课程中访问。不允许外部访问。



源代码示例:

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

int main()
{
MyClass obj;
obj.a = 10; // Allowed
obj.b = 20; // Not Allowed,given compiler error
obj.c = 30; //不允许,给出编译器错误
}






继承和访问说明符



继承是C ++可以是以下类型之一:




  • 私人继承

  • code>继承 继承 $ b $ <$>

  • b


以下是关于以下各项的成员访问规则:


第一个也是最重要的规则私人除了同类成员之外,任何类别的成员都不能访问成员。 >




公开继承:




Public 基类的成员变成 Public 派生类的成员&

All 受保护的成员变成受保护派生类的成员。


成员的访问权没有变化。我们之前讨论的访问规则进一步应用于这些成员。



代码示例:

 类库
{
public:
int a;
protected:
int b;
private:
int c;
};

class Derived:public Base
{
void doSomething()
{
a = 10; // Allowed
b = 20; // Allowed
c = 30; //不允许,编译器错误
}
};

int main()
{
Derived obj;
obj.a = 10; // Allowed
obj.b = 20; //不允许,编译器错误
obj.c = 30; //不允许,编译器错误

}



私人继承: h2>

> code>派生类成员&

所有受保护基类成员变为私有派生类成员


代码示例:

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

class Derived:private Base //没有提到私有是因为类默认为私有
{
void doSomething()
{
a = 10; // Allowed
b = 20; // Allowed
c = 30; //不允许,编译器错误
}
};

class Derived2:public Derived
{
void doSomethingMore()
{
a = 10; // Not Allowed,Compiler Error,a是Derived的私有成员现在
b = 20; //不允许,编译器错误,b是Derived的私有成员现在
c = 30; //不允许,编译器错误
}
};

int main()
{
Derived obj;
obj.a = 10; //不允许,编译器错误
obj.b = 20; //不允许,编译器错误
obj.c = 30; //不允许,编译器错误

}



受保护的继承: h2>

code>派生类成员&

所有受保护基类成员变为受保护派生类的成员。


代码示例:

  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; //不允许,编译器错误
}
};

class Derived2:public Derived
{
void doSomethingMore()
{
a = 10; //允许,a是Derived& Derived2是Derived的公开派生,a现在是Derived2的受保护成员
b = 20; //允许,b是Derived& Derived2是Derived的公有派生,b现在是Derived2的受保护成员
c = 30; //不允许,编译器错误
}
};

int main()
{
Derived obj;
obj.a = 10; //不允许,编译器错误
obj.b = 20; //不允许,编译器错误
obj.c = 30; //不允许,编译器错误
}

记住相同的访问规则适用于类和成员沿着继承层次结构。






重要注意事项:



- 访问规范是每个类不是每个对象



请注意,访问规范C ++是基于每个类工作的,而不是

一个很好的例子是,在复制构造函数或复制赋值运算符函数中,可以访问正在传递的对象的所有成员。



- 派生类只能访问自己的Base类的成员



以下代码示例

  class Myclass 
{
protected:
int x;
};

类派生:public Myclass
{
public:
void f(Myclass& obj)
{
obj.x = 5;
}
};

int main()
{
return 0;
}

这会导致编译错误:


prog.cpp:4:错误:'int Myclass :: x'受保护


因为派生类只能访问其 自己的基类 的成员。注意,这里传递的对象 obj 与被访问的派生类函数无关,它是一个完全不同的对象,因此派生的成员函数无法访问其成员。






什么是朋友 friend 如何影响访问规范规则?



您可以将函数或类声明为 friend 。当这样做时,访问规范规则不适用于 friend ed类/函数。类或函数可以访问该特定类的所有成员。


因此,朋友 em>


没有他们,反之他们提高了封装性!



friend ship用于表示两个实体之间的 有意强耦合 。 >
如果两个实体之间存在特殊关系,需要访问其他 private protected 但不希望 使用 public 访问说明符访问,则应使用 friend 船。


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 is 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.

这篇关于什么是访问限定符?我应该继承私人,保护还是公共?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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