Member functions in C++

In a C++,  we can use the function within the classes. These functions are called as a member function. It can be public ,private & protected.

Public member function: in a below program we have created a public function which can be accessed from outside of class.

#include 

using namespace std;
class Example{
public:
        void show() { // pubic member function                                                                                      
                cout << "inside show";
        }
};

int main() {
        Example ob;
        ob.show(); //call to function show
        return 0;
}

Output

 inside show
 

Private member function:  in a below program we have created a private function which can’t be accessed from outside of class.

#include 

using namespace std;

class Example {
private: 
void show() {// private member function
cout << "inside show";
}
};

int main() {
        Example ob;
        ob.show(); //error , show() is a private member function and can’t be accessed outside of class
        return 0;
}

Output

 error
 

Description: when we compile this program we will get an error message. Because show() function cant be access from main() because it is a private member function.

How to access private member function

 We can access private member function from public member function. In the below program we have created one public function display() and one private function show() and we can call the show() from within the display().

#include 

using namespace std;

class Example{
     private: 
	     void show() { // private member function                    				
           cout << "inside show" << endl;
        }

     public: 
         void display() { // pubic member function   							
           show(); // call to private member function show()			
           cout << "inside display";
        }
};

int main() {
        Example ob;
        //  ob.show();   //error  								  
        ob.display();
        return 0;
}

Output

inside show
inside display
 

Define a member function outside of the class

In a C++ language we can define a function inside  as well as outside of the class. To define a function a function outside of the class syntax is:

Syntax:

Datatype class_name :: function name

#include 

using namespace std;
class Demo {
        public: void show();
        void display();
};

void Demo::show() // define a function show() outside of class
{
        cout << "Inside Show";
}

void Demo::display() // define a function display() outside of class
{
        cout << "\n Inside display";
}

int main() {
        Demo obj;
        obj.show();
        obj.display();
}

Output

inside show
inside display
 

In the above program we have declare two function inside class and define outside of the class.

a private member function outside of the class

In a below program we have define a private function display() outside of the class which can’t be accessed from outside of class. If we try to access then we will get an error message.

#include 

using namespace std;
class Demo {
        public: void show();
        private: void display();
};

void Demo::show() // define a function show() outside of class
{
        cout << "Inside Show";
}

void Demo::display() // error, display is private function
{
        cout << "\n Inside display";
}

int main() {
        Demo obj;
        obj.show();
        obj.display(); // error, display is private function, it can’t call from main()
}

Output

error
 

Solution of above program: We can access private member function from public member function. In the below program we have created one public function show() and one private function display() and we can call the display() from within the show().

#include 

using namespace std;
class Demo {
        public: void show();
        private: void display();
};

void Demo::show() // define a function show() outside of class
{
        cout << "Inside Show";
        display(); // call display function from show()
}

void Demo::display() {
        cout << "\n Inside display";
}

int main() {
        Demo obj;
        obj.show();
}

OUTPUT

Inside Show
 Inside display
 

Access Private Data Member/Variable

In a C++ we can access private data member within the class. It can’t be used outside of the class.

class Demo {
        private: int x, y;
};
int main() {
        Demo obj;
        obj.x = 10; // error x is not accessible hear 
        obj.y = 20;	  // error y is not accessible hear  
        cout<<"sum ="<<(obj. x + obj.y);                // error x & y  are not accessible hear     
}

Output

Error
 

Note: we can’t access private data member outside of the class. The private data can be accessed by only member function and friend  function of that class. 

Solution of above program: The private data can be accessed by only member function and friend  function of that class. 

#include 

using namespace std;
class Demo {
        public: void input();
        void display();
        private: int x, y;
};

void Demo::input() {
        cout << "Enter value for x";
        cin >> x; // access private data “x” in public function 
        cout << "\n Enter value for y";
        cin >> y;
}

void Demo::display() // access private data “x” in public function
{
        cout << "\n value of x=" << x;
        cout << "\n value of y=" << y;
}

int main() {
        Demo obj;
        obj.input();
        obj.display();
        return 0; 
}

Output

Enter value for x
5
Enter value for y
6
 value of x=5
 value of y=6
 

Example. Write a C++ program to create a class and member function & find the factorial of the number given by user.

#include 

using namespace std;

class fact {
        private:
                int num, f, i;
        public:
                void input();
        void show();
};

void fact::input() {
        cout << "enter number =";
        cin >> num;
}

void fact::show() {
        f = 1;
        for (i = 1; i <= num; i++) {
                f = f * i;
        }
        cout << "factorial  =" << f;
}

int main() {
        fact o;
        o.input();
        o.show();
        return 0; 
}

Output

enter number =
4
factorial  =24
 
Categories C++