help me to understand this code.

plz help me to understand this code. how can i solve this code? all details in main() function with commenting out.


#include<iostream>
#include<fstream>
using namespace std;
int sz=4;
class Person{
protected:
int age;
int id;
int type;
public:
Person(){
age=0;id=0;type=0;
//cout<<"Person Created"<<endl;
}
Person(int a,int i,int t){
age=a;
id=i;
type=t;
//cout<<"Person Created"<<endl;
}
int getType(){return type;}
void setType(int t){type=t;}
void setID(int i){id=i;}
void setAge(int i){age=i;}
virtual void print()=0; //it makes the class abstract
};
class Student:public Person{ //derived class
private: float cgpa;
public:
Student():Person(0,0,1){
cgpa=0.0;
cout<<"Student Created"<<endl;
}
Student(int a,int i,float c):Person(a,i,1){
cgpa=c;
cout<<"Student Created"<<endl;
}
void setCGPA(float cg){cgpa=cg;}
void showCGPA(){cout<<cgpa<<endl;}
void print(){
cout<<"\nStudent Details - Person Type : "<<getType()<<endl;
cout<<"Age "<<age<<endl;
cout<<"CGPA "<<cgpa<<endl;
cout<<"ID "<<id<<endl;
}
};
class Officer:public Person{
int joinYear;
int salary;
public:
Officer():Person(0,0,2){joinYear=2018;salary=0;}
Officer(int a,int i,int jy,int sal):Person(a,i,2){
joinYear=jy;
salary=sal;
}
void print(){
cout<<"\nOfficer Details - Person Type : "<<getType()<<endl;
cout<<"ID : "<<id<<endl;
cout<<"Age : "<<age<<endl;
cout<<"Experience : "<<2018-joinYear<<" years"<<endl;
cout<<"Salary : "<<salary<<endl;
}
};
void display(Person *pt[]){
for(int i=0;i<sz;i++){
pt[i]->print();
}
}
void init(Person *ar[]){
int i;
for(i=0;i<sz-2;i++){
ar[i]=new Student(95+i,123+i+2,3.99);
}
for(i=2;i<sz;i++){
ar[i]=new Officer(95+i,123+i+2,3.99,100+i);
}
}
void loadStudentData(Person *ar[]){
ifstream fin;fin.open("student.txt");
int age,id;float cgpa;
for(int i=0;i<sz-2;i++){ //it should work for any number of lines in the file
fin>>age>>id>>cgpa;
ar[i]=new Student(age,id,cgpa);
}
}
void loadOfficerData(Person *ar[]){
ifstream fin;fin.open("officer.txt");
int age,id,salary;float jy;
for(int i=2;i<sz;i++){ //it should work for any number of lines in the file
fin>>age>>id>>jy>>salary;
cout<<jy;
ar[i]=new Officer(age,id,jy,salary);
}
}
int main(){
Person *ar[sz];

//place the following functions into a seperate class with a relevant name
loadStudentData(ar); //change the function to work for any number of lines in the file
loadOfficerData(ar); //change the function to work for any number of lines in the file
display(ar);

/*

//place the following functions into a seperate class with a relevant name
searchHighestPaidOfficer(ar); //only one Officer should be the highest paid based on salary
showAllOfficerJoinedIn(ar,1998);
showOfficerByExperienceAbove(ar,10) //show all officers who are having 10 years above experience
showOfficerByID(ar,223);

//place the following functions into a seperate class with a relevant name
searchBestStudent(ar); //only one student should be the best based on CGPA
printAllStudentNamesAndDepartment();
showStudentByID(ar,123);

//place the following functions into a seperate class with a relevant name
showAnyPersonByID(ar,123);
searchAnyPersonByName(ar,"abc"); //search and call print function for all the persons named "abc"
printAll(ar,"Officer"); //this function prints record according to the second paramater
printAll(ar,"Student"); //this function prints record according to the second paramater

*/

//ABOVE FUNCTIONS SHOULD BE CALLED ACCORDING TO THE USERS CHOICE ENTERED THROUGH KEYBOARD
return 0;
}
Last edited on
Please put your code in the source code tags so the indentations are formatted and it's pleasant to look at.

Does it compile? What are you trying to do? What specifically don't you understand?
Topic archived. No new replies allowed.