Nesting of Member Functions – Object Oriented Programming (OOP)

Download our Android App from Google Play Store and start reading Reference Notes Offline.

Nesting of Member Functions | Reference Notes Object Oriented Programming (OOP) Third Semester | Second year BSc.CSIT | Tribhuvan University (TU)

Nesting of Member Functions
A member function of a class can call any other member function of its own class irrespective of its privilege. This is known as nesting of member functions.

Consider the problem of finding the largest of n given number which illustrates the above concept.
```#include<iostream.h> #define MAX_SIZE 100 class Data { int num[MAX_SIZE]; int n; public: void get_data(void);               //declaration int largest(void);                     //declaration void display(void);                  //declaration }; void Data::get_data(void) { cout<<"Enter the total numbers(n):"<<endl; cin>>n; cout<<"Enter the number:"<<endl; for(int i=0;i<n;i++) { cout<<"Enter the number"<<i+1<<";"; cin>>num[i]; } } int Data::largest(void) { int max; max=num[0]; for(int i=1;i<n;i++) { if(max<num[i]) max=num[i]; } return max; } void Data::display(void) { cout<<"The largest number:"<<largest()<<endl(); } void main() { Data num; num.get_data(); num.display(); }```

The class Data has the member function display having the statement cout<<“The largest number:”<<largest()<<endl;. It calls the member function largest() to compute the largest of n given number.

(Visited 3,144 times, 1 visits today)

Posted By : | Comment RSS | Category : Third Semester
Subscribe Notes