using namespace std;
class Publication
{
private:
char title[10],name[10];
char date[10];
double price;
public:
Publication(){};
~Publication(){};
void inputData();
void display();
};
void Publication::inputData()
{
cout<<"请输入标题:"<<endl;
cin>>title;
cout<<"请输入出版物名称:"<<endl;
cin>>name;
cout<<"请输入单价:"<<endl;
cin>>price;
cout<<"请输入出版日期:"<<endl;
cin>>date;
}
void Publication::display()
{
cout<<"标题 名称 单价 出版日期"<<endl;
cout<<title<<'\t'<<name<<'\t'<<price<<'\t'<<date<<endl;
}
class Book:public Publication
{
private:
int page;
public:
Book(){};
~Book(){};
void inputData()
{
Publication::inputData();
cout<<"请输入图书页数:"<<endl;
cin>>page;
}
void display()
{
Publication::display();
cout<<"页数:"<<endl;
cout<<page<<endl;
}
};
class Tape:public Publication
{
private:
int playtime;
public:
Tape(){};
~Tape(){};
void inputData()
{
Publication::inputData();
cout<<"请输入播放时间:"<<endl;
cin>>playtime;
}
void display()
{
Publication::display();
cout<<"播放时间"<<endl;
cout<<playtime<<endl;
}
};
void main()
{
Publication a;
a.inputData();
a.display();
Book b;
b.inputData();
b.display();
Tape c;
c.inputData();
c.display();
}
2.利用继承设计学生与老师的类
#include<iostream>
using namespace std;
class Person
{
private:
char name[10];
int age;
char sex;
public:
void inputData()
{
cout<<"请输入姓名:"<<endl;
cin>>name;
cout<<"请输入年龄:"<<endl;
cin>>age;
cout<<"请输入性别:"<<endl;
cin>>sex;
}
void display()
{
cout<<"姓名 年龄 性别"<<endl;
cout<<name<<"\t"<<age<<"\t"<<sex<<"\t"<<endl;
}
};
class Teacher:public Person
{
private:
int number;
char jc[10],xb[10];
public:
void inputData()
{
Person::inputData();
cout<<"请输入教师编号:"<<endl;
cin>>number;
cout<<"请输入职称:"<<endl;
cin>>jc;
cout<<"请输入系别:"<<endl;
cin>>xb;
}
void display()
{
Person::display();
cout<<"教师编号 职称 系别"<<endl;
cout<<number<<"\t "<<jc<<"\t"<<xb<<endl;
}
};
class Student:public Person
{
private:
int sign;
char bj[10];
int chinese,math,english;
public:
void inputData()
{
Person::inputData();
cout<<"请输入学号:"<<endl;
cin>>sign;
cout<<"请输入班级:"<<endl;
cin>>bj;
cout<<"请输入语文:"<<endl;
cin>>chinese;
cout<<"请输入数学:"<<endl;
cin>>math;
cout<<"请输入英语:"<<endl;
cin>>english;
}
void display()
{
Person::display();
cout<<"学号 班 级 语文 数学 英语"<<endl;
cout<<sign<<"\t"<<bj<<"\t"<<chinese<<"\t"<<math<<"\t"<<english<<endl;
}
};
void main()
{
Teacher a;
a.inputData();
a.display();
Student b;
b.inputData();
b.display();
}