1.掌握类的数据共享与保护的概念和使用方法 2.学习简单的面向对象程序的编写
1、硬件要求:计算机一台。 2、软件要求:Windows操作系统,Dev-C++或VC++ 6.0编译环境
编写程序判断学生能否通过某个级别的考试,全部操作在类Grade中实现。要求:
话不多说,上代码:
#include<iostream> #include<string> #include<cstring> #define _CRT_SECURE_NO_WARNING #pragma warning(disable:4996) using namespace std; int numb; class Grade { public: Grade(const char* n, int s=0) { strcpy(name, n); score = s; static int count = 0; count++; numb = count; } void PrintData() { cout << "Name=" << name << " " << "Score=" << score ; isPass(); } friend void Horl(Grade& m, Grade& n){ if (m.score > n.score) { cout << "成绩最高的是:" << m.name << ",他的分数是:" << m.score; } else if (m.score == n.score) { cout << "成绩最高的是:" << n.name << ",他的分数是:" << n.score; } else cout << m.name << "和" << n.name << "分数相同,均为:" << m.score << endl; } private: void isPass() { if (score>=60) cout << " Pass" << endl; else cout << " Not Pass" << endl; } char name[50]; int score; }; int main() { Grade a("Sun",100),b("Wu",90),c("Jiang",52),d("Zhou"); a.PrintData(); b.PrintData(); c.PrintData(); d.PrintData(); cout << "共有" << numb << "个学生" << endl; return 0; }