In C++, this optional keyword starts declaring the public data members and/or member functions. The public members of a class are highly accessible by other program parts.

It is invoked thus:

class foo
{
public:
public data members
public member functions

protected:
//
private:
//
}

The public section tells the compiler that the data members and member functions are accessible to everything. ALL functions in the program can access public methods and data whether they are instances of the class, descendants, or neither.

C++ class member visibility:

In C++, members of the "public:" section(s) of a class (or struct) definition are visible wherever that class is visible. The public members of the class form its interface to the external world.

One cannot illustrate the behaviour of public: with code snippets, as all accesses tend to be legal. For (non-)examples of things that fail with private:, see that node's writeup.

By default, classes have some predefined members; these members are public members. The default constructor, default copy constructor, operator=, and are all public members. If you wish, you can put them explicitly in some other section -- but then you lose the default definition, and must define them yourself if you use them. Declaring an unused default member private:, and never defining it, is a good way to "delete" that member.

The default visibility of a struct is public:.

C++ inheritance visibility:

You may also specify inheritance as public. Public inheritance means that the inheritance relationship is "in plain sight" for all to use. Of course, publically inheriting from another class keeps all visibility restrictions of the original class -- private: and protected: members do not suddenly become visible in the inheriting class!

Public inheritance matches what is usually considered the "is-a" relationship; it is the form most commonly used.

Pub"lic (?), a. [L. publicus, poblicus, fr. populus people: cf. F. public. See People.]

1.

Of or pertaining to the people; belonging to the people; relating to, or affecting, a nation, state, or community; -- opposed to private; as, the public treasury.

To the public good Private respects must yield. Milton.

He [Alexander Hamilton] touched the dead corpse of the public credit, and it sprung upon its feet. D. Webster.

2.

Open to the knowledge or view of all; general; common; notorious; as, public report; public scandal.

Joseph, . . . not willing to make her a public example, was minded to put her away privily. Matt. i. 19.

3.

Open to common or general use; as, a public road; a public house.

"The public street."

Shak.

Public actstatute Law, an act or statute affecting matters of public concern. Of such statutes the courts take judicial notice. -- Public credit. See under Credit. -- Public funds. See Fund, 3. -- Public house, an inn, or house of entertainment. -- Public law. (a) See International law, under International. (b) A public act or statute. -- Public nuisance. Law See under Nuisance. -- Public orator. Eng. Universities See Orator, 3. -- Public stores, military and naval stores, equipments, etc. -- Public works, all fixed works built by civil engineers for public use, as railways, docks, canals, etc.; but strictly, military and civil engineering works constructed at the public cost.

 

© Webster 1913.


Pub"lic, n.

1.

The general body of mankind, or of a nation, state, or community; the people, indefinitely; as, the American public; also, a particular body or aggregation of people; as, an author's public.

The public is more disposed to censure than to praise. Addison.

2.

A public house; an inn.

[Scot.]

Sir W. Scott.

In public, openly; before an audience or the people at large; not in private or secrecy. "We are to speak in public."

Shak.

 

© Webster 1913.

Log in or register to write something here or to contact authors.