It's useless.

Why are stupid classes in C++ having private members for?

[snip]

I am never using OOP in C++ I'm just using structs to organize data fuck OOP and classes it's so ridiculous to me that I'm SMH

It's to make sure that the user of the class doesn't touch member data that he's not supposed to, or that when he does, it's in a fashion that is defined by the class (via a getter or setter function).

In my opinion, a "read only" keyword would have been a lot more useful (where the member data can only be written to by an object's own member function). To be quite honest, I haven't really found a practical use for private members myself, but that's probably because I write code I don't expect to be used by other people.

Obviously you do not understand. Let me see if I can explain. Take for example this class:

class myClass//it stores an int
{
    public:
    int myInt;
    myClass():myInt(0){}//default value is 0
    myClass(int i):myInt(i){}//you can set it on creation
    myClass(const myClass &o){myInt=o.myInt;}//copy constructor
};

This works fine, and it is the struct you are talking about. It is easy, I do not disagree! But lets say you have something like this:

class myVolatileClass//it stores an array of ints
{
    public:
    int *myInts;//stores an array of ints
    int numInts;//stores the number of ints in the array
    myClass():myInts(NULL),numInts(0){}//by default myInts is null and stores nothing
    myClass(int *ints, int num)//copies the array of ints
    {
        myInts=new int[num];
        for (int i=0; i<num; ++i)
            myInts[i]=ints[i];
        numInts=num;
    }
    myClass(const myClass &other)//a copy constructor
    {
        myInts=new int[other.numInts];
        for (int i=0; i<other.numInts; ++i)
            myInts[i]=other.myInts[i];
        numInts=other.numInts;
    }
    ~myClass()//this class requires a custom destructor to clean up the array
    {
        if (numInts>0)//if there are ints in the array
            delete[]myInts;//delete the array
    }
    int get(int index)//function to access a member of the array
    {
        if (index>=0&&index<numInts)//check that it is a valid index
            return myInts[index];
    }
    void add(int val)//add a value to the array
    {
        int *tmp=new int[numInts+1];//temporary storage for the new array
        for (int i=0; i<numInts; ++i)
            tmp[i]=myInts[i];
        tmp[numInts++]=val;
        if (numInts>0)
            delete[]myInts;
        myInts=tmp;
    }
};

Again this class is simple and works as you would expect... except look at this situation:

int main()
{
    int mydata[]={1,2,3};
    myVolatileClass c(mydata,3);
    //here we will cause an error:
    c.numInts=0;//trololol
    c.add(4);//sigseg here
    return 1;
}

As you can see, because we changed numInts outside the class we caused the class to crash. This is where making numInts and myInts private would be very very helpful in preventing a crash.

well class is a complete package providing you capability to have a set of functions in it too. private will make sure that the member variables or functions are not touchable or accessible to other classes. whereas as public gives other class the capability to access member variables and functions! struct is a basically a block that can be used!

Making member private make them hidden from outside world. So that only class's public member(which is only accessible from outside world) can modify them. Which is called abstraction in OOP.

This is the great feature defined by OOP and adopted by c++, c#, java like languages. Hiding data member prevents accidental modification of data member which is very normal thing.

Say for example you as a software company buy a third party DLL by spending huge amount of money, which provides you lots of classes for graphics manipulation . What if they dont make thier data member private and your developer changes them wrongly and your graphic software crashes. Will you blame the third party that their classes are not working properly. To prevent such kind of obious situation if third party make data members of class as private and expose some public methods which adds validation of data that you are accidentally providing wrong can handle a lot critical errors.

Providing public methods to access private data called encapsulation in OOP.

commented: Good explaination, encapsulation is 1 of the 3 main tenents of OOP the other 2 being inheritance and polymorphism +0

Private and Public are called as access specifiers
Private members are the ones which are not visible outside the class and Public members are visible outside the class by the member functions of the class or by other functions using an object of the class.
Another access specifier is Protected which enables the friend functions of the class to view the private data members.
Take a simple example
If we want to calculate the sum of marks for students and is we make the "marks" data member visible outside the class then other functions can modify it giving erronous results, that is why the concepts of access specifiers was introduced to avoid unwanted modifications in the program

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.