C++ Private Member Function & Nested Functions

You have learnt about member functions earlier. We now expand the idea and talk about private member function and nesting of function in C++.

Advertisements

A function declared inside the class’s private section is known as the “private member function”. A private member function is accessible through the only public member function.

Data Members

Data members include members that are mentioned with any of the fundamental types, as well as other types, including pointer, reference, array types, bit fields, and user-defined types.

You can declare a data member the same way as a variable, except those explicit initializers are not allowed inside the class definition. However, a constant static data member of integral or enumeration type may have an explicit initializer.

If an array is declared as a non-static class member, you must specify all of the dimensions of the array.

A class can have members that are of a class type or are pointers or references to a class type. Members that are of a class type must be of a class type that has been mentioned earlier.

An insufficient class type can be used in a member declaration as long as the size of the class is not needed. For example, a member can be implemented that is a pointer to an insufficient class type.

A class X cannot have a member that is of type X, but it can contain pointers to X, references to X, and static objects of X. Member functions of X can take arguments of type X and have a return type of X. 

Member Functions

Member functions are operators and functions that are initialized as members of a class. Member functions do not include operators and functions initialized with the friend specifier.

Advertisements

These are called friends of a class. You can declare a member function as static; this is called a static member function. A member function that is not initialized as static is called a non-static member function.

The definition of a member function is within the scope of its enclosing class. The body of a member function is explored after the class declaration so that members of that class can be used in the member function body, even if the member function definition appears before the declaration of that member in the class member list.

 When the function mul() is called in the following example, the data variables a, b, and c can be used in the body of mul().

Example #1

class x
{
public:
      int mul()             // inline member function mul
      {return a*b*c;};
private:
      int a,b,c;
};

Nesting of member Functions

Whenever we call a member function inside another member function of one class by using dot operator it is known as Nesting of the member function.

 Normally, the member function which is called by another member function is kept private so that it cannot be called directly using the dot operator.

Example #2

#include<iostream.h>
//nested member function

class squarenumb
{
int number;
public:
long square();
void getnumber();
void display();
};

void squarenumb:getnumber()
{
cout<<"Please enter an integer number:";
cin>>number;
}

long squarenumber::square()
{
number=number*number;
return (number);
}

void squarenumb::display()
{
cout<<"Square of the number:"<<square();

}

int main ()
{
squarenumb square;
square.getnumber();
square.display();

return 0;
}

Output:

Please enter an integer number:10
10
Square of the number:100

Summary

The private member function is protection of data wherever necessary, it is for the data protection. Unlike public functions the restricted access ensure that only required members can access the data, which may happen with nested functions.



Advertisements

Ads Blocker Detected!!!

We have detected that you are using extensions to block ads. Please support us by disabling these ads blocker.

Exit mobile version