发个简单的程序,有简单的template用法,简单的派生类
#include <string.h>
#include <vector>
using namespace std;
class stu
{
private:
string name;
double math;
double English;
double other;
public:
stu(string a,double b,double c,double d){
name=a,math=b,English=c,other=d;
}
dis()
{
cout<<"the student's name is:"<<name<<endl;
cout<<"his/her math score is:"<<math<<endl;
cout<<"his/her English score is:"<<English<<endl;
cout<<"his/her other score is:"<<other<<endl;
}
};
class teacher : public stu
{
private:
double salary;
public:
teacher(string a,double b,double c,double d,double s):stu(a,b,c, d)
{
salary=s;
}
dis()
{
cout<<"teacher's salary is "<<salary<<" One month"<<endl;
}
};
template <typename T>T add(T a,T b)
{
T c=a+b;
return c;
}
main()
{
teacher stud{"jack",75.4,65,86,2500};
stu td=(stu)stud;
td.dis();
stud.dis();
float a,b;
int k,j;
cin>>a>>b;
cout<<"a+b="<<add(a,b)<<endl;
cin>>k>>j;
cout<<"k+j="<<add(k,j)<<endl;
}
#include <string.h>
#include <vector>
using namespace std;
class stu
{
private:
string name;
double math;
double English;
double other;
public:
stu(string a,double b,double c,double d){
name=a,math=b,English=c,other=d;
}
dis()
{
cout<<"the student's name is:"<<name<<endl;
cout<<"his/her math score is:"<<math<<endl;
cout<<"his/her English score is:"<<English<<endl;
cout<<"his/her other score is:"<<other<<endl;
}
};
class teacher : public stu
{
private:
double salary;
public:
teacher(string a,double b,double c,double d,double s):stu(a,b,c, d)
{
salary=s;
}
dis()
{
cout<<"teacher's salary is "<<salary<<" One month"<<endl;
}
};
template <typename T>T add(T a,T b)
{
T c=a+b;
return c;
}
main()
{
teacher stud{"jack",75.4,65,86,2500};
stu td=(stu)stud;
td.dis();
stud.dis();
float a,b;
int k,j;
cin>>a>>b;
cout<<"a+b="<<add(a,b)<<endl;
cin>>k>>j;
cout<<"k+j="<<add(k,j)<<endl;
}
[此贴子已经被作者于2017-12-15 19:26编辑过]