c++程序设计B实验-类与对象的其他特性.docVIP

  • 192
  • 0
  • 约3.59千字
  • 约 6页
  • 2017-12-23 发布于河南
  • 举报

c++程序设计B实验-类与对象的其他特性.doc

c程序设计B实验-类与对象的其他特性

实验类型 验证 实验学时 2 一、实验目的和要求 掌握类中静态成员的的定义和使用 掌握友元的定义和使用 2.掌握常成员的定义和使用 二、实验环境(实验设备) 联想电脑 Studio Visual C++ 6.0 三、实验内容及过程 1.设计一个程序:定义学生类Student ,用于存放并输出学生的姓名、学号、四门功课的成绩及总成绩。具体要求如下: 1) 提示用户输入学生的姓名、四门课的成绩; 2) 计算并输出全班各门课程的平均成绩(纵向求平均); 3) 输出总成绩最高的学生的所有信息;(考虑并列第一的情况) 4)要求学号自动增加并定义为常成员。 //请学有余力的同学补充函数实现按总分排序,需要用到友元函数 源代码: #includeiostream using namespace std; class student { private: char name[20]; const double num; float math, prog, eng, phy, total; static float score[5]; static double count; static int n; static student*max[10]; public: student(); void display(); static void showaverscore(); static void showthemost(); }; float student::score[5] = { 0 }; double student::count = 312000; int student::n=0; student* student::max[10] = {NULL }; student::student():num(++count) { cout 请输入学生 num 姓名及数学,英语,程序设计,物理成绩!endl; cin name; cin math; cin eng; cin prog; cin phy; cout endl; score[0] += math; score[1] += eng; score[2] += prog; score[3] += phy; total = math + eng + prog + phy; if (total score[4]) { score[4] = total; for (int i = 0; i 10 max[i] != NULL; i++) max[i] = NULL; max[0] = this; } else if (total == score[4]) { int i; for (i = 0; i 10 max[i] != NULL; i++); max[i] = this; n++; } } void student::display() { cout 学生学号: num \t; cout 学生姓名: name \t; cout 体育成绩: math \t; cout 英语成绩: eng \t; cout 物理成绩: phy \t; cout 程序设计成绩: prog \t; cout 总分: total \t; } void student:: showaverscore() { cout 数学平均成绩: score[0] / 2 \t; cout 英语平均成绩: score[1] / 2 \t; cout 物理平均成绩: score[3] / 2 \t; cout 程序设计平均成绩: score[2] / n endl; } void student::showthemost() { cout 总分最高学生信息为: endl; for (int i = 0; i 10 max[i] != NULL; i++) max[i]-display(); cout endl; } int main() { student a[2] = {}; student::showaverscore(); student::showthemost(); system(pause); return 0; } 运行截图: 2.设计一个Width类,其中包含一个私有静态数据成员count,用于记录全局存在的Width对象的个数。每次创建一个对象则计数器自动加1,每次销毁时,自动减1。然后设计一个专门访问它的静态成员函数NumWi

您可能关注的文档

文档评论(0)

1亿VIP精品文档

相关文档