C++ public Keyword

The public keyword in C++ is an access specifier that determines the accessibility of class members. Members declared as public can be accessed from anywhere in the program, including outside the class. This makes public members directly accessible by objects of the class or by other classes and functions.

By default, all members of a struct are public, while all members of a class are private unless explicitly specified.


Syntax

</>
Copy
class ClassName {
public:
    // Public members
};
class
The keyword used to define a class.
public
The access specifier indicating that the members below it are accessible from outside the class.
members
The data members or member functions that are accessible from anywhere in the program.

Examples

Example 1: Accessing Public Members

In this example, you will learn how public members of a class can be accessed directly using an object of the class.

</>
Copy
#include <iostream>
using namespace std;

class Person {
public:
    string name; // Public data member
    int age;     // Public data member

    void display() {
        cout << "Name: " << name << ", Age: " << age << endl;
    }
};

int main() {
    Person person;
    person.name = "Alice"; // Directly access public member
    person.age = 30;       // Directly access public member

    person.display(); // Call public member function
    return 0;
}

Output:

Name: Alice, Age: 30

Explanation:

  1. The name and age members are declared as public, allowing direct access from outside the class.
  2. The display method, also public, prints the values of the public members.
  3. In the main function, an object of the Person class is used to assign and display the values of the public members.

Example 2: Public Members in a struct

In this example, you will learn how members of a struct are public by default.

</>
Copy
#include <iostream>
using namespace std;

struct Point {
    int x; // Public by default
    int y; // Public by default

    void display() {
        cout << "Point: (" << x << ", " << y << ")" << endl;
    }
};

int main() {
    Point p;
    p.x = 5; // Directly access public member
    p.y = 10; // Directly access public member

    p.display(); // Call public method
    return 0;
}

Output:

Point: (5, 10)

Explanation:

  1. The Point structure has two data members, x and y, which are public by default.
  2. The display method prints the values of x and y.
  3. In the main function, the x and y members are accessed and modified directly, and the result is displayed using the display method.

Key Points about public Keyword

  1. The public keyword allows members of a class to be accessed from anywhere in the program.
  2. Members of a struct are public by default, whereas members of a class are private by default.
  3. Use public members for operations that need to be accessible outside the class, such as interface methods or direct data manipulation (though the latter is discouraged for encapsulation).
  4. Public members are the most accessible but should be used carefully to maintain encapsulation and data integrity.