@图灵不吃苹果
C++学生类的设计(带注释)
实验要求
设计一个图形基类:Shape,在此基础上派生Rectangle、Circle、Triangle 类。
设计一个 Studnet(学生)类
1、基本信息:学号、姓名、性别、出生日期、年级、班级、院系、专业;其中:基本信息为 private 属性,成员函数为public 属性;
2、Student 类有多个构造函数:缺省构造函数、带参数的构造函数、带默认参数的构造函数
3、基本功能:
1)使用对象数组保存学生对象;
2)可以从键盘输入学生的基本信息;
3)SetInfo(形参表)函数可以修改学生的基本信息函数,例如:班级、专业等;
4)Show()函数显示学生信息;
[实验提示]
1、注意带参数的构造函数和带默认参数的构造函数的声明与定义;
2、从键盘输入基本信息,调用带参数的构造函数生成学生对象;
3、当输入数据为不合理数据时,提示用户输入错误;
[测试数据]
1、学号:2005001 姓名:zhangesan 出生日期:85 年11 月 年级:05 级 班级:jy01 院系:computer 专业:application
2、学号:2005002 姓名:lihuan 出生日期:84 年11 月 年级:05级 班级:tx02 院系:math 专业:application
3、学号:2005003 姓名:wang 出生日期:85 年7 月 年级:05 级 班级:jy01 院系:computer 专业:software
4、学号:2005004 姓名:zhan 出生日期:85 年11 月 年级:05 级 班级:jy02 院系:SCI
5、学号:2005005 姓名:song 出生日期:85 年11 月 年级:05级 班级:tx02 院系:math 专业:application
代码部分
<头文件student.h>
#ifndef student_h
#define student_h
#include <string>
using namespace std;
class student
{
public:
student();
void show();
int setinfo();
int findstu(string n);
void setName(string n);
void setID(int n);
void setYear(int y);
void setMonth(int m);
void setNianji(string n);
void setHclass(string n);
void setYuan(string n);
void setMajor(string n);
void setSexual(char s);
private:
int ID;//学号;
string name;//姓名;
char sexual;//f为女 m为男;
int year;//出生年
int month;//月
string nianji;//年级;
string Hclass;//班级
string yuan;//院
string major;//专业
};
#endif
<student.cpp>
#include "student.h"
#include <iostream>
extern int m;
student::student() //无参数构造
{
ID = ' ';
name = ' ';
sexual = ' ';
year = 0;
month = 0;
Hclass = ' ';
yuan = ' ';
major = ' ';
cout << "无参数构造完成 " << endl;
}
void student::show()
{
cout << "学号:" << this->ID << " 姓名:" << this->name << " 性别:" << this->sexual << endl;
cout << "出生日期:" << this->year << "年" << this->month << "月" << " 年级:" << this->nianji << endl;
cout << "院系" << this->yuan << " 专业:" << this->major << " 班级:" << this->Hclass << endl;
}
int student::setinfo()
{
while (true) {
cout << "需要改动的地方://不能输入负数(输入Q结束)" << endl;
cout << "1.学号 2.出生年 3.出生月 4.年级 5.院系 6.专业 7.班级" << endl;
int w;
cin >> w;
int ye, mon,IDD;
string yuan, maj, cla, Nianj;
switch (w)
{
case 1: {cout << "输入修改后的内容:"; cin >> IDD;
while (IDD < 0)
{
cout << "请输入正数:" << endl;
cin >> IDD; }
this->setID(IDD); break; }
case 2: {cout << "输入修改后的内容:";
cin >> ye;
while (ye < 0)
{
cout << "请输入正数:" << endl;
cin >> ye;
}this->setYear(ye); break; }
case 3: {cout << "输入修改后的内容:"; cin >> mon;
while (mon < 0)
{
cout << "请输入正数:" << endl;
cin >> mon;
}this->setMonth(mon); break; }
case 4: {cout << "输入修改后的内容:"; cin >> Nianj; this->setNianji(Nianj); break; }
case 5: {cout << "输入修改后的内容:"; cin >> yuan; this->setYuan(yuan); break; }
case 6: {cout << "输入修改后的内容:"; cin >> maj; this->setMajor(maj); break; }
case 7: {cout << "输入修改后的内容:"; cin >> cla; this->setHclass(cla); break; }
default: this->show();
return 0;
}
}
}
int student::findstu(string n)
{
if (this->name == n)
{
return 1;
}
else return 0;
}
void student::setName(string n)
{
name = n;
}
void student::setID(int n)
{
ID = n;
}
void student::setYear(int y)
{
year = y;
}
void student::setMonth(int m)
{
month = m;
}
void student::setNianji(string n)
{
nianji = n;
}
void student::setHclass(string n)
{
Hclass = n;
}
void student::setYuan(string n)
{
yuan = n;
}
void student::setMajor(string n)
{
major = n;
}
void student::setSexual(char s)
{
sexual = s;
}
<main.cpp>
// code5.2.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//设置一个student类
//构造类的数组 来进行存储
//为了让无参数构造显示出来 就打印了“无参数构造完成”的提示
//show是展示学生的信息
//setinfo是改变学生的信息
//findstu是找到需要改变的学生 的位置
//代码中可以通过id和学生的姓名查找学生
//id自动初始化 通过学生的年级进行初始化
//其中用到了string 和 int类的转换
//set函数都是传入参数修改信息
//在查找函数的时候用loc定位
//loc初始化为-1
//如果没有定位成功 loc仍然是-1 就知道没有找到学生 则可以继续查找
#define _CRT_SECURE_NO_WARNINGS
#include <iostream> //输入输出流
#include "student.h" //头文件
int m;
student stu[10];
using namespace std;
int main()
{
//freopen("in.txt", "r", stdin); 本来打算用打开文件的形式自动读进去信息 但是发现容易报错 而且半天找不出来错误在哪里 就注释掉了
string Na, yu, cla, maj, A;
char se;
int ye, mon,idd;
cout << "how many students:";
cin >> m;
for (int i = 0; i < m; i++) {
cout << "输入姓名:";
cin >> Na;
stu[i].setName(Na);
cout << "输入性别:f/m(f为女性 m为男性)";
cin >> se;
stu[i].setSexual(se);
cout << "出生年:"; cin >> ye;
while (ye < 0) {
cout << "请输入正数:" << endl;
cin >> ye;
}stu[i].setYear(ye);
cout << "出生月:"; cin >> mon; while (mon < 0||mon>12) {
cout << "请输入正数:" << endl;
cin >> mon;
}
stu[i].setMonth(mon);
cout << "输入年级:"; cin >> A; stu[i].setNianji(A);
cout << "输入院系:"; cin >> yu; stu[i].setYuan(yu);
cout << "输入班级:"; cin >> cla; stu[i].setHclass(cla);
cout << "输入专业:"; cin >> maj; stu[i].setMajor(maj);
idd = atoi(A.c_str());
idd = 2000000 + idd * 1000 + i + 1;
stu[i].setID(idd);
}
cout << "-----------------------------------------" << endl;
for (int i = 0; i < m; i++) {
stu[i].show();
}
cout << "-----------------------------------------" << endl;
cout << "需要改动的学生姓名:" << endl;
string Nam;
cin >> Nam;
int loc=-1;
for (int i = 0; i < m; i++)
{
if (stu[i].findstu(Nam) != 0) { loc = i; break; }
}
if (loc == -1) { cout << "未找到该学生 sry" << endl; }
else{ stu[loc].setinfo(); }
cout << "-----------------------------------------" << endl;
for (int i = 0; i < m; i++) {
stu[i].show();
}
return 0;
}
// IDD = 2000000 + A[2] * 1000+i+1;
// stu[i].setID(IDD);