求C++编程题代码
下面给出了基类person的主要成员:
(1) 私有成员:
int no;编号
char name[10]; 姓名
(2) 公有成员:
void input(); 编号和姓名的输入
void display(); 编号和姓名的显示 展开
CODE:
class Person
{
private:
int number;
//The name can use string in c++
string name;
public:
void input()
{
cout<<"Please input the number and the name: "<<endl;
cin>>number>>name;
}
void display()
{
cout<<"The message you input is: ";
cout<<number<<", "<<name<<endl;
}
};
class Student : public Person
{
private:
int classNumber;
double grade;
public:
void inputStudentMessage()
{
cout<<"OK, the classNumber and the grade: "<<endl;
cin>>classNumber>>grade;
}
void displayStudentMessage()
{
cout<<"And the student's class number and grade are: ";
cout<<classNumber<<", "<<grade<<endl<<endl;
}
};
class Teacher : public Person
{
private:
string position;
string department;
public:
void inputTeacherMessage()
{
cout<<"OK, the position and the department: "<<endl;
cin>>position>>department;
}
void displayTeacherMessage()
{
cout<<"And the teacher's position and department are: ";
cout<<position<<", "<<department;
}
};
void main()
{
//Student testing
cout<<"Student testing ----------------------------------"<<endl;
Student s;
s.input();
s.inputStudentMessage();
s.display();
s.displayStudentMessage();
//Teacher testing
cout<<"Teacher testing ----------------------------------"<<endl;
Teacher t;
t.input();
t.inputTeacherMessage();
t.display();
t.displayTeacherMessage();
}
THE RESULT:
using namespace std;
class Person
{
int no;//编号
char name[10];//姓名
public:
void intput();
void display();
};
void Person::intput()
{
cout<<"请输入编号和姓名,以空格隔开"<<endl;
cin>>no>>name;
}
void Person::display()
{
cout<<"编号:"<<no<<"姓名:"<<name<<endl;
}
class Student :public Person
{
int cno;//班号
float score1;//成绩1
float score2;//成绩2
float score3;//成绩3
public:
void Sinput();
void Sshow();
};
void Student::Sinput()
{
cout<<"请输入班号和三科成绩,以空格间开"<<endl;
cin>>cno>>score1>>score2>>score3;
}
void Student::Sshow()
{
cout<<"班号:"<<cno<<"三科成绩:"<<score1<<" "<<score2<<" "<<score3<<endl;
}
class Teacher :public Person
{
char post[10];//职称
char department[20];//部门
public:
void Tinput();
void Tshow();
};
void Teacher::Tinput()
{
cout<<"请输入教师职称、部门,以空格间开"<<endl;
cin>>post>>department;
}
void Teacher::Tshow()
{
cout<<"职称:"<<post<<"部门:"<<department<<endl;
}
int main()
{
cout<<"学生信息"<<endl;
Student std;
std.intput();
std.Sinput();
std.display();
std.Sshow();
cout<<"教师信息"<<endl;
Teacher tech ;
tech.intput();
tech.Tinput();
tech.display();
tech.Tshow();
getchar();//查看结果
return 0;
}
using namespace std;
class person
{
person();
~person();
private:
int no;\\编号
char name[10];\\ 姓名
public:
void input() \\编号和姓名的输入
{
cout<<“please input your no!”<<endl;
cin>>no;
cout<<"please input your name!"<<endl;
cin>>name;
}
void display() \\编号和姓名的显示
{
cout<<no<<endl;
cout<<name<<endl;
}
}
class student :public person
{
private:
char classname[20];
int scores;
public:
void student_input() \\输入
{
input();
cout<<“please input your classname!”<<endl;
cin>>classname;
cout<<"please input your scores!"<<endl;
cin>>scores;
}
void student_display() \\显示
{
display();
cout<<classname<<endl;
cout<<scores<<endl;
}
}
class teacher :public person
{
private:
char position[20];
char department[20];
public:
void teacher_input() \\输入
{
input();
cout<<“please input your position!”<<endl;
cin>>position;
cout<<"please input your department!"<<endl;
cin>>department;
}
void teacher_display() \\显示
{
display();
cout<<position<<endl;
cout<<department<<endl;
}
}
int main()
{
class student stu;
class teacher tea;
stu.student_input();
stu.student_display();
tea.teacher_input();
tea.teacher_display();
return 0;
}
直接写的代码,没有编译测试。可能有些标点会不正确。你自己测试下。
测试有一个错误。但我太菜了。实在找不出哪里错了。麻烦帮下忙
额 我编译下 调试下
你QQ多少,我发文件给你