public

Outline Button

TELEGRAM

Outline Button

TWITTER

class MyClass {

public:

int myNum;

string myString;

};


int main() {

MyClass myObj;


myObj.myNum = 15;

myObj.myString = "Some text";


cout << myObj.myNum << "\n";

cout << myObj.myString;

return 0;

}

Output:

15

Some text


All the class members declared under the public specifier will be available to everyone. The data members and member functions declared as public can be accessed by other classes and functions too. The public members of a class can be accessed from anywhere in the program using the direct member access operator (.) with the object of that class.