Saturday, 26 July 2014

CLass and Object in C++ -3

                                            

Private member Function :-

Although it is normal practice to place all the data in a private section and all the function in public section .But some situation may require certain function to be hidden (like private data) fro outside calls. 
So we can make a function private . this private function can't be call directly using . operator .
It can only be called by another function that is a member function of that class.

The array can be used as member variable in class .   


  1. // Private member Function
  2. //array within class
  3. #include<iostream>
  4. using namespace std;
  5. class sum_array{
  6.         private:
  7.                 int ar[5]; //array can be used as member variable
  8.                 void print_sum(void); //private member function
  9.         public:
  10.                 void getdata(void);
  11.                
  12. };
  13. void sum_array::getdata(){
  14.         cout<<" Input 5 element to sum ";
  15.         for(int i=0;i<5;i++){
  16.                 cin>>ar[i];
  17.         }
  18.         print_sum();
  19. }
  20. void sum_array::print_sum(){
  21.         int sum=0;
  22.         for(int i=0;i<5;i++){
  23.                 sum=sum+ar[i];
  24.         }
  25.         cout<<" Sum :- "<<sum;
  26. }
  27. main(){
  28.         sum_array x;
  29.         x.getdata();
  30.         // x.print_sum() ->> you can't access this using . operator as private
  31. }

Output:
private function

4 comments:

  1. I would like to thnkx for the efforts you have put in writing this site.
    I am hoping the same high-grade web site post from you in the upcoming also.
    In fact your creative writing skills has inspired me to
    get my own blog now. Actually the blogging is
    spreading its wings rapidly. Your write up is a great example of it.


    Feel free to visit my web page: book of ra free game online;
    ,

    ReplyDelete
  2. Good day! I just would like to offer you a big thumbs up
    for your excellent info you have here on this post.
    I am coming back to your blog for more soon.

    my weblog self build home ()

    ReplyDelete
  3. Searching for actions the whole family can get pleasure from?



    Also visit my blog post: legal drugs (www.legaldrugsforsale.com)

    ReplyDelete
  4. I like the valuable info you provide in your articles.
    Iཿll bookmark your weblog and check again here regularly.
    I am quite certain I will learn many new stuff right here!
    Good luck for the next!

    My webpage vitamins good for brain

    ReplyDelete

THANKS FOR UR GREAT COMMENT

Blogger Widgets