黑马程序员--定义一个学生类

 

---------------------- ASP.Net+Android+IOS开发.Net培训、期待与您交流! ----------------------

 

输入学生的学号、姓名和语文、数学和英语3门课程的成绩,计算并显示每位学生的各门课的成绩,总成绩和平均成绩以及统计学生的人数

#include<iostream>
#include<string.h>

using namespace std;
//----------------student类的声明部分开始-------------------
class Student{
public:
// ---------------三个重载构造函数的开始--------------------
Student();
Student(int NewId, char * NewName, int NewScore0,int NewScore1,int NewScore2);
Student(int NewId, char * NewName, int * NewScore);
//----------------三个重载函数声明结束----------------------
//----------------析构函数的内联定义------------------------

~Student()
{
cout<<endl<<name<<"被析构了!"<<endl;
}

void StuInput();
void StuOutput();
float AverScore();
void ScoreSum();

private:

static int stu_count;
int id;
char name[10];
float score[3];
float ScoSum;
};

//-------------------student类的声明部分结束----------------------------
//-------------------student类的实现部分开始----------------------------

int Student::stu_count=0;
//static型的数据成员必须全局性初始化

//-------------------三个重载构造函数定义开始---------------------------

Student::Student()
{
cout<<"没有参数的构造函数被调用!"<<endl;

stu_count++;
id=0;
strcpy(name,"aaaa");
score[0]=score[1]=score[2]=0;
ScoSum=0;
}

Student::Student(int NewId, char * NewName, int NewScore0,int NewScore1,int NewScore2)
{
cout<<"用三个整数成绩初始化数组的函数被构造函数调用了!"<<endl;
stu_count++;
id=NewId;
strcpy(name,NewName);
score[0]=NewScore0;
score[1]=NewScore1;
score[2]=NewScore2;
}

Student::Student(int NewId, char * NewName, int * NewScore)
{
cout<<"用成绩数组初始化的成绩数组被构造函数调用了!"<<endl;
stu_count++;
id=NewId;
strcpy(name,NewName);
for(int i = 0 ; i < 3 ; i ++){
score[i]= * NewScore++;
}
}

//---------------------三个重载构造函数的结束---------------------------

void Student::StuInput()
{
stu_count++;
int i;
cout<<"\n请输入整数编号,回车结束:\n";
cin>>id;
cout<<"\n请输入姓名字符串,回车结束:\n";
cin>>name;
cout<<"请输入三门课程成绩,每门回车结束!:\n";
for(i = 0; i < 3 ; i ++){
cin>>score[i];
}
cout<<endl;
}

void Student::StuOutput()
{
cout<<"现在共有"<<stu_count<<"名同学!"<<endl;
cout<<id<<'\t'<<name<<'\t'<<score[0]<<'\t'<<score[1]<<'\t'<<score[2]<<endl;
ScoreSum();
cout<<"总成绩是:"<<ScoSum<<endl;
cout<<"平均成绩是:"<<AverScore()<<endl;
}

void Student::ScoreSum()
{
ScoSum=score[0]+score[1]+score[2];
}

float Student::AverScore()
{
return ScoSum/3.0;
}

//---------------------------Student类的实现部分结束------------------------------

//---------------------------main函数的开始---------------------------------------

int main()
{
int temScore[]={100,100,100};
Student LiHong;
LiHong.StuOutput();
cout<<endl<<"----------------------输出第一条分割线----------------------------"<<endl<<endl;
Student ZhangJun(1001,"张军",89,79,69);
Student Wangxinye(1002,"王新野",temScore);
cout<<endl<<"----------------------输出第二条分割线----------------------------"<<endl<<endl;
ZhangJun.StuOutput();
cout<<endl<<"----------------------输出第三条分割线----------------------------"<<endl<<endl;
Wangxinye.StuOutput();
return 0;
}

 

---------------------- ASP.Net+Android+IOS开发.Net培训、期待与您交流! ----------------------

详细请查看:<ahref="http://edu.csdn.net" target="blank">http://edu.csdn.net</a>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值