Declare private things at h or c

Oct 7, 2010 at 3:54pm
Hello,
I've seen some c++ code examples with private: section at .h file.
I dont understand the finality of it. If thery are private....
Can anyone give me some reason ?

Thanks
Oct 7, 2010 at 4:10pm
Do you know what a class or struct is? The private keyword is used to declare class members that are not accessible outside the class, hence, they are private to the class.
Oct 7, 2010 at 4:11pm
if a member variable is private only its member functions can modify it

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class A{
public:

void setPri(int i) { pri = i; }
int pub;

private:
int pri;

};

int main()
{
A a;
a.pub = 1; // fine
a.setPri(1); // fine
a.pri = 1; // error!
}

Oct 7, 2010 at 4:20pm
quirkyusername wrote:
if a member variable is private only its member functions can modify access it
Last edited on Oct 7, 2010 at 4:20pm
Oct 7, 2010 at 4:50pm
Thanks everybody
I'm talking about .h and .cpp 'model'.
I see a private section at h file.
The unique reason I can think of is a 'work team' reason, with the idea to inform other programmers.
But the same can be achieved putting it at the cpp file, isn't it ?
Another reason ? A compiler advantage ?
tHANKS
Oct 7, 2010 at 6:13pm
The private keyword has no place in the C++ language outside of a class or struct.

You can't declare part of a header as private. You can only declare part of a class as private.
Oct 8, 2010 at 6:20am
Disch:
And is it possible to have private elements for a class into cpp ? I think no . Only can have private elements for all the classes, isn't it ?
Thanks
Oct 8, 2010 at 2:44pm
I don't understand you're question.

It's not that complicated.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class YourClass
{
public:

  // anything put here will be public

protected:

  // anything put here will be protected

private:

  // anything put here will be private

};


There's nothing more to it.
Oct 8, 2010 at 3:00pm
can someone explain from line no. 6 to line no.18? please! :(

1. /*C Program to arrange 5 numbers in ascending order*/
2. #include<iostream.h>
3. #include<conio.h>
4. void main()
5. {
6. int i,j,temp,a[5];

7. cout<<"Enter 5 integer numbers\n";

8. for(i=0;i<5;i++)

9. cin>>a[i];

10. for(i=0;i<5;i++)
11. {
12. for(j=i+1;j<5;j++)

13. {
14. if(a[i]>a[j])
15. {
16. temp=a[j];
17. a[j]=a[i];
18. a[i]=temp;
19. }
20. }

21. }

22. cout<<"\n\nThe 5 numbers sorted in ascending order are\n";

23. for(i=0;i<5;i++)

24. cout<<"\t"<<a[i];
25. getch();
26. }
Oct 8, 2010 at 4:01pm
6 - declare i, j, and temp as integer variables, and a as an array of 5 integers.
7 - output the message to the user
8-9 - loop 5 times, read integer input from keyboard and store integer value in successive positions in the array
10-21 - sort the array in ascending order using an incorrect (but still functional) bubble sort
Oct 8, 2010 at 4:23pm
tonnot wrote:
And is it possible to have private elements for a class into cpp ?

Class declarations are usually kept in header files, so they can be used across a number of .cpp files. But notice that the only difference between .h and .cpp files is conventional. They're both text files and all the #include directive does is copy and paste the text from the .h file into the .cpp file.
Last edited on Oct 8, 2010 at 4:36pm
Oct 9, 2010 at 5:35am
thankyou jsmith! :) can you give me a code for putting the five integers in ascending order using a correct bubble sort or without it?
Oct 9, 2010 at 8:47am
closed account (EzwRko23)
This is a well known inconsistency in the C++ OOP model, caused by the fact they wanted to keep C++ compatible with C. The private keyword is a compiler enforced comment saying, "please, whenever I access this member from outside, treat is as an error". Apart from that, the compiler does not make any other distinction between private/protected/public - internally all private and protected members are part of the **public** interface of the class, so they must be visible to any code using that class. Thus, they are usually placed in headers. If you change a private member signature, add/remove a private member, all the code depending on this class will need to be recompiled.

Note: This is a different behaviour than in true OOP languages (like Smalltalk or Java). In those languages private members are not part of the API/ABI. You can freely change them, and the changes are isolated from the rest of the world.
Last edited on Oct 9, 2010 at 8:52am
Oct 10, 2010 at 3:40am
And is it possible to have private elements for a class into cpp ? I think no . Only can have private elements for all the classes, isn't it ?


You could, using a static variable in a .cpp module. For example, you can have:

1
2
3
4
5
6
7
8
// in .hpp
class MyClass {
  private:
    static int g_MyClassInt;
};

// in .cpp
int MyClass::g_MyClassInt = 42;    // initialize 

or, you can have the equivalent.
1
2
// in .cpp
static int g_MyClassInt = 42;


I often prefer the latter construct over the former because I don't have to reveal my class private members in a header (and add additional compile dependencies between modules).
Oct 11, 2010 at 6:47am
kfmfe04 :
I often prefer the latter construct over the former because I don't have to reveal my class private members in a header (and add additional compile dependencies between modules).

This is the answer that I expected. Thanks
Topic archived. No new replies allowed.