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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | #include <iostream> 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
1 | inside show |
Private member function: in a below program we have created a private function which can’t be accessed from outside of class.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | #include <iostream> 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
1 | 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().
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | #include <iostream> 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
1 2 | 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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | #include <iostream> 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
1 2 | 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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | #include <iostream> 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
1 | 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().
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | #include <iostream> 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
1 2 | 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.
1 2 3 4 5 6 7 8 9 | 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
1 | 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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | #include <iostream> 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
1 2 3 4 5 6 | 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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | #include <iostream> 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
1 2 3 | enter number = 4 factorial =24 |