`public` — C++ Keyword
`public` — C++ Keyword
The public keyword in C++: grants unrestricted access to class members.
`public` — C++ Keyword
The public keyword in C++: grants unrestricted access to class members.
Use reference pages to confirm names, categories, nearby facilities, and the constraints that matter before writing or reviewing code.
publicDeclares that the following class members are accessible from anywhere. Also specifies public inheritance when inheriting from a base class.
class Name {
public:
// accessible everywhere
};
class Derived : public Base { ... };
#include <print>
class Rectangle {
public:
Rectangle(double w, double h) : w_(w), h_(h) {}
double area() const { return w_ * h_; } // public method
private:
double w_, h_;
};
class Square : public Rectangle {
public:
explicit Square(double s) : Rectangle(s, s) {}
};
int main() {
Rectangle r{3.0, 4.0};
std::println("{}", r.area()); // 12
Square s{5.0};
std::println("{}", s.area()); // 25
}
struct members are public by default; class members are private by default.int main() {
// Pick one facility from this reference page.
// Write the smallest program that exercises its main precondition,
// complexity rule, or lifetime constraint before scaling up.
return 0;
}