删除的那个函数要怎么实现啊。
某大学招收专科生、本科生、研究生若干名。所有学生必须登记学号、姓名、性别、出生日期、身高和体重。本科生和专科生须填写高考总成绩和专业代码,其中研究生还要单独填写研究生须填...
某大学招收专科生、本科生、研究生若干名。所有学生必须登记学号、姓名、性别、出生日期、身高和体重。本科生和专科生须填写高考总成绩和专业代码,其中研究生还要单独填写研究生须填写面试成绩(5分制,A——E代表)和其选择的导师姓名。
编制一个程序,完成如下功能:
(1)为每个新生登记个人信息。
(2)根据指定的信息删除已经存在的学生信息。
(3)显示已经存在的学生信息。
注意:输入、删除、显示信息时,应对不同类型的学生加以区别。要求定义一个学生类为基类,每种类型的学生类都在此基类上进行继承和派生。
#include "iostream"
#include "string"
using namespace std;
class Student
{
public:
Student(int num,string nam,char se,string bir,double h,double w)
{
number=num;
name=nam;
sex=se;//性别
birthday=bir;
hight=h;
weight=w;
}
void show();
private:
int number;
string name;
char sex;
string birthday;
double hight;
double weight;
};
void Student::show()
{
cout<<"number:"<<number<<endl;
cout<<"name:"<<name<<endl;
cout<<"sex:"<<sex<<endl;
cout<<"birthday:"<<birthday<<endl;
cout<<"hight:"<<hight<<"cm"<<endl;
cout<<"weight:"<<weight<<"kg"<<endl;
}
class Specialized:public Student //专科生
{
public:
Specialized(int num,string nam,char se,string bir,double h,double w,double sco,long int co):Student(num,nam,se,bir,h,w)
{
score=sco;
code=co;
}
void Specialized_show();
void Specialized_deltet();
private:
double score;
int code;
};
void Specialized::Specialized_show()
{
show();
cout<<"score:"<<score<<endl;
cout<<"code:"<<code<<endl;
cout<<endl;
}
class Undergraduates:public Student //本科生
{
public:
Undergraduates(int num,string nam,char se,string bir,double h,double w,double sco,long int co):Student(num,nam,se,bir,h,w)
{
score=sco;
code=co;
}
void Undergraduates_show();
private:
double score;
int code;
};
void Undergraduates::Undergraduates_show()
{
show();
cout<<"score:"<<score<<endl;
cout<<"code:"<<code<<endl;
cout<<endl;
}
class Graduate:public Student //研究生
{
public:Graduate(int num,string nam,char se,string bir,double h,double w,char in,string tea):Student(num,nam,se,bir,h,w)
{
interview=in;
teacher=tea;
}
void Graduate_show();
private:
char interview;
string teacher;
};
void Graduate::Graduate_show()
{
show();
cout<<"interview:"<<interview<<endl;
cout<<endl;
}
int main()
{
Specialized s1(2001,"wang",'f',"1995.01.10",159,45,355,1026);
Specialized s2(2002,"li",'m',"1993.03.08",188,75,301,1100);
Undergraduates u1(3001,"mike",'m',"1992.02.25",175,60,501,3500);
Undergraduates u2(3002,"rose",'f',"1992.05.30",165,50,533,3351);
Graduate g1(4001,"marry",'f',"1993.06.21",161,46,'C',"jin");
Graduate g2(4002,"luo",'m',"1995.01.26",175,70,'A',"chen");
s1.Specialized_deltet();
s1.Specialized_show();
s2.Specialized_show();
u1.Undergraduates_show();
u2.Undergraduates_show();
g1.Graduate_show();
g2.Graduate_show();
return 0;
} 展开
编制一个程序,完成如下功能:
(1)为每个新生登记个人信息。
(2)根据指定的信息删除已经存在的学生信息。
(3)显示已经存在的学生信息。
注意:输入、删除、显示信息时,应对不同类型的学生加以区别。要求定义一个学生类为基类,每种类型的学生类都在此基类上进行继承和派生。
#include "iostream"
#include "string"
using namespace std;
class Student
{
public:
Student(int num,string nam,char se,string bir,double h,double w)
{
number=num;
name=nam;
sex=se;//性别
birthday=bir;
hight=h;
weight=w;
}
void show();
private:
int number;
string name;
char sex;
string birthday;
double hight;
double weight;
};
void Student::show()
{
cout<<"number:"<<number<<endl;
cout<<"name:"<<name<<endl;
cout<<"sex:"<<sex<<endl;
cout<<"birthday:"<<birthday<<endl;
cout<<"hight:"<<hight<<"cm"<<endl;
cout<<"weight:"<<weight<<"kg"<<endl;
}
class Specialized:public Student //专科生
{
public:
Specialized(int num,string nam,char se,string bir,double h,double w,double sco,long int co):Student(num,nam,se,bir,h,w)
{
score=sco;
code=co;
}
void Specialized_show();
void Specialized_deltet();
private:
double score;
int code;
};
void Specialized::Specialized_show()
{
show();
cout<<"score:"<<score<<endl;
cout<<"code:"<<code<<endl;
cout<<endl;
}
class Undergraduates:public Student //本科生
{
public:
Undergraduates(int num,string nam,char se,string bir,double h,double w,double sco,long int co):Student(num,nam,se,bir,h,w)
{
score=sco;
code=co;
}
void Undergraduates_show();
private:
double score;
int code;
};
void Undergraduates::Undergraduates_show()
{
show();
cout<<"score:"<<score<<endl;
cout<<"code:"<<code<<endl;
cout<<endl;
}
class Graduate:public Student //研究生
{
public:Graduate(int num,string nam,char se,string bir,double h,double w,char in,string tea):Student(num,nam,se,bir,h,w)
{
interview=in;
teacher=tea;
}
void Graduate_show();
private:
char interview;
string teacher;
};
void Graduate::Graduate_show()
{
show();
cout<<"interview:"<<interview<<endl;
cout<<endl;
}
int main()
{
Specialized s1(2001,"wang",'f',"1995.01.10",159,45,355,1026);
Specialized s2(2002,"li",'m',"1993.03.08",188,75,301,1100);
Undergraduates u1(3001,"mike",'m',"1992.02.25",175,60,501,3500);
Undergraduates u2(3002,"rose",'f',"1992.05.30",165,50,533,3351);
Graduate g1(4001,"marry",'f',"1993.06.21",161,46,'C',"jin");
Graduate g2(4002,"luo",'m',"1995.01.26",175,70,'A',"chen");
s1.Specialized_deltet();
s1.Specialized_show();
s2.Specialized_show();
u1.Undergraduates_show();
u2.Undergraduates_show();
g1.Graduate_show();
g2.Graduate_show();
return 0;
} 展开
1个回答
展开全部
把你的代码改些地方,添加一个学校类.
#include "iostream"
#include "string"
#include <vector>
using namespace std;
//学生基类
class Student
{
public:
Student() {}/////////////////
Student(Student & s)///////////
{
number = s.number ;
name = s.name ;
sex = s.sex ;
birthday = s.birthday ;
hight = s.hight ;
weight = s.weight ;
}
Student(int num, string nam, char se, string bir, double h, double w)
{
number = num;
name = nam;
sex = se; //性别
birthday = bir;
hight = h;
weight = w;
}
friend class School;/////////////////
virtual void show() = 0; /////////////////
virtual ~Student() {} /////////////////
private:
int number;
string name;
char sex;
string birthday;
double hight;
double weight;
};
void Student::show()/////////////////
{
cout << "学号: " << number << "\t姓名: " << name << "\t性别:" << sex << endl;
cout << "身高: " << hight << "cm" << "\t体重: " << weight << "kg" << "\t生日: " << birthday << endl;
}
//专科生
class Specialized: public Student
{
public:
Specialized() {}/////////////////
Specialized(Specialized & s): Student(s)/////////////////
{
score = s.score ;
code = s.code ;
}
Specialized(int num, string nam, char se, string bir, double h, double w, double sco, long int co)
: Student(num, nam, se, bir, h, w)
{
score = sco;
code = co;
}
void show();
//void Specialized_deltet();/////////////////
private:
double score;
int code;
};
void Specialized::show()/////////////////
{
Student::show();
cout << "score:" << score << "\tcode:" << code << endl << endl;
}
//本科生
class Undergraduates: public Student
{
public:
Undergraduates() {}/////////////////
Undergraduates(Undergraduates & s): Student(s)/////////////////
{
score = s.score ;
code = s.code ;
}
Undergraduates(int num, string nam, char se, string bir, double h, double w, double sco, long int co)
: Student(num, nam, se, bir, h, w)
{
score = sco;
code = co;
}
void show();
private:
double score;
int code;
};
void Undergraduates::show()/////////////////
{
Student::show();
cout << "score:" << score << "\tcode:" << code << endl << endl;
}
//研究生
class Graduate: public Student
{
public:
Graduate() {}/////////////////
Graduate(Graduate & s) : Student(s)/////////////////
{
interview = s.interview ;
teacher = s.teacher ;
}
Graduate(int num, string nam, char se, string bir,
double h, double w, char in, string tea): Student(num, nam, se, bir, h, w)
{
interview = in;
teacher = tea;
}
void show();
private:
char interview; string teacher;
};
void Graduate::show()/////////////////
{
Student::show();
cout << "interview:" << interview << endl << endl;
}
/////////////////
class School/////////////////
{
public:
vector<Student*> students;
void add(Specialized & sd)
{
Specialized* stud = new Specialized(sd);
students.push_back(stud);
}
void add(Undergraduates & sd)
{
Undergraduates* stud = new Undergraduates(sd);
students.push_back(stud);
}
void add(Graduate & sd)
{
Graduate* stud = new Graduate(sd);
students.push_back(stud);
}
void del(int num)
{
for(vector<Student*>::iterator p = students.begin(); p != students.end(); p++)
{
if((*p)->number == num)
{
delete *p;
students.erase(p);
return;
}
}
}
~School()
{
for(vector<Student*>::iterator p = students.begin(); p != students.end(); p++)
delete(*p);
}
void show()
{
for(vector<Student*>::iterator p = students.begin(); p != students.end(); p++)(*p)->show() ;
}
};
void fn()//测试析构.
{//add 添加学生 ,添加方式看下面. del 删除学生.show 显示学校所有学生.
School school;
school.add(Specialized(2001, "wang", 'f', "1995.01.10", 159, 45, 355, 1026));
school.add(Specialized(2002, "li", 'm', "1993.03.08", 188, 75, 301, 1100));
school.add(Undergraduates(3001, "mike", 'm', "1992.02.25", 175, 60, 501, 3500));
school.add(Undergraduates(3002, "rose", 'f', "1992.05.30", 165, 50, 533, 3351));
school.add(Graduate(4001, "marry", 'f', "1993.06.21", 161, 46, 'C', "jin"));
school.add(Graduate(4002, "luo", 'm', "1995.01.26", 175, 70, 'A', "chen"));
school.show();
school.del(3002);
school.show();
}
int main()
{ fn(); cin.get(); return 0;}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询