oriented program

hi,in oriented programming i want to know how we use public in the first part for describing classes why should do we do this ?because we called them by value in the main part and i am wondering why we shoud use this headlines for work our program literally how it works.
thanks
Last edited on
got any examples? It's difficult to know exactly what you want to know if you don't have any examples.

For a general answer however. In Object-Oriented programming languages we have access modifiers, well that's what they call it in java I forgot if they're called the same in C++. We have three access modifiers namely public, private, and protected. All of which have different levels of accessibility. public is used when you want anything in your program to be able to access it. What do I mean with "access it"? Access is basically being able to manipulate or operate on something directly. Well for example suppose we have a person class.

1
2
3
4
5
class Person
{
     public:
          myStuff     // just a general example so I won't include data types
};


since myStuff is public that means that anyone can access it. Kinda like

1
2
3
4
5
6
7
int main()
{
     Person me;
     
     me.myStuff--; // OMG someone is taking my stuff without permission
     return 0;
}


well that shows why public access isn't very good. Since anything in your program can access it, wrong data can be entered thus introducing errors into your program. That's why in OOP attributes are usually private, and you only use public on methods, which are used to access your attributes.
Topic archived. No new replies allowed.