answersLogoWhite

0


Best Answer

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.

}

User Avatar

Wiki User

9y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

10y ago

Public, protected and private. Private access limits member exposure to the class members and to friends of the class. Protected access is the same as private but also allows access to derived classes. Public access is unrestricted.

This answer is:
User Avatar

User Avatar

Wiki User

10y ago

The C++ access specifiers are public, protected and private. Access specifiers allow class designers to limit the exposure of their class members. Private members are only accessible to other members of the class as well as friends of the class. Protected members are the same as private members but are also accessible to derivatives of the class. Public members are fully accessible.

For class data types, the default access is private. For struct data types, the default is public. Other than that, there is no difference between a struct and a class.

class A {

int a;

protected:

int b;

public:

int c;

};

In the above example, A::a is private because private is the default access. A::b is protected because it is in a protected section. For the same reason, A::c is public.

Although the default access is often implied, most programmers will explicitly state the access, even if the default access would have applied anyway. Thus class A may also be declared as follows:

class A {

private:

int a;

protected:

int b;

public:

int c;

};

Access specifiers can also be declared in any order, and may be repeated as often as is required. Note also that access specifiers can be applied to member methods (including constructors and operators) as well as member data, but they do not apply to friend declarations (friends can be declared in any section).

Access specifiers can also be used to determine the visibility of base class members with regards to derived classes.

class B : A {};

Again, most programmers will specify the visibility mode even if the default would apply anyway:

class B : private A {};

In the above example, B inherits from A, and therefore inherits both b and c from A (private members can never be inherited). However, because visibility is private, both B::b and B::c are private.

class C : protected A {};

In the above example, C also inherits b and c, but C::c is protected (C::b remains protected).

class D : public A {};

In the above example, D::b remains protected while D::c remains public.

Sometimes it may be desirable to alter the access of an individual base class member. To achieve this you simply redeclare the base class member with the required access:

class E : public A

{

protected:

int A::c;

};

In the above example, E is effectively the same as B (where both b and c are protected). However, if A had other public members they would remain public to E.

Note that you cannot increase access to base class members, you can only reduce or leave it the same.

This answer is:
User Avatar

User Avatar

Wiki User

10y ago

Access specifiers determine the accessibility of class members. Private class members are accessible to the class and to friends of the class. Protected class members are the same as private but are also accessible to derivatives of the class. Public class members are fully accessible. If no access specifier is specified, private access is implied when declaring class members, while public access is implied when declaring struct members.

This answer is:
User Avatar

User Avatar

Wiki User

10y ago

Private, protected and public. These keywords are also used to declare the type of inheritance, demoting anything with greater access in the base class to the access specified by the inheritance in the derived class. E.g., protected inheritance demotes public members of the base class to protected members of the derived class, while private inheritance demotes protected and public members of the base class to private members of the derived class.

This answer is:
User Avatar

User Avatar

Wiki User

10y ago

There are three: private, protected and public. Private members are only accessible to the class members and to friends of the class. Protected members are the same as private members but are also accessible to derivatives of the class. Public members are fully-accessible, both inside and outside the class.

This answer is:
User Avatar

User Avatar

Wiki User

13y ago

An Access Specifier or an Access Modifier is a keyword that determines the visibility of that entity under consideration. The different access modifiers in Java are:

  • Public
  • Private
  • Protected
  • Default

Public is the least restrictive access specifier whereas private is the most restrictive

This answer is:
User Avatar

User Avatar

Wiki User

12y ago

Private, unless the class is a struct, in which case the default is public.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What is a C plus plus program to demonstrate the use of access specifiers?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

What are the access control specifiers used in c plus plus?

The access control specifiers in C++ are...public - to denote that the member is accessible from any in scope codeprivate - to denote that the member is accessible only from within the containing classprotected - the same as private, except that derived classes are includedPrivate is the default for a class type object, while public is the default for a structure type object.


Are the private access specifiers in Java and C plus plus the same?

No.In Java, the private access modifier restricts member access to the class in which the member is declared. But in C++, private members are also accessible to friends of the class in which they are declared. The rough equivalent in Java would be package private access.Not that Java doesn't have access specifiers, it has access modifiers. When no modifier is specified, default access is implied, which is package private for classes and public for interfaces.


What is xhost plus in Linux?

xhost is server access control program for X. xhost + means is Access is granted to everyone


How to restart c plus plus program?

Exit the program and relaunch it.


Can you program games with c plus plus?

Yes, you can program games with C++.


Is there an answer book for the A plus program?

The A Plus Program is an initiative, not a test. So no, there is no answer book.


Does United Airlines offer special travel rewards?

Yes, their program is called Mileage Plus. Aside from earning miles, being an elite member of their program can get you better seats, discounts, bonus points, and premier access.


In ms access alt plus F4 is used to?

Alt+F4 is an almost universal shortcut in many platforms to exit or quit the application.


Why does sometimes when you run a program it will execute the previous program instead the current open program in C plus plus?

Because you aren't careful enough.


What is the default access specifier in C plus plus?

private


What is the only function all C plus plus programs must contain?

Every C plus plus program that is a main program must have the function 'main'.


A program c plus plus on automorphic numbers or not?

how to write a program that counts automorphic number from 1 to 999