The Question is as follows: Create 2 classes Students, Subjects and enter their data members as Maps and interconnect them using Composition.The code is as follows:
#include <iostream>
#include <string>
#include <map>
using namespace std;
class student {
public:
map<int, string> student_map;
student()
{
student_map[100] = " Noel Philip";
student_map[101] = " John";
student_map[102] = " Jerry";
student_map[103] = " James";
}
void display_details_Engineering()
{
for (map<int, string>::iterator itr = student_map.begin(); ((itr->first) == 100) || ((itr->first) == 101); ++itr) {
cout << "ROll NO" << itr->first << " Name" << itr->second << endl;
}
}
void display_details_Medicine()
{
for (map<int, string>::iterator itr = student_map.begin(); ((itr->first) == 102) || ((itr->first) == 103); ++itr) {
cout << "ROll NO" << itr->first << " Name" << itr->second << endl;
}
}
void display_alldetails()
{
for (map<int, string>::iterator itr = student_map.begin(); itr != student_map.end(); ++itr) {
cout << "ROll NO" << itr->first << " Name" << itr->second << endl;
}
}
~student(){};
};
class subjects {
public:
map<string, int> eng_subjects;
map<string, int> medicine_subjects;
subjects()
{
eng_subjects[" Engineering Physics"] = 90;
eng_subjects[" Engineering Chemistry"] = 80;
medicine_subjects[" Zoology"] = 90;
medicine_subjects[" Humanology"] = 80;
}
void display_details_Engineering()
{
for (map<string, int>::iterator itr = eng_subjects.begin(); itr != eng_subjects.end(); ++itr) {
cout << "Subject Name" << itr->first << " Grades" << itr->second << endl;
}
s1.display_details_Engineering();
}
void display_details_Medicine()
{
for (map<string, int>::iterator itr = medicine_subjects.begin(); itr != medicine_subjects.end(); ++itr) {
cout << "Subject Name" << itr->first << " Grades" << itr->second << endl;
}
s1.display_details_Medicine();
}
~subjects(){};
// void display_details_Engineering() Not Required
//{
//s1.display_details_Engineering();
//}
private:
student s1;
};
int main()
{
student s;
s.display_alldetails();
s.display_details_Engineering();
s.display_details_Medicine();
subjects sub;
sub.display_details_Engineering();
sub.display_details_Medicine();
}
The output is as follows
ROll NO100 Name Noel Philip
ROll NO101 Name John
ROll NO102 Name Jerry
ROll NO103 Name James
ROll NO100 Name Noel Philip
ROll NO101 Name John
Subject Name Engineering Chemistry Grades80
Subject Name Engineering Physics Grades90
ROll NO100 Name Noel Philip
ROll NO101 Name John
Subject Name Humanology Grades80
Subject Name Zoology Grades90
When the required output is:
ROll NO100 Name Noel Philip
ROll NO101 Name John
ROll NO102 Name Jerry
ROll NO103 Name James
ROll NO100 Name Noel Philip
ROll NO101 Name John
ROll NO102 Name Jerry
ROll NO103 Name James
Subject Name Engineering Chemistry Grades80.
Subject Name Engineering Physics Grades90
ROll NO100 Name Noel Philip
ROll NO101 Name John
Subject Name Humanology Grades80
Subject Name Zoology Grades90
ROll NO102 Name Jerry
ROll NO103 Name James
I didn't look closely, but in your "for" loop the middle expression is not a filter, if it doesn't evaluate to boolean true then the for loop stops. So in "display_details_Medicine()", as soon as the itr->first doesn't equal 102 or 103 the loop ends.
Do your comparisons within the body of the methods. Or better yet, create an array of the values for each set and iterate through each set, which is far more efficient than reading every member of the map and selecting only the ones that you want.