C++ private member functions

C++ private member function

Although it is normal practice to place all the data items in private section and all the functions in public, some situations may certain functions to be to be hide(like private data) from the outside calls. Task such as deleting an account of a customer file, or providing increment to any employee are events of serious consequences and therefore the functions handling such tasks should restricted access. We can place these functions in the private scope

A private member function can only be called by another function that is a member of same or its class.
Even an object cannot invoke a private function using the .(dot) operator. consider class as defined below

class student
{
int roll;
void read(); // private member function
public:
void update();
void write();
};

so if s is the object of student class then
s.read(); // will not work m object cannot access private members or member functions
is illegal however the function read() can be called only by the function update() to update the value of m

void sample:: update()
{
read(); // simple call no object used
}

ARRAY WITHIN A CLASS

The array can be used a member variable in a class. The following class definition is valid

const int size=12; // provide value for array size

class Array
{
int a[size]; // here ‘a’ is int type array
public:
void setvalue();
void putvalue();
};

The array variable a[] declared as a private member of the class array can be used in the member functions. like any other variable.we can perform any operations on it. for instance, in the above class definition, the member function setval() sets the values of elements of the array a[], and display() function display the values. similarly we may use other member functions to perform any other operations on th array values

let us take example of Array with in the class

# include <iostream>
const int size=5;
class student
{
int roll_no;
int marks[size];
public:
void getdata ();
void tot_marks ();
} ;
void student :: getdata ()
{
std::cout<<"\nEnter roll no: ";
std::cin>>roll_no;
for(int i=0; i < size; i++)
{
std::cout<<"Enter marks in subject" << (i+1) << ":";
std::cin>> marks[i];
}
}
void student :: tot_marks() //calculating total marks
{
int total=0;
for(int i=0; i < size; i++)
std::cout <<"\n\nTotal marks"<< total;
}
int main()
{
student stu;
stu.getdata();
stu.tot_marks() ;
}

Output:
Enter roll no: 101
Enter marks in subject 1: 67
Enter marks in subject 2 : 54
Enter marks in subject 3 : 68
Enter marks in subject 4 : 72
Enter marks in subject 5 : 82
Total marks = 343

  • C++ Dynamic Initialization of Objects
  • C++ Copy Constructor
  • C++ Dynamic Constructor
  • C++ Destructors
  • C++ Exercise
  • C++ Operator Overloading
  • C++ Overloading Unary Operators
  • Const pointer in C
  • Void pointer in c
  • C++ Overloading Binary Operators
  • C++ Overloading Binary Operators Using Friends
  • C++ Manipulation String Using Operators
  • C++ Rules for overloading operators
  • C++ Exercise
  • C++ Basic To class Type
  • C++ Class TO Basic Type
  • C++ One class To another class type
  • C++ Exercise
  • C++ Inheritance introduction
  • C++ Single Inheritance
  • C++ Multiple Inheritance
  • C++ Ambiguity Resolution in inheritance
  • C++ Hierarchical Inheritance
  • C++ Hybrid Inheritance
  • C++ Virtual Base Classes
  • C++ Exercise
  • C++ abstract class
  • C++ nesting of classes
  • C++ Exercise
  • C++ polymorphism
  • C++ Exercise
  • C++ pointers
  • C++ Pointers TO object
  • C++ this pointer
  • C++ Pointer to Derived class
  • C++ Virtual functions
  • C++ Exercise
  • C++ streams
  • C++ unformatted I/O operations
  • C++ Put() and get()
  • C++ getline() and write()
  • C++ Formatted console I/O
  • C++ Manipulators
  • C++ Exercise
  • C++ file handling
  • C++ file stream classes
  • C++ Open and closing file
  • C++ open using constructor
  • C++ open using open()
  • C++ Detecting End of file
  • C++ File modes
  • C++ File pointers and Manipulators
  • C++ Sequential I/O
  • C++ Reading and Writing
  • C++ Updating a File
  • C++ Error handling In File
  • C++ Command Line Arguments
  • C++ Exercise
  • C++ Template introduction
  • C++ Class Templates with multiple Parameters
  • C++ Function templates
  • C++ Function templates with multiple parameters
  • C++ member function Template
  • C++ Exercise
  • C++ Exception handling
  • C++ Basics of Exception Handling
  • C++ Exception Handling Mechanism
  • C++ Throwing Mechanism
  • C++ Catch Mechanism
  • C++ Catch all Exceptions
  • C++ Re-Throwing An Exception
  • C++ Specifying Exceptions
  • C++ Exercise