用C++评选优秀教师和优秀学生
2014-05-08 13:41
232 查看
//评选优秀教师和优秀学生 #include<iostream.h> #include<string.h> class base//定义基类 { protected: char name[8]; //姓名 int chengji; //chengji 表示学生的成绩 int file; //file 表示论文 public: base(char n[8]) { strcpy(name,n); } base(int ch=0,int f=0)//构造函数,使基类初始化 { chengji=ch; file=f; } virtual bool isgood()=0; //纯虚函数,判断是否优秀 virtual void inputname()=0;// 纯虚函数 ,输入姓名 virtual void inputchji()=0;//纯虚函数,输入成绩 virtual void inputfile()=0;//纯虚函数,输入论文 virtual void show()=0; //纯虚函数,显示优秀的学生和教师 }; class student:public base//定义base派生的student类 { public: student(int ch=0):base(ch,0) { inputname();//调用student的inputname(),输入学生的姓名 inputchji();//调用student的inputchji(),输入学生的成绩 } void inputname() { cout<<"请输入学生的姓名:"; cin>>name; } void inputchji() { cout<<"请输入成绩:"; cin>>chengji; } bool isgood() { if (chengji>90) return true; else return false; } void show()//输出优秀学生的姓名 { if (isgood()) cout<<name<<"是优秀学生"<<endl; } void inputfile(){} }; class teacher:public base//定义base派生的teather类 { public: teacher(int f=0):base(0,f)//初始化基类的成员函数 { inputname();//调用的teacher的inputname(),输入教师的姓名 inputfile();//调用的teacher的inputfile(),输入教师的论文 } void inputname() { cout<<"请输入教师的姓名:"; cin>>name; } void inputfile() { cout<<"请输入此教师一年中的论文:"; cin>>file; } bool isgood()//判断优秀教师的条件 { if (file>3) return true; else return false; } void show() { if (isgood()) cout<<name<<"是优秀教师"<<endl; } void inputchji(){} //由于基类中定义:virtual void inputchji()=0;的纯虚函数,在派生类中必须定义 //否则就会出错,此时这个函数只是形式的需要,在功能方面什么都不做 }; void main()//主函数 { base *p[100];//定义base类的100个对象的指针 int i=0; int cut=0; int type=0; cout<<"请输入要评选的人数:"; cin>>cut;//接收输入的评选的人数 for(i=0;i<cut;i++)//i是计数器 { cout<<"请选择教师(1)或学生(2):"; cin>>type;//输入的是教师(1)还是学生(2) if (type==1)//判断 p[i]=new teacher;//定义teacher新对象 else p[i]=new student;//定义student新对象 } for(i=0;i<cut;i++) { p[i]->show();调用 delete p[i];//删除对象 } }
相关文章推荐
- 用c++实现打印三角形
- C++11 新特性练习
- C语言从控制台输入一个字符串,然后赋值给变量
- 【C语言】08-数组
- 单例模式(Singleton)的创建
- 【POJ】1276 Cash Machine(多重背包)
- C/C++中的转义字符
- Python源码 -- C语言实现面向对象编程(基类&派生类&多态)
- kmp算法C++源码实现
- c++ 时间类型详解 time_t
- C++ 1
- QML 与 C++(Qt)通过信号和槽进行交互
- 【C语言学习】02__auto,static,register分析
- 安装Symantec Backup Exec报“microsoft visual C++ 2005 SP1 command line syntax error.”
- C获取数组长度
- Lambda Functions in C++11 - the Definitive Guide
- 【C语言学习】01__基本数据类型分析
- C++ Primer 学习笔记(一)
- 【C++系列15】c++的正无穷和负无穷
- thrift在windows的编译/安装--c++版