编写一个学生和教师数据输入和显示程序,学生数据有编号、姓名、班号和成绩,教师数据有编号、姓名、职称和部门。要求将编号、姓名输入和显示设计成一个类person,并作为学生数据操作类student和教师数据操作类teacher的基类。
#include <iostream>
#include <string>
using namespace std;
class person
{
private:
int no;
string name;
public:
void input()
{
cout<<"请输入编号和姓名:";
cin>>no>>name;
}
void display()
{cout<<"编号: "<<no<<endl;
cout<<"姓名:"<<name<<endl;}
};
class student:public person
{private:
int bh;
int score;
public:
void get() {
input();
cout<< "请输入班号和成绩:";
cin>>bh>>score;
}
void show()
{
display();
cout<<"班号:"<<bh<<endl;
cout<<"成绩: "<<score<<endl;}
};
class teacher:public person
{
private:
string title,department;
public:
void get()
{
input();
cout<< "请输入职称和部门:";
cin>>title>>department;
}
void show( )
{
display();
cout<<"职称:"<<title<<endl;
cout<<"部门:"<<department<<endl;}
};
int main( )
{
student s;
teacher t;
s.get( );
s.show( );
t.get( );
t.show( );
return 0; }
设计一个虚基类base,包括姓名和年龄私有数据成员以及相关的成员函数;由它派生出领导类leader,包含职务和部门私有数据成员以及相关的成员函数;再由base派生出工程师类engieer,包含职称和专业私有数据成员以及相关的成员函数;然后由leader和engieer,派生出主任工程师类chairman.采用相关数据进行测试。
#include<iostream>
#include<string>
using namespace std;
class base
{
public:
base(string name1,int age1)
{ name=name1;
age=age1;
}
void print()
{
cout<<"姓名:"<<name<<endl;
cout<<"年龄:"<<age<<endl;
}
protected:
string name;
int age;
};
class leader:virtual public base
{
public:
leader(string name1,int age1,string job1,string dep1):base(name1,age1)
{job=job1;
dep=dep1;
}
void print()
{
base::print();
cout<<"职务:"<<job<<endl;
cout<<"部门:"<<dep<<endl;
}
protected:
string job;
string dep;
};
class engineer:virtual public base
{public:
engineer(string name1,int age1,string prof1,string major1):base(name1,age1)
{prof=prof1;
major=major1;
}
void print()
{
base::print();
cout<<"职称:"<<prof<<endl;
cout<<"专业:"<<major<<endl;
}
protected:
string prof;
string major;
};
class chairman:public leader,public engineer
{chairman(string name1,int age1,string job1,string dep1,string prof1,string major1)
:base(name1,age1),leader(name1,age1,job1,dep1),engineer(name1,age1,prof1,major1)
{;}
void print()
{
leader::print();
engineer::print();
}
};
void main()
{
chairman my_chairman("lili","22","laoshi","jiaowuchu","gaojijiaoshi","huaxue");
my_chairman.print();
}