姓名”、“性别”和“年龄”是教师、学生共有的属性,“班级”和“成绩”是学生特有的,“职称”和“工资”是教师特有的属性。请用类的继承模拟。
#include <iostream>
using namespace std;
#include <string>
class schooler
{
protected:
string name;
int age;
string sex;
public:
schooler() {}
schooler(string n,int a,string s) : name(n),age(a),sex(s) {cout<<"schooler"<<endl;}
virtual void show()
{
cout<<"姓名:"<<name<<endl;
cout<<"年龄:"<<age<<endl;
cout<<"性别:"<<sex<<endl;
}
};
class student : public schooler
{
protected:
string classes;
float score;
public:
student() {}
student(string n,int a,string s,string c,float sc) : schooler(n,a,s),classes(c),score(sc) {cout<<"student"<<endl;}
void show()
{
schooler::show();
cout<<"班级:"<<classes<<endl;
cout<<"成绩:"<<score<<endl;
}
};
class teacher : public schooler
{
protected:
string zc;
float sal;
public:
teacher() {}
teacher(string n,int a,string s,string z,float sa): schooler(n,a,s),zc(z),sal(sa) {cout<<"teacher"<<endl;}
void show()
{
schooler::show();
cout<<"职称:"<<zc<<endl;
cout<<"薪资:"<<sal<<endl;
}
};
int main()
{
schooler *pStu = new student("张三",18,"男","高二(1)班",98);
pStu->show();
schooler *pTea = new teacher("王老师",38,"女","高级教师",8500);
pTea->show();
}
#include<iostream.h>
#include<string.h>
class person
{
public:
person (char *a,char *b,int s)
{
name=a;
sex=b;
score=s;
}
void display()
{
cout<<"姓名:"<<name<<endl;
cout<<"性别:"<<sex<<endl;
cout<<"年龄:"<<score<<endl;
}
private:
char *name ;
char *sex ;
int score;
};
class student:public person
{
public:
student(char *a,char *b,int s,char *c,float s1):person(a,b,s)
{
cla=c;
score=s1;
}
void display1()
{
cout<<"学生:"<<endl;
display();
cout<<"班级:"<<cla<<endl;
cout<<"成绩:"<<score<<endl;
cout<<endl;
}
private:
char *cla ;
float score;
};
class teacher: public person
{
public:
teacher(char *a,char *b,int s,char *p,int n,int sa):person(a,b,s)
{
post=p;
num=n;
salary=sa;
}
void display2()
{
cout<<"教师:"<<endl;
display();
cout<<"工号:"<<num<<endl;
cout<<"职称:"<<post<<endl;
cout<<"工资:"<<salary<<endl;
cout<<endl;
}
private: char *post ;
int num;
int salary;
};
void main()
{
teacher g1("李昂","男",20, "教师",1001,3000);
g1.display2();
student s1("李昂","男",20,"软件13",86);
s1.display1 ();
}