C++类之 学生基本情况例子
2006-11-07 20:49
344 查看
//有两个文件 一个头文件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
='/0';
}
char *getName() const //读出姓名
{
return (char *)strName;
}
void setId(char *id) //设置ID
{
int n=strlen(id);
strncpy(strId,id,n);
strId
='/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;
}
//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
='/0';
}
char *getName() const //读出姓名
{
return (char *)strName;
}
void setId(char *id) //设置ID
{
int n=strlen(id);
strncpy(strId,id,n);
strId
='/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;
}
相关文章推荐
- 定义一个描述学生(Student)基本情况的类,数据成员包括姓名(name)、学号(num)、数学成绩(mathScore)、英语成绩(englishScore)、人数(count)、数学总成绩(ma
- 定义一个描述学生(Student)基本情况的类,数据成员包括姓名(name)、学号(num)、数学成绩(mathScore)、英语成绩(englishScore)、人数(count)、数学总成绩(ma
- 一,系学生工作基本情况简介
- 定义一个描述学生(Student)基本情况的类,数据成员包括姓名(name)、学号(num)、数学成绩(mathScore)、英语成绩(englishScore)、人数(count)...
- 《定义一个学生基本情况的类——C#第四周》
- 我和我的同伴是如何用具体的代码例子,说明源代码管理的基本操作
- 我国消费税基本情况
- 有10个学生,每个学生的数据包括学号、姓名、3门课的成绩,编写如下函数: (1) 输入10个学生 的基本信息(input); (2) 求出每个学生的平均分(average); (3) 求出平均分
- (1) 输入学生的基本信息,计算每个学生的平均分,并将学生信息存入磁盘文件“stud”中; (2) 将文件stud中的数据按平均分从高到低的顺序进行排序,并将排序结果存放到文件“stud_sort”中
- R语言 基本语法 附综合性例子
- 统计学生提交作业情况
- C++类的基本认识
- Django介绍、安装配置、基本使用、Django用户注册例子
- 【2分钟问卷】海大在校学生计算机使用、学习习惯情况调查(6题-2分钟)
- Ceph的基本情况进行概要介绍
- 一些 NSArray 的基本操作代码例子
- 给学生练习的几个例子
- 检测 Linux 内存使用情况的 free 命令的10个例子
- SQL SERVER 查询Job作业基本信息及执行情况