Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ classes (public, private, and protected)

How can classes in C++ be declared public, private, or protected?

like image 694
Simplicity Avatar asked Jan 31 '11 19:01

Simplicity


People also ask

What is public/private and protected in C?

In C++, there are three access specifiers: public - members are accessible from outside the class. private - members cannot be accessed (or viewed) from outside the class. protected - members cannot be accessed from outside the class, however, they can be accessed in inherited classes.

What is public class in C?

public (C++) When preceding a list of class members, the public keyword specifies that those members are accessible from any function. This applies to all members declared up to the next access specifier or the end of the class.

What is protected in C?

The protected keyword specifies access to class members in the member-list up to the next access specifier ( public or private ) or the end of the class definition. Class members declared as protected can be used only by the following: Member functions of the class that originally declared these members.

Are C++ classes Public or private?

By default access to members of a C++ class is private. The private members are not accessible outside the class; they can be accessed only through methods of the class. The public members form an interface to the class and are accessible outside the class.


1 Answers

In C++ there is no notion of an entire class having an access specifier the way that there is in Java or C#. If a piece of code has visibility of a class, it can reference the name of that class and manipulate it. That said, there are a few restrictions on this. Just because you can reference a class doesn't mean you can instantiate it, for example, since the constructor might be marked private. Similarly, if the class is a nested class declared in another class's private or protected section, then the class won't be accessible outside from that class and its friends.

like image 119
templatetypedef Avatar answered Oct 11 '22 18:10

templatetypedef