C++类之 学生基本情况例子

//有两个文件 一个头文件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;
 
}

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

物欲添彩

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值