class Foo
{
private:
int a;
protected:
int b;
public:
int c;
void Bar() // internal function
{
a; // allowed
b; // allowed
c; // allowed
}
};
class Goo : Foo
{
void Car() // internal function
{
a; // not allowed, this was private
b; // allowed
c; // allowed
}
};
int main()
{
Foo f;
f.a; // not allowed, this is private
f.b; // not allowed, this is protected
f.c; // allowed
Goo g;
g.a; // not allowed
g.b; // not allowed
g.c; // not allowed, inheritance is "private" by default
}
sir,thank u for the reply but i want to ask that....how to access the private and protected functions of the class...........for example....if i have declared a function private in the class then how can i call that function and pass the arguments in that function..........
You declare a private function when you don't want that function available to the outside and are using it as an internal utility. You can only call that function from within (not from outside).
Here's an example of a class with a private function Max() which we use to help our public function addNumber() to manage everything. Sure we could make Max() public, but it doesn't add anything to the class itself so why expose it?
class Foo
{
private:
int m_number1;
int m_number2;
int Max(int a, int b)
{
return a > b ? a : b;
}
public:
void addNumber(int input)
{
m_number2 = Max(m_number1, input);
m_number1 = input;
}
int getBiggestNumber()
{
return Max(m_number2, m_number1);
}
};
nikita 19, you declare a function as private then want to access it publicly... declare it as public then.
you could friend a class and that class would be able to access the information.
But, backing up a bit - you need to evaluate your design process and come up with good class definitions that accurately describe that functionality that you want. Don't just do something to later circumvent it.