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
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
main.cpp: In function 'int main()': main.cpp:10:8: error: 'void Student::inputOn()' is private void inputOn(void) ^ main.cpp:47:14: error: within this context std.inputOn();need an explanation for this answer? contact us directly to get an explanation for this answer