Example(s):Palindrome, prime, armstrong, "linear search", reverse etc.
Example(s):1575, 1632, 1539 (Only one at a time)
Login
[lwa]
Q&A
#5432
Question:
What is the difference between public member and private member of a class.
Answer:
Srl
Public Member
Private Member
1.
A public member can be accessed outside the class by creating a class object in other methods.
A private member can only be used within the methods of the same class.
2.
A public member can be accessed by child classes as public, private or protected based on the visibility mode set while deriving the child class.
A private member is not visible to even the child class.
Notes:
Example:
class Father
{
private: int x;
public: int y;
};
class Son:public Father
{
private: int p;
public: int q;
void childfunc()
{
x=10 //This is not valid even for a publicly visible child class
y=10; //This is valid as y is publicly visible
}
};
int OutsideFunc()
{
Father F;
F.x=10; // This is not valid as x is private
F.y=2-; //This is valid as y is public
}