c++

#include<iostream>
using namespace std;

class FirstClass{
public:

private:
FirstFunction(){
cout<<"Random text "<<endl;
}
};
int main ()
{
return 0;
}

My question is how to access "PRIVATE" with PUBLIC inside the CLASS and to call it in main function?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include<iostream>
using namespace std;

class FirstClass
{
  public:
  void publicFunctionCallingPrivate()
  {
    FirstFunction();
  }

  private:
  void FirstFunction()
  {
    cout<<"Random text "<<endl;
  }
};

int main ()
{
  FirstClass object;
  object.publicFunctionCallingPrivate();
  return 0;
}
private and public are for external use. inside the class everything is public to itself.

that is, inside your class you can call Firstfunction, but outside it, you cannot, because it is private.

take out the private keyword.
then in main do this

main
{
FirstClass x;
x.FirstFunction();
}

and it should work. but if you leave it private, it will not. When you make something private, you are FORCING that part of the object to NOT BE accessible; that is the whole point of it.
Last edited on
wooow man i didint think it can work like that? So i can basicly call my private function inside a public function and then just make an object and call it in main? Thats fabulastic..
Thanks man! I appreciate it!!
You have no idea how much you just helped me!!! Thanks
#peace
#respect
yes, this is the exact same concept for a function that getters/setters give you for variable members. I didn't realize you wanted to do that, and didnt think to offer it.
I have one question tho.
When i make my program and i wanna sell it to a person. How do i hide PRIVATE CLASS from a person that bought the program?
programs and code are different things.
are you going to sell source code (unusual, but it is done sometimes) or the program? The program (compiled, runnable thing that they can execute) would not have the source code and the user would not have any clue about any of your functions or variables.

if you want to hide things in a library, private works. They get an interface (functions they can call and objects they can create) but no visibility into its internals. This is like selling a program except they can use your classes in their code, but they can't see into them, its a black box. Think DLLS in windows... many programs can use one, you can use it too, but you can't see the source code for it... all you can see is the .h file with function names you can call and whatever documentation they provided. You can't change it, or recompile it, all you can do is use it as-is, blindly / according to their docs and hope it works as described.
Last edited on
You helped me so much.
Thanks.
Topic archived. No new replies allowed.