Coupon Accepted Successfully!


The public keyword

In Section 8.3, we noticed that the object directly accesses the member variables of structure, whereas the same is not possible with class members. The keyword public can be used to allow objects to access the member variables of a class directly like structure. The public keyword is written inside the class. It is terminated with a colon (:). The member variables and functions declared followed by the keyword public can be accessed directly by the object. The declaration can be done as follows:

class item // class declaration


public: // public section begins

int codeno;

float prize;

int qty;


Consider the following program that illustrates the use of public keyword with class.

8.3 Write a program to declare all members of a class as public. Access the elements using object.


Explanation: In the above program, the members of class item are declared followed by keyword public. The object one of class item accesses the member variables directly. The member variables are initialized and values are displayed on the screen.

Test Your Skills Now!
Take a Quiz now
Reviewer Name