![]() |
Using Pointer To Member Function C++ |
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
How the Program Works#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