The private members of a class have strict access control. Only the member functions of the same class can access these members. The private members of a class are inaccessible outside the class, thus providing a mechanism for preventing accidental modifications of the data members.
Example
Class person
{
private:
private members;
………….
Int age;
Int getage( );
};
If w e are setting the private member function to assign the values for the private data members, even though the private member functions are invoked only by the public member functions, i.e., by using the object we cannot invoke directly the private member function. For the reason only in private section data members are
declared, in public section the member functions are declared.
Sample Program
#include <iostream.h>
Class pri
{
int a,b;
void setab(void)
{
a = 2;
b = 4;
}
public:
void callsetad(void)
{
setab();
}
void show(void)
{
cout<<a<<b;
}
};
void main()
{
pri X;
X.callsetab();
X.show();
}
No comments:
Post a Comment