Firstly create a Person base class
#ifndef PERSON_H
#define PERSON_H
#include <string>
using namespace std;
class Person{
private:
int age,gender;
double height;
string name;
public:
Person();
virtual ~Person();
void set_age(int);
int get_age();
int get_gender();
void set_height(double);
void set_gender(int);
double get_height();
string getName();
void setName(string);
};
#endif // PERSON_H
here age ,gender is private and gender 1 represent female while gender 0 represent male
A student class
#ifndef STUDENT_H
#define STUDENT_H
#include"Person.h"
class student: public Person
{
private:
double oop_grade;
string id;
public:
student *next;
static int student_num;
student();
~student();
student(string name,int age,int gender,double height,string id,double oop_grade);
bool operator<(const student& a)const;
void setStudentGrade(double grade);
double getStudentOOPgrede()const;
string getID();
protected:
};
#endif // STUDENT_H
student.cpp
#include "student.h"
#include<iostream>
student::student(){}
student::~student(){
}
int student::student_num=0;
student::student(string name,int age,int gender,double height,string id,double oop_grade)
{
//name=name;//wrong because name is private!!
setName(name);//use -> not .
set_age(age);
set_gender(gender);
set_height(height);
this->oop_grade=oop_grade;//this doesn't need to setgrade function ,but need this-> //oop_grade=oop_grade wrong it is ambiguous
this->id=id;
}
void student::setStudentGrade(double grade)
{
oop_grade=grade;
}
double student::getStudentOOPgrede()const
{
return oop_grade;
}
bool student::operator<(const student&a)const
{
return oop_grade<a.getStudentOOPgrede();
}
string student::getID()
{
return id;
}
However in student constructor I use name=name;
it is wrong for two reason
1. name is private in Person class .I should use setName
2. Even name is public, I can't use name=name.Because the compiler can't know what name is about,and it is ambiguous
may be i can use name=newName ,the different parameter name
Attention!
bool student::operator<(const student&a)const
{
return oop_grade<a.getStudentOOPgrede();
}
1. bool student::
2. a.getStudentOOPgrede() not a. oop_grade
the main
#include <iostream>
#include <string>
#include <fstream>
#include <vector>
#include <stdio.h>
#include <windows.h>
#include "Person.h"
#include "student.h"
using namespace std;
string name;
int age,gender;
string id;
double oop_grade, height;
student* head=new student(); //create a new student but not student head because i want it exist in the whole program
typedef student* stu; //define a student pointer as stu which is more convenient
void inputStudent()
{
string file;
int k=1;
cout<<"Enter a file name:"<<endl;
cin>>file;
ifstream input(file.c_str());//ifstream ,input and change the string into cstring
if(input.fail()) cout<<"File doesn's exist"<<endl;
else
{
stu q; //the q is only used to be a pointer it is not really a student
head->next=NULL;
q=head;
while(!input.eof())
{
input>>name>>age>>gender>>height>>id>>oop_grade;
student* p=new student();
*p=student(name,age,gender,height,id,oop_grade);
q->next=p;
q=p;
student::student_num++;
}
q->next=NULL;//Don't forget NULL
cout<<"Successfully input"<<endl
<<"There are "<<student::student_num<<" students"<<endl;
}
}
void modifyStudentInfo()
{
int num;
string name;
string id;
cout<<"Please choose:"<<endl
<<"1.Add student"<<endl
<<"2.Delete student"<<endl
<<"3.Modify student's information"<<endl;
cin>>num;
switch(num)
{
case 1:
{
int i;
cout<<"Please enter student's name,age,gender,height ,id and oop_grade in order"<<endl;
cin>>name>>age>>gender>>height>>id>>oop_grade;
stu newStudent=new student();
*newStudent=student(name,age,gender,height,id,oop_grade); //Don't forget the * pointer
stu p=head->next;//the p is only used to be a pointer it is not really a student
while(p->next) p=p->next;
p->next=newStudent;
newStudent->next=NULL;
student::student_num++;
cout<<"Add successfully"<<endl
<<"There are "<<student::student_num<<" students"<<endl;
}
break;
case 2:
{
cout<<"Please enter student's name"<<endl;
cin>>name;
stu before,q;
q=head->next;
while(q&&q->getName()!=name)
{
before=q;
q=q->next;
}
if(!q) cout<<"This student does's exist"<<endl;
else
{
delete before->next;//not delete q but before->next because before->next is new
before->next=q->next;
student::student_num--;
cout<<"Delete successfully"<<endl
<<"There are "<<student::student_num<<" students"<<endl;
}
}
break;
case 3:
{
int n,nValue;
stu p;
cout<<"Please enter student's name :"<<endl;
cin>>name;
p=head->next;
while(p!=NULL&&p->getName()!=name)
{
p=p->next;
}
if(p==NULL)
{
cout<<"The student does'n exist"<<endl;
}
else
{
cout<<"What do you want to modify:"<<endl
<<"1.oop_grade"<<endl
<<"2.height"<<endl
<<"3.age"<<endl
<<"4.no,thank"<<endl;
cin>>n;
cout<<"Please enter new value:"<<endl;
cin>>nValue;
switch(n)
{
case 1:
{
p->setStudentGrade(nValue);
}
break;
case 2:
{
p->set_height(nValue);
}
break;
case 3:
{
p->set_age(nValue);
}
break;
}
cout<<"Modify successfully"<<endl;
}
}
}
}
void getStudentInfo()
{
int n;
string name;
cout<<"Please enter student's name"<<endl;
cin>>name;
stu j;
j=head->next;
while(j&&j->getName()!=name)
{
j=j->next;
}
if(j==0)
{
cout<<"The student does'n exist"<<endl;
}
else
{
cout<<"student "<<j->getName()<<":"<<endl
<<"id: "<<j->getID()<<endl
<<"age: "<<j->get_age()<<endl
<<"gender: "<<j->get_gender()<<endl
<<"height: "<<j->get_height()<<endl
<<"oop grade: "<<j->getStudentOOPgrede()<<endl;
}
}
//Using insert sorting algorithm
void sortStudent()
{
stu p,q,before,pt;
//p point to the first student
p=head->next;
//q point to the second student
q=p->next;
//make the first student become isolated so we can move it and insert anywhere
p->next=NULL;
while(q)
{
//q is the student we want to sort
//pt is just a temporary student pointer represent q
pt=q;
//this statement is important if i do not put it here ,it will get wrong
q=q->next;
//before is used to record the previous student before studentA
before=head;
//p is the student used to compare with pt
p=head->next;
//find a studentA whose opp grade is smaller than pt's opp grade
while(p&&(*pt<*p))//Attention!! do not use > because i only overload < no > //and it is *pt not pt
{
before=p;
p=p->next;
}
//Found studentA and insert pt before studentA
before->next=pt;
pt->next=p;
}
int i=1;
q=head->next;
while(q)
{
cout<<i<<" "<<q->getName()<<" : "<<q->getStudentOOPgrede()<<endl;
q=q->next;
i++;
}
}
int main()
{
while(1)
{
system("cls");
int input;
cout<<"Welcome!"<<endl
<<"Please choose:"<<endl
<<"1.Input student information by txt file"<<endl
<<"2.Modify student information "<<endl
<<"3.Get student's information "<<endl
<<"4.sort students' oop grades"<<endl;
cin>>input;
system("cls");
switch(input)
{
case 1:
inputStudent();
break;
case 2:
modifyStudentInfo();
break;
case 3:
getStudentInfo();
break;
case 4:
sortStudent();
break;
}
Sleep(3000);
}
return 0;
}