CKTIS / C++
public — public access and public inheritance
The public keyword specifies public access to class members or public inheritance. A public: section exposes a class interface, while : public Base preserves the public and protected access levels of base-class members.
Version: C++23; repository compiler flag, production runtime not attested
Syntax
class Type {
public:
// member declarations
};
class Derived : public Base {
// member declarations
};public has two uses: specifying member access and specifying inheritance access. Inside a class definition, the public: label applies to subsequent declarations until the next access label or the end of the class. These members can be named without access-control restrictions, but other language rules, such as those involving const, still apply.
Members of a class are private by default; members of a struct or union are public by default. Multiple access sections are allowed in any order. A public method does not automatically expose private data: external code can call increment(), but cannot directly use value_.
The declaration class Derived : public Base specifies public inheritance: the base class's public members remain accessible as public, and its protected members as protected. Private base members do not thereby become accessible to the derived class. If the base is unambiguous and publicly accessible, a derived pointer or reference can implicitly convert to the corresponding base type. Without an explicit inheritance access specifier, inheritance defaults to private for class and public for struct.
In the example, NamedCounter exposes the inherited methods, and const Counter& base = counter; demonstrates reference conversion. The program is expected to print 1 followed by a newline. The example targets local verification in C++23; the linked document is an evolving working draft, not a frozen C++23 publication.
Examples
#include <iostream>
class Counter {
int value_ = 0;
public:
void increment() { ++value_; }
int value() const { return value_; }
};
class NamedCounter : public Counter {};
int main() {
NamedCounter counter;
counter.increment();
const Counter& base = counter;
std::cout << base.value() << '\n';
}
Result
1
The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.