剩下一个成员函数不会写
- C/C++ code
/*
设计评选优秀教师和学生的程序
先定义一个基类CBase,类中有保护成员姓名:name[10],虚函数isgood()
由CBase派生两个类:student和teacher
在student中定义私有成员学号stunumber和考试成绩score1、score2、score3
定义setscore()设置三门成绩的值,并定义isgood()函数
如果学生平均成绩大于90分且最低分不低于85分,则isgood()返回ture,否则返回false
在teacher类中定义教工号teanumber和论文名称数组Paper[]
定义SetPaper()输入论文题目保存到Paper[]中
再定义isgood()函数
如果论文数目多于三篇则isgood()返回ture,否则返回false
最后设计完整的程序进行演示,输出评选结果
*/
#include <iostream.h>
#include <string.h>
class cbase
{
protected:
char name[10];
public:
cbase(char *p)
{
strcpy(name,p);
}
virtual void isgood(){};
};
class student:public cbase
{
private:
char *stunumber;
float score1,score2,score3;
public:
student(char *name,char *m):cbase(name),stunumber(m){};
//student(char *name,char *m,float x,float y,float z):cbase(name),stunumber(m),setscore(x,y,z){}; ?不知道可否实现
void setscore(float x,float y,float z);
void isgood();
};
class teacher:public cbase
{
private:
char *teanumber;
char paper[];
public:
teacher(char *name,char *m):cbase(name),teanumber(m){};
void setpaper()
{
//这里不会了
}
void isgood()
{
if(n>3)
cout < <"true";
else
cout < <"false";
}
};
void student::isgood()
{
cout < <name < <"\t\t" < <stunumber < <"\t\t";
float aver;
aver=(score1+score2+score3)/3;
if(aver>90&&score1>=85&&score2>=85&&score3>=85)
cout < <"true\n";
else
cout < <"false\n";
}
void student::setscore(float x,float y,float z)
{
score1=x;
score2=y;
score3=z;
}
void main()
{
cout < <"\nstudent:" < <endl < <"name\t\t" < <"number\t\t\t" < <"good\n";
student a("zhang","200603050202");
a.setscore(96,95,85);
a.isgood();
student b("wang","200603050101");
b.setscore(85,85,85);
b.isgood();
cout < <endl < <endl;
}
/*
其中的teacher里面的setpaper不会写了,我想也用student里的setscore相同的方法实现
还有一个问题就是:在初始化student成员列表的时候可不可以像这样student a("zhang","200603050202",85,95,100),直接把85.95.100三个书送给setscore,而不用单独写a.setscore(85,95,100)
谢谢
*/
[解决办法]
代码每行不要太长,注释也是如此。
[解决办法]
这个没区别的,一般说来 一个h文件只定义一个类,不内联的话,实现写在cpp文件里
[解决办法]
[解决办法]
[解决办法]
这篇帖子受益良多,顶起。
[解决办法]
student的带参数的构造函数就可以实现。
我写了一个你看看吧!多文件操作的!
[code=C/C++][/code]
//head.h头文件类的定义
#ifndef BASE_H
#define BASE_H
#include <string>
#pragma warning (disable: 4786)
#include <vector>
using namespace std;
class cbase
{
public:
cbase(string n);
virtual bool isgood()=0;
virtual void display()=0;
protected:
string name;
};
class student:public cbase
{
public:
student(string sn,int m1);
friendbool score_legal(int s);
void setscore();
bool isgood();
void display();
private:
int stundentnum;
int score1;
int score2;
int score3;
};
class teacher:public cbase
{
public:
teacher(string tn,int m2);
void setpaper();
bool isgood();
void display();
private:
int teanumber;
vector<string> paper;
};
#endif
//head.cpp类的实现文件
#include <iostream>
#include "base.h"
using namespace std;
cbase::cbase(string n)
{
name=n;
}
student::student(string sn,int m1):cbase(sn)//构造函数的实现
{
stundentnum=m1;
}
bool score_legal(int s)
{
if(s>=0 && s<=100)
return true;
else
return false;
}
void student::setscore ()
{
bool flag;
do{
cout<<"\n请分别输入3门功课的成绩之间用空格隔开"<<endl;
cin>>score1>>score2>>score3;
if(score_legal(score1) && score_legal(score2) && score_legal(score3))
flag=false;
else
{
cout<<"\n输入有误请重新输入!"<<endl;
flag=true;
}
}while(flag);
}
bool student::isgood ()
{
if((score1+score2+score3)/3>90 && score1>85 && score2>85 && score3>85)
return true;
else
return false;
}
void student::display()
{
if(isgood())
cout<<"\t"<<stundentnum<<"\t"<<name<<endl;
}
teacher::teacher(string tn,int m2):cbase(tn)
{
teanumber=m2;
}
void teacher::setpaper()
{
char flag;
do{
string p1;
cout<<"请输入论文名"<<endl;
cin>>p1;
paper.push_back(p1);
cout<<"\t继续输入请按y"<<endl;
cin>>flag;
}
while(flag=='y');
}
bool teacher::isgood()
{
if(paper.size()>3)
return true;
else
return false;
}
void teacher::display()
{
if(isgood())
cout<<"\t"<<teanumber<<"\t"<<name<<endl;
}
//fun.h函数文件
#include<iostream>
#include "base.h"
using namespace std;
void input1(vector<student> &data1)
{
char flag;
do{
flag='n';
string n1;
int m1;
cout<<"请输入学生编号"<<endl;
cin>>m1;
cout<<"请输入学生姓名"<<endl;
cin>>n1;
student obj1(n1,m1);
obj1.setscore();
data1.push_back (obj1);
cout<<"\n继续输入请按Y"<<endl;
cin>>flag;
}while(flag=='y');
}
void input2(vector<teacher> &data2)
{
char flag;
do{
flag='n';
string n1;
int m1;
cout<<"\n请输入老师的编号\n"<<endl;
cin>>m1;
cout<<"\n请输入老师的姓名\n"<<endl;
cin>>n1;
teacher obj2(n1,m1);
obj2.setpaper();
data2.push_back (obj2);
cout<<"\n继续输入请按Y"<<endl;
cin>>flag;
}
while(flag=='y');
}
void output(vector<student> &data1,vector<teacher> &data2)
{
int i;
if(data1.size()==0)
cout<<"\n没有学生的信息!\n";
else
{
cout<<"\n优秀学生评选如下:"<<endl;
cout<<"\t"<<"学号"<<"\t"<<"姓名"<<endl;
for(i=0;i!=data1.size();++i)
data1[i].display();
}
if(data2.size()==0)
cout<<"\n没有老师的信息!"<<endl;
else
{
cout<<"\n优秀老师评选结果如下:"<<endl;
cout<<"\t"<<"编号"<<"\t"<<"姓名"<<endl;
for(i=0;i!=data2.size();++i)
data2[i].display();
}
}
//main.cpp主函数文件
#include <iostream>
#include "fun.h"
using namespace std;
int main()
{
vector<student> data1;
vector<teacher> data2;
char ch;
cout<<"\t\t\t\t欢迎使用本系统!"<<endl;
while(1)
{
cout<<"\t\t\t\t\t1.录入学生资料\n"
<<"\t\t\t\t2.录入教师资料\n"
<<"\t\t\t\t3.查看评比结果\n"
<<"\t\t\t\t4.退出系统"<<endl;
cin>>ch;
switch(ch)
{
case '1': input1(data1);break;
case '2': input2(data2);break;
case '3': output(data1,data2);break;
case '4': cout<<"\n\t\t\t\tGoodbye!"<<endl;return 0;
}
}
return 0;
}