answersLogoWhite

0

Why class is not protected in c?

Updated: 8/19/2019
User Avatar

Wiki User

13y ago

Best Answer

In C#, the concept of protected is to be accessible to derived classes.

Let's assume that a class can be modified as protected. When you want to subclass from such class, wait, you cannot see that class, because only the derived classes can see it, but the one you want to create is not one of them (yet).... I think this is the reason a class cannot have protected accessibility

User Avatar

Wiki User

13y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Why class is not protected in c?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

Which access limitation is found in a class member declared protected internal?

"Internal" is not a C++ keyword, so it is meaningless in this context. "Protected" means that the class member is visible to (has scope from) only the class and classes derived from the class.


Difference between private and protected specifier in c plus plus language?

In C++, the private specifier means that the item can only be accessed by methods of the class, not including methods of derived classes. Protected, on the other hand, means the item can be accessed by methods of the class, and methods of derived classes. Public, to complete the explanation, means that the item can be acessed by any method, this class, another class, or otherwise.


What is friendly in c plus plus?

The keyword "friend" allows a function or variable to have access to a protected member inside a class.


Why you are mark a class protected for inheritance?

Public, Private and Protected "keywards/ access modifiers" are used similarly as they are with variables. Protected variables, methods or class CAN ONLY be used by an inherited class.


Examples of inheritance in c plus plus?

Single-inheritance is where one class inherits directly from another class: class A {}; class B : public A {}; Here, class B inherits all the public and protected members of class A. Multiple-inheritance is where one class inherits directly from two or more classes: class A {}; class B {}; class C : public A, public B {}; Here, class C inherits all the public and protected members of both A and B. Multi-level inheritance is where one class inherits from another class that itself derived. class A {}; class B : public A {}; class C : public B {}; Here, class B inherits all the public and protected members of A while class C inherits all the public and protected members of B, including those inherited from A. Virtual inheritance applies to multi-level inheritance whereby a virtual base class becomes a direct ancestor to the most-derived class. This variation of inheritance is typically used in multiple inheritance situations where two or more intermediate classes inherit from the same base class: class A {}; class B : public virtual A {}; class C : public virtual A {}; class D : public B, public C {}; Here, classes B and C both inherit from class A. Without virtual inheritance this would mean class D would inherit two instances of A (B::A and C::A), thus creating ambiguity when referring to D::A. By employing virtual inheritance, D inherits directly from A, and both B and C inherit from D::A. In other words, B and C share the same instance of A. Another use of virtual inheritance is when you need to make a class final. class A; class B { friend class A; B() {} // private constructor }; class A : public virtual B { }; Here, class A is the final class. Class B is a helper class that has a private constructor while class A is declared a friend of class B. Class A is therefore the only class that can inherit from class B as it is the only class that can construct objects from class B. However, by inheriting class B virtually, we ensure that no other class can be derived from class A because virtual inheritance ensures that the most-derived class must be able to construct a class B object first. Currently, only class A has that privilege and must always be the most-derived class.


What do you mean by protected derivation of a sub class from base class?

When you derive a class (the sub-class) from a base class using protected access, all public members of the base class become protected members of the derived class, while protected members of the base class will remain protected. Private members are never inherited so they remain private to the base class. By contrast, if you use public inheritance, the public members of the base class remain public to the derived class, while protected members of the base class remain protected in the derived class. If you use private inheritance, both the public and protected members of the base class become private to the derived class. Note that accessibility cannot be increased, only reduced or left the same. That is, a protected member of a base class cannot be inherited as a public member of a derived class -- it can only be declared private or remain protected. Note also that accessibility is viewed from outside of the derived class. That is, all members of a base class other than the private members are inherited by the derived class and are therefore fully accessible to the derived class. But from outside of the derived class, all base class accessibility is determined by the access specified by the type of inheritance.


What is a C plus plus program to demonstrate the use of access specifiers?

Access specifiers apply to class and struct data types only. If a member is declared before an access specifier is declared, the default access is implied. Once an access specifier is declared, that specifier remains in force until another specifier is declared. Specifiers can be declared in any order and may be repeated as often as required. The following demonstrates usage and purpose of each specifier. class X { friend void f(); // Friends can be declared anywhere. private: // The default access specifier for class types (implied if omitted). int a; // Only accessible to members of X and to friends of X. protected: int b; // Same as private, also accessible to derivatives of X. public: int c; // Accessible to any code where X is visible. }; struct Y { friend void f(); // Friends can be declared anywhere. public: // The default access specifier for struct types (implied if omitted). int a; // Accessible to any code where Y is visible. protected: int b; // Same as private, also accessible to derivatives of Y. private: int c; // Only accessible to members of Y and friends of Y. }; struct Z : X {}; void f() { X x; x.a = 42; // OK! X::a is private and f is a friend of X. x.b = 42; // OK! X::b is protected and f is a friend of X. x.c = 42; // OK! X::c is public and X is visible to f. Y y; y.a = 42; // OK! Y::a is public and Y is visible to f. y.b = 42; // OK! Y::b is protected and f is a friend of Y. y.c = 42; // OK! Y::c is private and f is a friend of Y. Z z; z.a = 42; // OK! Z::Y::a is public and Z is visible to f. z.b = 42; // OK! Z::Y::b is protected and f is a friend of Y. z.c = 42; // OK! Z::Y::c is private and f is a friend of Y. } int main() { X x; x.a = 42; // error! X::a is private and main is not a friend of X. x.b = 42; // error! X::b is protected and main does not derive from X. x.c = 42; // OK! X::c is public and is X is visible to main. Y y; y.a = 42; // OK! Y::a is public and is Y is visible to main. y.b = 42; // error! Y::b is protected and main does not derive from Y. y.c = 42; // error! Y::c is private and main is not a friend of Y. Z z; z.a = 42; // OK! Z::Y::a is public and Z is visible to main. z.b = 42; // error! Z::Y::b is protected and main is not derived from Y. z.c = 42; // error! Z::Y::c is private and main is not a friend of Y. }


Which C plus plus keyword allows a function outside of a class to access private class members?

The keyword is friend. The external function must be declared a friend of the class (from within the class itself) in order to become a member of the class and thus gain access to the private (and protected) members of the class.


Difference bitween various visibility modes used c plus plus?

By visibility I assume you mean member accessibility. C++ uses three levels of accessibility: private, protected and public. Private members are only accessible to the class itself and friends of the class. Protected members are the same as private members except derived classes also have access. Public members are fully accessible. With regards inheritance, base class members with greater access than that specified are reduced to the specified access in the derived class. Thus public inheritance has no effect on base class member access. Protected inheritance reduces public members of the base class to protected members of the derived class. Private inheritance reduces both public and protected members of the base class to private members of the derived class. Private members of the base class are never inherited, thus they always remain private to the base class. Note that access to base class members can never be increased through inheritance, only reduced or kept the same. However, as well as defining an overall inheritance access, you can also specify member-wise inheritance access. Thus you could use public inheritance overall, but specify certain public members of the base class to be protected or private in the derived class and/or certain protected members of the base class to be private members of the derived class.


What is multilevel inheritance in C plus plus?

Multi-level inheritance involves at least 3 classes, a, b and c, such that a is derived from b, and b is derived from c. c is therefore the least-derived base class, b is an intermediate base class and a is the most-derived class.


What class address is 192.168.1.21?

Class C.


What is visibility mode what are the different inheritance visibility modes support by c plus plus?

There is no such thing as visibility mode in C++. Visibility is a function of information hiding but that relates to the way in which implementation details can be obfuscated within binary executables and libraries where only the interface need be exposed in a plain-text header file. This has nothing whatsoever to do with object oriented programming since information hiding is also possible in C. You probably meant access specifiers. There are three levels: private, protected and public. Private access limits access to the class and to friends of the class. Protected is the same as private but extends access to derivatives of the class. Public access imposes no limits. In terms of inheritance, the specified access level determines the accessibility of the protected and public members of the base class (private members are never inherited and will always remain private to the base class). in essence, members with access greater than the specified inheritance are reduced to the specified access. Thus if you specify protected inheritance, all public members of the base class become protected members of the derivative, while private inheritance reduces all public and protected members to private access. You may also reduce access to specific base class members simply be redeclaring them with the appropriate access.