Help with using private members outside of a class?

I think this goes here. Yeah, it does.

Anyway, I always have trouble with this. Say, I make a class called "super.h" and it looks like this (note: this is just an example, I am not working on something like this):

"super.h"
1
2
3
4
5
6
7
class super{
    public:
        super(sObject);
    private:
        int a, b;
        super sObject;
};


And then I make a second class "cl.h" and it looks like this:

"cl.h"
1
2
3
4
5
6
class cl{
    public:
        cl();
    private:
        int c, d;
};


and then I make... a calculator function in the "super.cpp" (assume I prototyped it in "super.h".

1
2
3
4
5
6
7
8
9
10
void super::calculator()
{
    cout<<"A: ";
    cin>>a;
    
    cout<<endl<<"B: ";
    cin>>b;
    
    cout<<endl<<endl<<a+b;
}


Then I make the same thing in "cl.cpp":

"cl.cpp"
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include "super.h"
#include "cl.h"
#include <iostream>
using namespace std;

cl::cl(sObject)
: sObject.a(c), sObject.b(d)
{
    
}

void cl::cl_calculator()
{
    sObject.calculator();
}


I don't think that even works, but i am in a rush and made a quick little program in Code::Blocks, so I didnt really pay close attention. Anyway, it wouldnt let me use sObject.a and set it to c, the debugger would always say, "a member is private." I know it is but how do I make it to where it can work? Remember I am in a rush and didnt pay attention to this so I may edit it when I have time in the morning.
Since you set c and d as private, so there's no way to assign values to c or d.
You can set c and d as public, then you can do anything with them.
Or you can refer to the tutorial in this site:
http://www.cplusplus.com/doc/tutorial/classes/
where some simple examples illustrate the basics of class.
Private data members exist for a reason - look up data hiding and data abstraction.
OOOoohh yeah I remember that in a tutorial I watched. Thanks qmzh85. But... just one thing... I thought you weren't supposed to make variables public, because of hackers and stuff?
You are right, AceDawg45. We cannot simply put anything into "public". As L B mentioned, private data members exist for a reason.

I am also new to C++, that's only my limited understanding. Definitedly there would be many other better ways to handle this problem.
AceDawg45 wrote:
because of hackers and stuff?
A hacker would disregard any language you used and just change the raw memory. It is up to the operating system, not you or your program, to defend against that.
Really L B? Wow I didnt know it was up to the OS. I dont really know much about hacking, I always thought it was like, getting into the network and messing up the code of stuff. I program good things, I dont intend to learn hacking (not yet at least)
My point is, making data members private provides no protection of any kind when the program is in binary form or is running in memory - it is only useful during compilation of the program.
Topic archived. No new replies allowed.