You don't need to make any part of a class private, but then it would behave no differently to a struct, which is public by default. The point of private access is to hide data and methods that are internal to the class; you are not supposed to be able to access them from outside the class.
There are three levels of access: private access is only accessible to the class itself, and to friends of the class. Protected access is the same as private, but is also accessible to derived classes. Public access is accessible to all.
To give an example, imagine the following structure:
struct s
{
unsigned int num;
};
Suppose the num member can be any positive value but must never be zero. That means that whenever we pass this structure to a function, we must assert or verify that num is non-zero. We are relying on those functions to perform verification that should rightly be performed by the structure. But num is public, so there's really no way to safeguard against the structure containing invalid data.
So let's replace the structure with a class:
class c
{
public:
c():num(1);
void setnum(unsigned int number){if(number !=0 ) num = number;}
unsigned int getnum()const{return( num );}
private:
unsigned int num;
};
Now we can be sure that num is never zero. The constructor automatically sets num to positive 1, and the setnum mutator only changes num if the given number parameter is non-zero. Meanwhile, the getnum accessor returns a copy of num, not a reference to num. Only the class itself has access to the private num variable, thus the data is hidden from outside influence, and the public interface guarantees that num will always be non-zero. We no longer need to verify this fact because that functionality is encapsulated within the class itself, where it belongs.
In container class we can only access the public part of base class. For accessing of private and protected part of base class we use friend functions.
In some computer languages it is possible to do so, but I would not even think or design any application in this way. A base class SHOULD NEVER know what the derived classes are. Perhaps it was created by generalizing some classes. Even at that point, this new base class should have no knowledge of the derived classes whatsoever. To do a good OO design, the base class should have a method like getPrivatePartOfDerivedClass() as abstract, then force the derived class to provide the implementation of this method.
Instance variableA variable, part of an Object. These might better be called perObject variables since each instantiated object of this class will have its own private copy of this variable. They are allocated when the object is allocated via new. Static methods may not access the instance variables of their class (or any other class for that matter), other that via some object reference, e.g. anObject.someField. Static methods may even access private instance variables in their class via some object reference.
There is no such thing as visibility mode in C++. Visibility is a function of information hiding but that relates to the way in which implementation details can be obfuscated within binary executables and libraries where only the interface need be exposed in a plain-text header file. This has nothing whatsoever to do with object oriented programming since information hiding is also possible in C. You probably meant access specifiers. There are three levels: private, protected and public. Private access limits access to the class and to friends of the class. Protected is the same as private but extends access to derivatives of the class. Public access imposes no limits. In terms of inheritance, the specified access level determines the accessibility of the protected and public members of the base class (private members are never inherited and will always remain private to the base class). in essence, members with access greater than the specified inheritance are reduced to the specified access. Thus if you specify protected inheritance, all public members of the base class become protected members of the derivative, while private inheritance reduces all public and protected members to private access. You may also reduce access to specific base class members simply be redeclaring them with the appropriate access.
Inner classes are very useful for classes that are written specifically to be used with the encompassing class. A good example of this would be a LinkedListNode class being part of a LinkedList class: public class LinkedList { private LinkedListNode root; private class LinkedListNode { private Object data; private LinkedListNode nextNode; } } No class except your LinkedList class needs to know anything about the LinkedListNode class. So we hide it so no one else needs to worry about what it does.
In container class we can only access the public part of base class. For accessing of private and protected part of base class we use friend functions.
in C++/java if you want to perform data hiding(one of the concept of OOP's) you need to use Private. i.e. only functions in that class or object can use that. public can be used any where in the other program which is not a part of that class. Private classes are defined in order to increase the security, since in many situations there is no need for others to access the private attributes.
In some computer languages it is possible to do so, but I would not even think or design any application in this way. A base class SHOULD NEVER know what the derived classes are. Perhaps it was created by generalizing some classes. Even at that point, this new base class should have no knowledge of the derived classes whatsoever. To do a good OO design, the base class should have a method like getPrivatePartOfDerivedClass() as abstract, then force the derived class to provide the implementation of this method.
Instance variableA variable, part of an Object. These might better be called perObject variables since each instantiated object of this class will have its own private copy of this variable. They are allocated when the object is allocated via new. Static methods may not access the instance variables of their class (or any other class for that matter), other that via some object reference, e.g. anObject.someField. Static methods may even access private instance variables in their class via some object reference.
There is no such thing as visibility mode in C++. Visibility is a function of information hiding but that relates to the way in which implementation details can be obfuscated within binary executables and libraries where only the interface need be exposed in a plain-text header file. This has nothing whatsoever to do with object oriented programming since information hiding is also possible in C. You probably meant access specifiers. There are three levels: private, protected and public. Private access limits access to the class and to friends of the class. Protected is the same as private but extends access to derivatives of the class. Public access imposes no limits. In terms of inheritance, the specified access level determines the accessibility of the protected and public members of the base class (private members are never inherited and will always remain private to the base class). in essence, members with access greater than the specified inheritance are reduced to the specified access. Thus if you specify protected inheritance, all public members of the base class become protected members of the derivative, while private inheritance reduces all public and protected members to private access. You may also reduce access to specific base class members simply be redeclaring them with the appropriate access.
Inner classes are very useful for classes that are written specifically to be used with the encompassing class. A good example of this would be a LinkedListNode class being part of a LinkedList class: public class LinkedList { private LinkedListNode root; private class LinkedListNode { private Object data; private LinkedListNode nextNode; } } No class except your LinkedList class needs to know anything about the LinkedListNode class. So we hide it so no one else needs to worry about what it does.
The only function of a friend is to extend the private class interface outwith the class, essentially making the friend part of the class interface.
no we cannot initialize a constructor in private in order to call a constructor from outside of a class it must be a public member.in order to create an object we should call the constructor .so only private members can implement outside of the class.
a class member declared as private can only be accessed by member functions and friends of that classa class member declared as protected can only be accessed by member functions and friends of that class,and by member functions and friends of derived classes
An Access Modifier is a key word in java that determines what level of access or visibility a particular java variable/method or class has. There are 4 basic access modifiers in java. They are: 1. Public 2. Protected 3. Default and 4. Private Private is the most restrictive access modifier whereas public is the least restrictive. Default is the access protection you get when you do not specifically mention an access modifier to be used for a java object.
A "normal" function is just a function. Even a friend function is just a normal function. However, when a class declares an external function or an external class method to be a friend of the class, the friend function gains access to the private members of the class. class foo { friend void bar(foo&); int m_data; }; void bar(foo& f) { f.m_data=42; } In the example above, the foo class declares the bar function to be a friend function. As such, the bar function has unrestricted access to the private members of foo. In this case, foo::m_data is private (by default) and would therefore be inaccessible to bar were it not declared a friend of foo. Other than that, the bar function is no different to any other function. Note that you cannot declare friendship from outside of a class. The class itself must declare its own friends. However, the same function can be declared friends in more than one class, which can be a useful feature when two or more classes work closely together, as the friend function can be used to provide the "glue" that binds them together.
it grows