参考:面向对象程序设计及C++,P68
共3个文件
// excise_20230808e.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//
#include <iostream>
#include "Student.h"
const int N = 10;
//int count = 0;
void menu();
void OutputStu(Student* array, int& count);
void InputStu(Student* array, int& count);
int SearchStu(Student* array, string na, int& count);
int main()
{
Student array[N]; //定义学生数组
int choice;
string na;
int cnt = 0; //用于计数
do
{
menu();
cout << "输入你的选择" << endl;
cin >> choice;
switch(choice)
{
case 1:
InputStu(array, cnt);
break;
case 2:
cout << "输入查找的姓名" << endl;
cin >> na;
int i;
i = SearchStu(array, na, cnt);
if (i == N)
cout << "查无此人" << endl;
else
array[i].Dislay(); //显示
break;
case 3:
OutputStu(array,cnt);
break;
case 0:
cout << "再见" << endl;
//exit(0);
break;
default:
cout << "输入错误" << endl;
break;
}
} while (choice);
std::cout << "Hello World!\n";
}
void menu() //菜单
{
cout << "*********1.录入信息**********" << endl;
cout << "*********2.查询信息**********" << endl;
cout << "*********3.浏览信息**********" << endl;
cout << "*********0.退 出**********" << endl;
}
void OutputStu(Student* array,int& count)
{
cout << "学生总人数=" << count << endl;
for (int i = 0; i < count; i++)
array[i].Dislay();
}
void InputStu(Student* array, int &count) //输出数组元素
{
char ch;
do
{
array[count].input(); //输入一个学生对象
count++;
cout << "继续输入吗?(Y/N)"<<endl;
cin >> ch;
} while ((ch == 'Y') ||( ch == 'y'));
}
int SearchStu(Student* array, string na,int &count)
{
int j = N;
for (int i = 0; i < count; i++)
if (array[i].GetName() == na)
j = i;
return j;
}
#pragma once
#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 Dislay(); //显示学生信息
void input(); //输入学生信息
~Student();
};
#include "Student.h"
Student::Student()
{
name = " ";
age = 0;
}
Student::~Student()
{
}
Student::Student(string na, string id, string num, string spec,
int age):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 number;
}
string Student:: GetSpec()
{
return speciality;
}
int Student::GetAge()
{
return age;
}
void Student::Dislay() //显示学生信息
{
cout << "姓名:" << name << endl;
cout << "身份证:" << ID << endl;
cout << "学号:" << number << endl;
cout << "专业:" << speciality << endl;
cout << "年龄:" << age << endl;
}
void Student::input() //输入学生信息
{
cout << "姓名:" << endl;
cin >> name;
cout << "身份证:" << endl;
cin >> ID;
cout << "学号:" << endl;
cin >> number;
cout << "专业:" << endl;
cin >> speciality;
cout << "年龄:" << endl;
cin >> age;
}
运行结果: