//有两个文件 一个头文件student.h 一个cpp文件student.cpp
//student.h
#include <iostream.h>
#include <string.h>
class score //成绩类
{
public:
score()
{
math=0;
english=0;
c=0;
}
void setmath(int i) //设置数学成绩
{
math=i;
}
void setintenglish(int i) //设置英语成绩
{
english=i;
}
void setc(int i) //设置C成绩
{
c=i;
}
int getmath() const //读出数学成绩
{
return math;
}
int getenglish() const //读出英语成绩
{
return english;
}
int intc() const //读出C成绩
{
return c;
}
int getSum() const //读出总成绩
{
int i;
i=math+english+c;
return i;
}
double getAve() const //读出平均成绩
{
double i;
i=(math+english+c)/3.0;
return i;
}
private:
int math,english,c;
};
class student //学生信息类
{
public:
void setName(char *name) //设置姓名
{
int n=strlen(name);
strncpy(strName,name,n);
strName[n]='/0';
}
char *getName() const //读出姓名
{
return (char *)strName;
}
void setId(char *id) //设置ID
{
int n=strlen(id);
strncpy(strId,id,n);
strId[n]='/0';
}
char *getId() const //读出ID
{
return (char *)strId;
}
score chengji;
private:
char strName[20];
char strId[20];
};
//student.cpp
#include <iostream.h>
#include "student.h"
void main()
{
student stu;
stu.setId("10234652"); //学号
stu.setName("LiMing"); //姓名
stu.chengji.setmath(85); //数学成绩
stu.chengji.setintenglish(70); //英语成绩
stu.chengji.setc(90); //C成绩
cout<<"学生 "<<stu.getName()<<" 的总成绩是: "<<stu.chengji.getSum()<<" 平均成绩: "
<<stu.chengji.getAve()<<endl;
}