In C Language What is the difference between a function and
In C++ Language:
What is the difference between a function and a member function?
Explain the difference between the private and public access modifier. Give an example using code when a private member cannot be accessed.
Solution
A function can be either public or private, static or non-static, where as the member function is of type non-static.
A function is a block of code which can take a set of values as input, and returns atmost one value as output.
A member function is the one which is defined inside a class, and is used to access/modify the member variables of the instance object of type class.
The private access modifier is that which is a member function which can inturn be called only by the member functions, where as a public access modifier is a member function which can be called from outside the class, usually to access the private members or private methods of the class.
class Example
{
int x;
void incrementX() //This method cannot be called from outside the class.
{
x++;
}
public:
int getX() //This method can be called from outside the class.
{
return x;
}
}
