Example of private member function in C++
Private Member Function
A function declared inside the class's private section is known as "private member function". A private member function is accessible through the only public member function. (Read more: data members and member functions in C++).
Example:
In this example, there is a class named "Student", which has following data members and member functions:
- Private
- Data members
- rNo - to store roll number
- perc - to store percentage
- Member functions
- inputOn() - to print a message "Input start..." before reading the roll number and percentage using public member function.
- inputOff() - to print a message "Input end..." after reading the roll number and percentage using public member function.
- Public
- Member functions
- read() - to read roll number and percentage of the student
- print() - to print roll number and percentage of the student
Here, inputOn() and inputOff() are the private member functions which are calling inside public member function read().
Program:
Output
Error: When you try to call the private member function inside the main with the object name.
Example: Changing only main() part
Output
need an explanation for this answer? contact us directly to get an explanation for this answer