最近准备陆续把之前敲过的代码放进来,顺序就……..(:
在对类的定义和使用有了初步认识之后,可以尝试面向对象的风格设计一个简单的学生信息管理系统(:
#include <iostream> #include <string> using namespace std;class student { private : string name; string ID; string number; string speciality; int age; public : student (); student (string na, string id, string num, string spec, int ag); student (const student& per); string GetName () ; string GetID () ; string GetNumber () ; string GetSpec () ; int GetAge () ; void Display () ; void Input () ; };
#include "student.h" student::student () { name = " " ; age = 0 ; } student::student (string na, string id, string num, string spec, int ag):name (na),ID (id),number (num),speciality (spec),age (age) { } student::student (const student& per) { name = per.name; ID = per.ID; number = per.number; speciality = per.speciality; age = per.age; } string student::GetName () { return name; } string student::GetID () { return ID; } string student::GetNumber () { return string (); } string student::GetSpec () { return speciality; } int student::GetAge () { return age; } void student::Display () { cout << "姓 名:" ; cout << name << endl; cout << "身份证:" << ID << endl; cout << "学 号:" << number << endl; cout << "专 业:" << speciality << endl; cout << "年 龄:" << age << endl; } void student::Input () { cout << "输入姓 名:" ; cin >> name; cout << "输入身份证号:" ; cin >> ID; cout << "输入年 龄:" ; cin >> age; cout << "输入专 业:" ; cin >> speciality; cout << "输入学 号:" ; cin >> number; }
#include "student.h" const int N = 10 ;void menu () ;void OutputStu (student* array) ;void InputStu (student* array) ;int SearchStu (student* array, string na) ;int coo = 0 ;int main () { student array[N]; int choice; string na; do { menu (); cout << "Please input your choice:" ; cin >> choice; switch (choice) { case 1 :InputStu (array);break ; case 2 :cout << "Input the name searched:" << endl; cin >> na; int i; i = SearchStu (array, na); if (i == N) cout << "查无此人!\n" ; else array[i].Display (); break ; case 3 :OutputStu (array);break ; case 0 :cout << "Thanks,see you..." << endl;break ; default :cout << "Input error!" << endl; break ; } } while (choice); return 0 ; } void menu () { cout << "*********1.录入信息*********\n" ; cout << "*********2.查询信息*********\n" ; cout << "*********3.浏览信息*********\n" ; cout << "*********0.退 出*********\n" ; } void OutputStu (student* array) { cout << "学生总人数=" << coo << endl; for (int i = 0 ;i < coo;i++) array[i].Display (); } void InputStu (student* array) { char ch; do { array[coo].Input (); coo++; cout << "继续输入吗?(Y or N)\n" ; cin >> ch; } while ((ch == 'Y' ) || (ch == 'y' )); } int SearchStu (student* array, string na) { int i, j = N; for (i = 0 ;i < coo;i++) { if (array[i].GetName () == na) j = i; } return j; }
*********1.录入信息********* *********2.查询信息********* *********3.浏览信息********* *********0.退 出********* Please input your choice:1 输入姓 名:zhangcheng 输入身份证号:320106200101011819 输入年 龄:18 输入专 业:computer 输入学 号:19070302 继续输入吗?(Y or N) Y 输入姓 名:wangcheng 输入身份证号:320101200201014011 输入年 龄:17 输入专 业:accounting 输入学 号:19070320 继续输入吗?(Y or N) N *********1.录入信息********* *********2.查询信息********* *********3.浏览信息********* *********0.退 出********* Please input your choice:2 Input the name searched: zhangcheng 姓 名:zhangcheng 身份证:320106200101011819 学 号:19070302 专 业:computer 年 龄:18 *********1.录入信息********* *********2.查询信息********* *********3.浏览信息********* *********0.退 出********* Please input your choice:3 学生总人数=2 姓 名:zhangcheng 身份证:320106200101011819 学 号:19070302 专 业:computer 年 龄:18 姓 名:wangcheng 身份证:320101200201014011 学 号:19070320 专 业:accounting 年 龄:17 *********1.录入信息********* *********2.查询信息********* *********3.浏览信息********* *********0.退 出********* Please input your choice:0 Thanks,see you...