MrJazsohanisharma

Using Pointer To Member Function C++

Using Pointer To Member Function C++
The pointer to member syntax is very difficult to remember at first sight. It is also not very intuitive. Let us see how to simplify the syntax and also look at its use. Let us consider an example that is first solved without using the pointer to member function (Program 4.15) and then solved using the function (Program 4.16). The two programs can then be compared to understand how the solution with pointer to member function is better.

Let us again use the student class. Now, there are three more requirements to the class. We need to store information about the assignments the students have completed, the projects that he/she has done, and the references that he/she has got. Hence, three more functions are added, namely, AddAssignment(), AddProject(), and AddReference(). There will be three arrays containing details about the assignments, projects, and references.

It is important to note that the number of assignments, projects, or references is different for each student. If we defi ne arrays to store the information, we need to define arrays large enough to accommodate information about a student with the maximum number of, projects, or references. The array size can be kept at 100 for assignments as most students do not have more than five assignments. A better solution is to have a linkedlist for storing student information. Program 4.15 uses the simpler approach of having arrays large enough for accepting details about students having the maximum number of assignments, projects, or references.

Solution without using pointer to member functions


    
#include 
using namespace std;
enum jobs {ASSIGNMENT, PROJECT, REFERENCE};
class Student
{
private:
int RollNumber;
string Name;
string Address;
string Assignments[100];
string Projects[20];
string References[10];
public:
static int NextAssignment;
static int NextProject;
static int NextReference;
void GetDetails()
{
cout << "\n Enterroll number: ";
cin >> RollNumber;
cout << "\n Enter Name: ";
cin >> Name;
cout << "\n Enter address: ";
cin >> Address;
}
void AddAssignment()
{
string NewAssignment;
cout << "Enter next assignment \n";
cin >> NewAssignment;
Assignments[NextAssignment] = NewAssignment;
NextAssignment++;
}
void AddProject()
{
string NewProject;
cout << "Enter next project \n";
cin >> NewProject;
Projects[NextProject] = NewProject;
NextProject++;
}
void AddReference()
{
string NewReference;
cout << "Enter next reference \n";
cin >> NewReference;
References[NextReference] = NewReference;
NextReference++;
}
void AddJob(jobs StudentJobs, int NoOfJobs)
{

int i;
switch(StudentJobs)
{
case ASSIGNMENT:
for(i = 0; i < NoOfJobs; i++)
{
AddAssignment();
}
break;
case PROJECT:
for(i = 0; i < NoOfJobs; i++)
{
AddProject();
}
break;
case REFERENCE:
for(i = 0; i < NoOfJobs; i++)
{
AddReference();
}
break;
default:
break;
}
}
void PrintDetails()
{
int i;
cout << "Roll number is " << RollNumber << "\n";
cout << "Name is " << Name << "\n";
cout << "Address is " << Address << "\n";
cout << "Assignments are \n";
for(i = 0; i < NextAssignment; i++)
{
cout << Assignments[i]<< "\n";
}
cout << "Projects are \n";
for(i = 0; i < NextProject; i++)
{
cout << Projects[i]<< "\n";
}
cout << "References are \n";
for(i = 0; i < NextReference; i++)
{
cout << References[i]<< "\n";
}
}
};
// Mandatory defi nitions of static variables required at linking time
int Student::NextAssignment;
int Student::NextProject;
int Student::NextReference;
void main()
{
Student Lara;
jobs StudentJobs = ASSIGNMENT;
Lara.GetDetails();
Lara.AddJob(StudentJobs,3);
Lara.PrintDetails();
// Some other job to be done
Lara.AddJob(StudentJobs,2);
}
Output
Enter roll number: 1
Enter name: BrianCharlesLara
Enter address: TheWestIndies
Enter next assignment
Captainship
Enter next assignment
BatsmanShip
Enter next assignment
LeadForTheWorldCup
Roll number is 1
Name is BrianCharlesLara
Address is TheWestIndies
Assignments are
Captainship
BatsmanShip
LeadForTheWorldCup
Projects are
References are
Enter next assignment // Asking for two more assignments
MaintainDisipline
Enter next assignment
CheckPhysicalFitness
    
How the Program Works 
Let us now analyze each module of this program. 
Definitions Look at the defi nition of the three arrays for Assignments, Projects, and References. 

string Assignments[100]; 
string Projects[20]; 
string References[10]; 

Detailed informed is not being stored. If we need to do that, then we have to defi ne Assignment as a class and have an array of objects of Assignment class; the same needs to be done for projects and references. We have taken a simpler approach here where we store information in a string alone. This approach will not help if we need to fi nd how many students have done C++ assignments or have executed projects involving OO modelling. As mentioned earlier, such questions can be answered only if assignments, projects, and references are defi ned as classes in which a function for providing such information is available. However, the objective here is to showcase the need of pointer to member function, for which this solution is good enough.

*

Post a Comment (0)
Previous Post Next Post