Example of private member function in C++

C++ | Private member function Example: Here, we are going to learn about the private member function in C++ with Example.
Submitted by IncludeHelp, on September 19, 2018 [Last updated : March 01, 2023]

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().

C++ program to demonstrate the example of private member function

#include <iostream>
using namespace std;

class Student {
private:
    int rNo;
    float perc;
    //private member functions
    void inputOn(void)
    {
        cout << "Input start..." << endl;
    }
    void inputOff(void)
    {
        cout << "Input end..." << endl;
    }

public:
    //public member functions
    void read(void)
    {
        //calling first member function
        inputOn();
        //read rNo and perc
        cout << "Enter roll number: ";
        cin >> rNo;
        cout << "Enter percentage: ";
        cin >> perc;
        //calling second member function
        inputOff();
    }
    void print(void)
    {
        cout << endl;
        cout << "Roll Number: " << rNo << endl;
        cout << "Percentage: " << perc << "%" << endl;
    }
};

//Main code
int main()
{
    //declaring object of class student
    Student std;

    //reading and printing details of a student
    std.read();
    std.print();

    return 0;
}

Output

Input start...
Enter roll number: 101
Enter percentage: 84.02
Input end...

Roll Number: 101
Percentage: 84.02%

Error: When you try to call the private member function inside the main with the object name.

Example: Changing only main() part

//Main code
int main()
{
	//declaring object of class student
	Student std;
	//trying to call private data member 
	std.inputOn(); //error - because it's a private member
	//reading and printing details of a student
	std.read();
	std.print();
	
	return 0;
}

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();
  


Related Programs



Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.