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 ?
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.
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!
}
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
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
class YourClass
{
public:
// anything put here will be public
protected:
// anything put here will be protected
private:
// anything put here will be private
};
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
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.
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.
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:
staticint g_MyClassInt;
};
// in .cpp
int MyClass::g_MyClassInt = 42; // initialize
or, you can have the equivalent.
1 2
// in .cpp
staticint 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).
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).